您可以使用联系人框架。希望这将为您提供联系的直接解决方案。
#import <Contacts/Contacts.h>
-(void)getContact{
CNContactStore *store = [[CNContactStore alloc] init];
[store requestAccessForEntityType:CNEntityTypeContacts completionHandler:^(BOOL granted, NSError * _Nullable error) {
// make sure the user granted us access
if (!granted) {
dispatch_async(dispatch_get_main_queue(), ^{
// user didn't grant access;
// so, again, tell user here why app needs permissions in order to do it's job;
// this is dispatched to the main queue because this request could be running on background thread
});
return;
}
// build array of contacts
NSMutableArray *contacts = [NSMutableArray array];
NSError *fetchError;
CNContactFetchRequest *request = [[CNContactFetchRequest alloc] initWithKeysToFetch:@[CNContactEmailAddressesKey, CNContactPhoneNumbersKey, CNContactFamilyNameKey, CNContactGivenNameKey, CNContactPostalAddressesKey]];
BOOL success = [store enumerateContactsWithFetchRequest:request error:&fetchError usingBlock:^(CNContact *contact, BOOL *stop) {
[contacts addObject:contact];
}];
if (!success) {
CTLog(@"error = %@", fetchError);
}
__weak typeof(self)weakSelf = self;
[weakSelf parseContactWithContact:contacts];
}];
- (void)parseContactWithContact :(NSMutableArray* )contacts{
for (CNContact *contact in contacts)
{
NSString * firstName = contact.givenName;
NSString * lastName = contact.familyName;
NSString *StrContactName = [NSString stringWithFormat:@"%@ %@",firstName,lastName];
NSArray * arryPhone = [[contact.phoneNumbers valueForKey:@"value"] valueForKey:@"digits"];
for (int i=0; i<[arryPhone count]; i++)
{
NSMutableDictionary *mDictValue = [[NSMutableDictionary alloc]init];
[mDictValue setValue:firstName forKey:@"firstName"];
[mDictValue setValue:lastName forKey:@"lastName"];
[mDictValue setValue:StrContactName forKey:@"contactName"];
[mDictValue setValue:[arryPhone objectAtIndex:i] forKey:@"Phone"];
[arrayOfContacts addObject:mDictValue];
}
}