1

here i am trying to add new contact number from web array and there is no data enter in device

CNLabeledValue *homePhone = [CNLabeledValue labeledValueWithLabel:CNLabelPhoneNumberMobile value:[CNPhoneNumber phoneNumberWithStringValue:[NSString stringWithFormat:@"%@",[[contactDict valueForKey:@"Mobile"]objectAtIndex:i]]]];

        contact.phoneNumbers = @[homePhone];
CNSaveRequest *request = [[CNSaveRequest alloc] init];
        [request addContact:contact toContainerWithIdentifier:nil];
Vivek Goswami
  • 432
  • 3
  • 16

1 Answers1

1

Objective-C

// create contact
CNMutableContact *contact = [[CNMutableContact alloc] init];
contact.familyName = @"Doe";
contact.givenName = @"John";

// Add mobile number
CNLabeledValue *mobileNumber = [CNLabeledValue labeledValueWithLabel: CNLabelPhoneNumberMobile value:[CNPhoneNumber phoneNumberWithStringValue:@"123-123-1212"]];
contact.phoneNumbers = @[mobileNumber];

CNSaveRequest *request = [[CNSaveRequest alloc] init];
[request addContact:contact toContainerWithIdentifier:nil];

// save contact
NSError *saveError;
if (![store executeSaveRequest:request error:&saveError]) {
    NSLog(@"error = %@", saveError);
}

Swift

// create contact
var contact: CNMutableContact = CNMutableContact()
contact.familyName = "Doe"
contact.givenName = "John"

// Add mobile number
var mobileNumber: CNLabeledValue = CNLabeledValue.labeledValueWithLabel(CNLabelPhoneNumberMobile, value: CNPhoneNumber.phoneNumberWithStringValue("123-123-1212"))
contact.phoneNumbers = [mobileNumber]
var request: CNSaveRequest = CNSaveRequest()
request.addContact(contact, toContainerWithIdentifier: nil)

// save contact
NSError * saveError
if !store.executeSaveRequest(request, error: saveError) {
    NSLog("error = %@", saveError)
}
Dipen Panchasara
  • 13,480
  • 5
  • 47
  • 57
  • is it throwing any error while `executeSaveRequest`? may be your values would be `null` or `empty`. – Dipen Panchasara Mar 11 '16 at 11:27
  • I reviewed your code and tested on my machine, its working like charm, surely you have issue with your data dictionary. check it again. – Dipen Panchasara Mar 11 '16 at 11:38
  • Problem solved i was add only one value contact.phoneNumbers = [mobileNumber], if mobile number blank there is no insertion done, now i have added multiple contact like work,iPhone,mobile, and Main in contact.phoneNumbers = [work,iPhone,mobile] and now it's done... Thank you @Dipen Panchasara – Vivek Goswami Mar 11 '16 at 12:01
  • @VivekGoswami Welcome Happy Coding :) – Dipen Panchasara Mar 11 '16 at 12:09