IOS 獲取系統通信錄中的聯繫人信息

- (IBAction)getAllContactFromSystem {
ABAddressBookRef ab = ABAddressBookCreateWithOptions(NULL, NULL);
ABAddressBookRequestAccessWithCompletion(ab, ^(bool granted, CFErrorRef error) {

//取得通信錄訪問受權
ABAuthorizationStatus authorization= ABAddressBookGetAuthorizationStatus();
if (authorization!=kABAuthorizationStatusAuthorized) {
NSLog(@"還沒有得到通信錄訪問受權!");
return ;
}

//取得通信錄中全部人員記錄
CFArrayRef allPeople= ABAddressBookCopyArrayOfAllPeople(ab);
for (int i=0; i<CFArrayGetCount(allPeople); ++i) {
ABRecordRef recordRef = CFArrayGetValueAtIndex(allPeople, i);
//獲取用戶名
NSString *firstName = (__bridge NSString *) ABRecordCopyValue(recordRef, kABPersonFirstNameProperty);
NSString *lastName = (__bridge NSString *)ABRecordCopyValue(recordRef, kABPersonLastNameProperty);
NSString *personName = [NSString stringWithFormat:@"%@%@",lastName,firstName];
//獲取手機號
NSMutableArray *phoneNumbers = [NSMutableArray new];
ABMultiValueRef phoneNumbersRef = ABRecordCopyValue(recordRef, kABPersonPhoneProperty);
for(int j=0; j<ABMultiValueGetCount(phoneNumbersRef); ++j){
NSString* phoneNumber = (__bridge NSString *)(ABMultiValueCopyValueAtIndex(phoneNumbersRef, j));
phoneNumber = [phoneNumber stringByReplacingOccurrencesOfString:@"-" withString:@""];
if (phoneNumber.length > 0) {
[phoneNumbers addObject:phoneNumber];
}
}
}

//釋放資源
CFRelease(allPeople);
});
}orm

相關文章
相關標籤/搜索