ios 獲取手機(jī)數(shù)據(jù)是方式有兩種框架可以實(shí)現(xiàn):ios9 之前的AddressBook.framework 和 ios9之后的Contacts.framework魔策。
AddressBook.framework 使用(送授權(quán)申請, 需要手動授權(quán)。需要引用 <AddressBook/AddressBook.h>)
NSMutableArray *array = [NSMutableArray array];
if ( ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusAuthorized) {
//2. 創(chuàng)建通訊錄
ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, NULL);
//3. 獲取所有聯(lián)系人
CFArrayRef peosons = ABAddressBookCopyArrayOfAllPeople(addressBook);
//4. 遍歷所有聯(lián)系人來獲取數(shù)據(jù)(姓名和電話)
CFIndex count = CFArrayGetCount(peosons);
for (CFIndex i = 0 ; i < count; i++) {
//5. 獲取單個聯(lián)系人
ABRecordRef person = CFArrayGetValueAtIndex(peosons, i);
//6. 獲取姓名
NSString *lastName = CFBridgingRelease(ABRecordCopyValue(person, kABPersonLastNameProperty));
NSString *firstName = CFBridgingRelease(ABRecordCopyValue(person, kABPersonFirstNameProperty));
ABMultiValueRef phones = ABRecordCopyValue(person, kABPersonPhoneProperty);
//7.1 獲取電話的count數(shù)
CFIndex phoneCount = ABMultiValueGetCount(phones);
//7.2 遍歷所有電話號碼
for (CFIndex i = 0; i < phoneCount; i++) {
NSString *label = CFBridgingRelease(ABMultiValueCopyLabelAtIndex(phones, i));
NSString *value = CFBridgingRelease(ABMultiValueCopyValueAtIndex(phones, i));
// 刪除手機(jī)號碼里除了了數(shù)字之處的數(shù)據(jù)(如果-)
NSCharacterSet *setToRemove = [[ NSCharacterSet characterSetWithCharactersInString:@"0123456789"]
invertedSet ];
NSString *strPhone = [[value componentsSeparatedByCharactersInSet:setToRemove] componentsJoinedByString:@""];
model.phoneNumber = strPhone;
// 打印標(biāo)簽和電話號
NSLog(@"label: %@, value: %@",label, value);
}
NSLog(@"\\n\\n");
//8.1 釋放 CF 對象
CFRelease(phones);
[array addObject:model];
}
//8.1 釋放 CF 對象
CFRelease(peosons);
CFRelease(addressBook);
Contacts.framework 使用:(系統(tǒng)會自動發(fā)送授權(quán)申請, 不需要手動授權(quán)。 需要引用Contacts.framework和ContactsUI.framework)
if ([CNContactStore authorizationStatusForEntityType:CNEntityTypeContacts] == CNAuthorizationStatusNotDetermined) {
CNContactStore *store = [[CNContactStore alloc] init];
[store requestAccessForEntityType:CNEntityTypeContacts completionHandler:^(BOOL granted, NSError * _Nullable error) {
if (granted) {
NSLog(@"授權(quán)成功");
// 2. 獲取聯(lián)系人倉庫
CNContactStore * store = [[CNContactStore alloc] init];
// 3. 創(chuàng)建聯(lián)系人信息的請求對象
NSArray * keys = @[CNContactGivenNameKey, CNContactFamilyNameKey, CNContactPhoneNumbersKey];
// 4. 根據(jù)請求Key, 創(chuàng)建請求對象
CNContactFetchRequest * request = [[CNContactFetchRequest alloc] initWithKeysToFetch:keys];
// 5. 發(fā)送請求
[store enumerateContactsWithFetchRequest:request error:nil usingBlock:^(CNContact * _Nonnull contact, BOOL * _Nonnull stop) {
// 6.1 獲取姓名
NSString * givenName = contact.givenName;
NSString * familyName = contact.familyName;
NSLog(@"%@--%@", givenName, familyName);
// 6.2 獲取電話
NSArray * phoneArray = contact.phoneNumbers;
for (CNLabeledValue * labelValue in phoneArray) {
CNPhoneNumber * number = labelValue.value;
NSLog(@"%@--%@", number.stringValue, labelValue.label);
}
}];
}