ios - CNContact 框架电话号码存储空白

标签 ios objective-c cncontact cncontactstore

我在这里尝试从网络数组中添加新的联系电话,但设备中没有输入数据

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

最佳答案

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

关于ios - CNContact 框架电话号码存储空白,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35938728/

相关文章:

objective-c - xcode View 被调用并初始化但未显示

iphone - 在滑动通知解锁后将用户重定向到我的应用程序的特定屏幕

ios - 在 OpenGL ES 中绑定(bind)多个缓冲区

iphone - 如何通过Web服务(php)sql查询从ios iphone sdk中选择条件来捕获数据

ios - 如何在使用自定义 UITableViewCell 创建 UITableViewCell 后覆盖它?

ios - NSOperationQueue 和 NSURLSession 有什么区别?

ios - CNContactPicker 授予权限不提示

ios - 自动布局黄色警告。它会使我的应用程序在运行时崩溃吗

iOS9 联系人框架从新保存的联系人中获取标识符

ios - 在 Swift 2.0 Contact Framework iOS 中从 CNContactPicker 获取电子邮件地址