2017-05-25 26 views
4

設置>聯繫人>默認帳戶被選爲iCloud並保存了一個新聯繫人。然後將默認帳戶更改爲gmail。 CNContactStore不獲取新保存的聯繫人。CNContact框架工作不提取所有聯繫人(iCloud,Gmail)

CNContactStore *store = [[CNContactStore alloc] init]; 
[store requestAccessForEntityType:CNEntityTypeContacts completionHandler:^(BOOL granted, NSError * _Nullable error) { 
    if (granted == YES) 
    { 
     NSArray *keys = @[CNContactFamilyNameKey, CNContactGivenNameKey, CNContactPhoneNumbersKey, CNContactImageDataKey,CNContactEmailAddressesKey]; 
     NSArray * contactContainerArray = [store containersMatchingPredicate:nil error:nil]; 
     for(CNContainer * container in contactContainerArray) { 
      NSPredicate *predicate = [CNContact predicateForContactsInContainerWithIdentifier:container.identifier]; 
      NSError *error; 
      NSArray *cnContacts = [store unifiedContactsMatchingPredicate:predicate keysToFetch:keys error:&error]; 
} 

回答

3

如果你只是想獲得全部統一聯繫人(在表現同一人物的不同帳戶聯繫可自動連接在一起。鏈接聯繫人顯示在MacOS和iOS應用程序作爲統一的聯繫人)最好的解決辦法是低於一個

-(void)fetchContacts 
    { 
     CNContactStore *store = [[CNContactStore alloc] init];  
     [store requestAccessForEntityType:CNEntityTypeContacts completionHandler:^(BOOL granted, NSError * _Nullable error) { 
     if (granted == YES) 
     { 
     //keys with fetching properties 
     NSArray *keys = @[CNContactFamilyNameKey, CNContactGivenNameKey, CNContactPhoneNumbersKey, CNContactImageDataKey,CNContactEmailAddressesKey]; 
     NSString *containerId = store.defaultContainerIdentifier; 
     NSArray * contactContainerArray = [store containersMatchingPredicate:nil error:nil]; 
     CNContactFetchRequest * fetchRequest = [[CNContactFetchRequest alloc]initWithKeysToFetch:keys]; 
     [store enumerateContactsWithFetchRequest:fetchRequest error:nil usingBlock:^(CNContact * _Nonnull contact, BOOL * _Nonnull stop) { 

     }]; 
     } 
    } 
+1

偉大的解決方案Vaisakh。也請你讓我知道如何識別聯繫人屬於哪個「賬戶」?意味着如何識別來自iCloud或Gmail或本地通訊錄的特定聯繫人? –

+0

我已經在這裏問我的問題https://stackoverflow.com/questions/44133669/how-to-get-account-name-from-contact-framework –

+0

@MitBhatt實際問題會給你所有聯繫人的容器(用戶可能在其設備的本地帳戶或服務器帳戶中有聯繫人,這些帳戶或服務器帳戶被配置爲同步聯繫人。每個帳戶至少有一個聯繫人容器。聯繫人只能位於一個容器中。) – Vaisakh

相關問題