2015-11-05 36 views
7

我嘗試使用聯繫人框架創建並保存組。 首先,用戶授權應用程序進行聯繫人訪問。 提供了一個視圖控制器,並帶有一個+按鈕用戶顯示帶有文本框的警報視圖。使用聯繫人框架創建新組,CNErrorDomain代碼= 2

用戶鍵入他想要的組名稱,然後單擊按鈕來保存alertview(save)。

這是保存新組的代碼。組名稱是有,但它不可能仍要保存這組:

CNContactStore *contactStore = [CNContactStore new]; 

[contactStore requestAccessForEntityType:CNEntityTypeContacts completionHandler:^(BOOL granted, NSError *error){ 
if (granted) { 

    CNMutableGroup *newGroup = [CNMutableGroup new]; 
    CNSaveRequest *saveRequest = [CNSaveRequest new]; 

    [newGroup setName:groupName]; 

    //when saving to container with identifier nil, we get this error: 
    //Error Domain=CNErrorDomain Code=2 "(null)" UserInfo={CNInvalidRecords=(
    //"<CNMutableGroup: 0x10a059f20: identifier=2F4981B9-8A47-45A4-8841-1FA5A09584A4:ABGroup, name=gghh>" 
    [saveRequest addGroup:newGroup toContainerWithIdentifier:nil]; 
    [contactStore executeSaveRequest:saveRequest error:&error]; 

    if (error){ 
     //error saving group 
     //NSLog(@"error message: %@",error); 
    } else { 
     //if no errors, reload tableview 
     dispatch_async(dispatch_get_main_queue(), ^{ 
      [self.tableView reloadData]; 
     }); 
    } 
} 
}]; 



Error Domain=CNErrorDomain Code=2 "(null)" UserInfo={CNInvalidRecords=(
    "<CNMutableGroup: 0x14fb3e5e0: identifier=8E490585-1223-407E-B353-0D25609B05AB:ABGroup, name=jddjd>" 
)} 

下一個奇怪的是:爲什麼是保存請求試圖挽救這組
標識符爲:ABGroup末?

該錯誤包含有關CNInvalidRecords的信息。
我只使用聯繫人框架。
這是怎麼發生的?

任何解決方案?

回答

2

它工作正常,對我來說,基本上是相同的代碼。

CNMutableGroup *newGroup = [CNMutableGroup new]; 
CNSaveRequest *saveRequest = [CNSaveRequest new]; 
[newGroup setName:self.groupName]; 
[saveRequest addGroup:newGroup toContainerWithIdentifier:nil]; 
[contactStore executeSaveRequest:saveRequest error:&error]; 

並創建一個新組

+1

這造成如果一個新的羣體,提供的iCloud設置爲默認設置>郵件,日曆,聯繫人。如果默認爲Exchange,則無法創建和保存組。這就是爲什麼我得到這個錯誤。我正在尋找解決此問題的解決方法。 – brush51

+0

根據Apple的文檔,您無法在Exchange容器中創建組,您需要爲每個「組」創建一個單獨的容器。 –

+0

是的,這是正確的,但我想這將不可能創建另一個獨立的容器來創建一個新的組,並在本地保存當iCloud同步激活。 – brush51

相關問題