2016-08-16 16 views
-3

我需要獲取來自設備的所有聯繫人,並希望單獨顯示相應..kindly來看看我的代碼感謝如何獲取設備的聯繫人以及如何單獨顯示

// using method to fetch contacts from device 
-(void)fetchContacts{ 
CNAuthorizationStatus status = [CNContactStore authorizationStatusForEntityType:CNEntityTypeContacts]; 
    if(status == CNAuthorizationStatusDenied || status == CNAuthorizationStatusRestricted){ 
      NSLog(@"access denied"); 
    }else{ 
      CNContactStore *contactStore = [[CNContactStore alloc] init]; 
      NSArray *keys = [[NSArray alloc]initWithObjects:CNContactIdentifierKey, CNContactEmailAddressesKey, CNContactBirthdayKey, CNContactImageDataKey, CNContactPhoneNumbersKey, CNContactViewController.descriptorForRequiredKeys, nil]; 
      CNContactFetchRequest *request = [[CNContactFetchRequest alloc] initWithKeysToFetch:keys]; 
      request.predicate = nil; 
      [contactStore enumerateContactsWithFetchRequest:request error:nil usingBlock:^(CNContact* __nonnull contact, BOOL* __nonnull stop){ 
       NSString *phoneNumber = @""; 
       if(contact.phoneNumbers) 
        phoneNumber = [[[contact.phoneNumbers firstObject] value] stringValue]; 
       NSMutableDictionary *contactValue=[[NSMutableDictionary alloc] init]; 
       if ([contact.givenName isEqualToString:@""]) { 
       }else{ 
        [contactValue setValue:phoneNumber forKey:@"phoneNumber"]; 
        [contactValue setObject:contact.givenName forKey:@"userName"]; 
        [contactValue setObject:contact.familyName forKey:@"familyName"]; 
        [contactValue setObject:[contact.emailAddresses valueForKey:@"value"] ?:@"" forKey:@"emailAddress"]; 
        [contactValue setObject:contact.identifier forKey:@"phoneIdentifier"]; 

       } 
       [_totalContact addObject:contactValue]; 
      }]; 
} 
+0

請您甲酸鹽代碼。 – Sofeda

+0

和一些細節。 – Sofeda

回答

-1

您可以使用聯繫人框架,該框架。

CNContactStore *cnContactStore = [[CNContactStore alloc] init]; 



    [cnContactStore requestAccessForEntityType:CNEntityTypeContacts completionHandler:^(BOOL granted, NSError * _Nullable error) { 



     if (granted == YES) { 



      NSArray *keys = @[CNContactFamilyNameKey, CNContactGivenNameKey, CNContactPhoneNumbersKey, CNContactImageDataKey]; 



      NSString *containerId = cnContactStore.defaultContainerIdentifier; 



      NSPredicate *predicate = [CNContact predicateForContactsInContainerWithIdentifier:containerId]; 



      NSError *error; 



      _contactDetailArray = [cnContactStore unifiedContactsMatchingPredicate:predicate keysToFetch:keys error:&error]; 



      if (error) { 



       NSLog(@」error fetching contacts %@」, error); 



      } else { 

       //Do Something 



      } 
} 

}]; 

對於完整代碼,請訪問我blog

相關問題