我们正在使用CNContactStore从iphone获取联系人,但是我们正在从我的iphone获取所有联系人,这也提供了所有同步联系人,如facebook和其他Gmail联系人,但我只需要获取存储在iphone中的联系人。怎么做,请帮我摆脱这个。
答案 0 :(得分:0)
导入这些
#import <AddressBook/AddressBook.h>
#import <AddressBookUI/AddressBookUI.h>
- (void) fetchContactsFromDevice {
//1
CFErrorRef error = NULL;
ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, &error);
if (addressBook != nil) {
ABAddressBookRequestAccessWithCompletion( addressBook, ^(bool granted, CFErrorRef error) {
if (error) {
NSLog(@"ABAddressBookRequestAccessWithCompletion error: %@", CFBridgingRelease(error));
}
if (granted) {
// if they gave you permission, then just carry on
[self listPeopleInAddressBook:addressBook];
} else {
// however, if they didn't give you permission, handle it gracefully, for example...
dispatch_async(dispatch_get_main_queue(), ^{
// BTW, this is not on the main thread, so dispatch UI updates back to the main queue
[[[UIAlertView alloc] initWithTitle:nil message:@"" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil] show];
});
}
CFRelease(addressBook);
});
} else {
NSLog(@"Error reading Address Book : %@", error);
}
}
此方法将帮助您仅提取地址簿号码
然后用它来绘制
- (void)listPeopleInAddressBook:(ABAddressBookRef)addressBook {
NSArray *allPeople = CFBridgingRelease(ABAddressBookCopyArrayOfAllPeople(addressBook));
NSInteger numberOfPeople = [allPeople count];
for (NSInteger i = 0; i < numberOfPeople; i++) {
ABRecordRef person = (__bridge ABRecordRef)allPeople[i];
NSMutableDictionary * user = [[NSMutableDictionary alloc] init];
NSString *firstName = CFBridgingRelease(ABRecordCopyValue(person, kABPersonFirstNameProperty));
NSString *lastName = CFBridgingRelease(ABRecordCopyValue(person, kABPersonLastNameProperty));
[user setObject:firstName forKey:@"firstname"];
[user setObject:lastName forKey:@"lastname"];
ABMultiValueRef phoneNumbers = ABRecordCopyValue(person, kABPersonPhoneProperty);
CFIndex numberOfPhoneNumbers = ABMultiValueGetCount(phoneNumbers);
for (CFIndex i = 0; i < numberOfPhoneNumbers; i++) {
NSString *phoneNumber = CFBridgingRelease(ABMultiValueCopyValueAtIndex(phoneNumbers, i));
[user setObject:phoneNumber forKey:@"phoneNumbers"];
}
CFRelease(phoneNumbers);
[self.contactDataArray addObject:user];
}
[yourtable reloadData];
}
希望这会有所帮助