Vivek Goswami
Vivek Goswami

Reputation: 432

CNContact framework phonenumbers store blank

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];

Upvotes: 1

Views: 915

Answers (1)

Dipen Panchasara
Dipen Panchasara

Reputation: 13600

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)
}

Upvotes: 1

Related Questions