iOS错误创建联系人

时间:2016-12-15 00:56:00

标签: contacts

我正在使用以下代码创建联系人:

CNContactRelation *rel = [[CNContactRelation alloc] initWithName:@"John"];
CNLabeledValue *lv = [[CNLabeledValue alloc] labeledValueBySettingLabel:@"_$!<Friend>!$_" value:rel];

NSMutableArray *arr = [NSMutableArray new];
[arr addObject:lv];

CNMutableContact *contact = [[CNMutableContact alloc] init];
contact.givenName = @"Joe";
[contact setContactRelations:arr];

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

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

我在保存时收到此错误:

error = Error Domain=CNErrorDomain Code=2 "(null)" UserInfo={CNKeyPaths=(
contactRelations)}

如果我删除了设置联系关系的行([contact setContactRelations:arr]),那么联系人就可以了。

有人知道接触关系数组有什么问题,还是设置不允许?

1 个答案:

答案 0 :(得分:0)

试试这个。它对我有用。 Swift 3.1。必须导入“联系人”

 let store = CNContactStore()
 let contact = CNMutableContact()
 contact.givenName = "first Name"
 contact.familyName = "Last Name"

 let workEmail = CNLabeledValue(label:CNLabelWork, value:"EMAIL" as NSString)
 contact.emailAddresses = [workEmail]

 let homePhone = CNLabeledValue(label: CNLabelWork, value: CNPhoneNumber(stringValue :NUMBER ))
 contact.phoneNumbers = [homePhone]



 let saveRequest = CNSaveRequest()
 saveRequest.add(contact, toContainerWithIdentifier:nil)
 try! store.execute(saveRequest)