2017-10-08 128 views
0

我使用聯繫人組創建了一個組,然後我想向該組添加聯繫人。使用聯繫人框架將聯繫人添加到組

 NSPredicate *predicate = [CNGroup predicateForGroupsWithIdentifiers:@[[[NSUserDefaults standardUserDefaults]objectForKey:@"groupIDentifier"]]]; 
     NSArray *groups = [store groupsMatchingPredicate:predicate error:&saveError]; 

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

     CNLabeledValue *homePhone = [CNLabeledValue labeledValueWithLabel:CNLabelHome value:[CNPhoneNumber phoneNumberWithStringValue:@"312-555-1212"]]; 
     contact.phoneNumbers = @[homePhone]; 

     CNSaveRequest *request = [CNSaveRequest new]; 
     CNGroup *group = [groups firstObject]; 
     [request addContact:contact toContainerWithIdentifier:group.identifier]; 

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

錯誤是:

誤差=錯誤域= CNErrorDomain代碼= 200 「更新的記錄不 存在」 的UserInfo = {CNInvalidRecordIdentifiers =( 「45FFBB0D-C74B-4A14-8293- 9099EA7DEF81:ABGroup」),NSLocalizedDescription =更新的記錄不存在, NSLocalizedFailureReason =該保存請求失敗,因爲它更新一個 記錄不存在或已被刪除}

。 210

我也嘗試使用:

[request addMember:contact toGroup:[groups firstObject]]; 

這種情況下,錯誤是:

error = Error Domain=CNErrorDomain Code=200 "Updated Record Does Not Exist" UserInfo={CNInvalidRecords=(
    "<CNContact: 0x7f8ce97aa640: identifier=7CC6BC1D-1B23-48DA-8282-06115F542A97:ABPerson, givenName=John, familyName=Doe, organizationName=, phoneNumbers=(\n \"<CNLabeledValue: 0x600001873cc0: identifier=68277209-3AE4-40AF-9EEA-DF0E1D01883C, label=_$!<Home>!$_, value=<CNPhoneNumber: 0x600000433300: stringValue=312-555-1212, initialCountryCode=(null)>>\"\n), emailAddresses=(\n), postalAddresses=(\n)>"), NSLocalizedFailureReason=The save request failed because it updates a record that does not exist or has already been deleted., NSLocalizedDescription=Updated Record Does Not Exist} 

回答

2

我發現瘋狂的事情是:我需要通話雙方使用addMember和的addContact,實際上使聯繫人添加到組。

CNGroup *group = [groups firstObject]; 
    [request addMember:contact toGroup:group]; 
    [request addContact:contact toContainerWithIdentifier:nil]; 

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

這實際上是爲了達到目的,但我不知道爲什麼我必須實際做出兩種類型的請求。

相關問題