我在我的IOS應用程序中使用Addressbook
來獲取聯繫人姓名和號碼,但發生了一些奇怪的事情,它顯示了一些聯繫人的電話號碼。IOS地址簿沒有從聯繫人列表中獲取所有電話號碼
我希望它會顯示與電話號碼的所有聯繫人列表。
所以這裏是我的代碼獲取電話號碼:
-(void)getAddressbookData
{
#if __IPHONE_OS_VERSION_MIN_REQUIRED < 60000
ABAddressBookRef addressBook = ABAddressBookCreate();
#else
CFErrorRef *error = nil;
ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, error);
#endif
NSArray * people;
BOOL accessGranted = [self __addressBookAccessStatus:addressBook];
if (accessGranted)
{
people = (__bridge_transfer NSArray*)ABAddressBookCopyArrayOfAllPeople(addressBook);
// Do whatever you need with thePeople...
}
CFIndex nPeople = ABAddressBookGetPersonCount(addressBook);
NSMutableArray *contactArray = [[NSMutableArray alloc] init];
for (CFIndex i = 0; i < nPeople; i++)
{
ABRecordRef record = CFArrayGetValueAtIndex((__bridge CFArrayRef)(people), i);
NSString *firstName = (__bridge NSString *)ABRecordCopyValue(record, kABPersonFirstNameProperty);
NSString *lastName = (__bridge NSString *)ABRecordCopyValue(record, kABPersonLastNameProperty);
NSString *fullName = nil;
if (ABPersonGetCompositeNameFormat() == kABPersonCompositeNameFormatFirstNameFirst)
fullName = [NSString stringWithFormat:@"%@ %@", firstName, lastName];
else
fullName = [NSString stringWithFormat:@"%@, %@", lastName, firstName];
[contactArray addObject:fullName];
//
// Phone Numbers
//
ABMutableMultiValueRef phoneNumbers = ABRecordCopyValue(record, kABPersonPhoneProperty);
CFIndex phoneNumberCount = ABMultiValueGetCount(phoneNumbers);
NSMutableArray *numbersArray = [[NSMutableArray alloc] init];
for (CFIndex k=0; k<phoneNumberCount; k++)
{
CFStringRef phoneNumberLabel = ABMultiValueCopyLabelAtIndex(phoneNumbers, k);
CFStringRef phoneNumberValue = ABMultiValueCopyValueAtIndex(phoneNumbers, k);
CFStringRef phoneNumberLocalizedLabel = ABAddressBookCopyLocalizedLabel(phoneNumberLabel);
// converts "_$!<Work>!$_" to "work" and "_$!<Mobile>!$_" to "mobile"
// Find the ones you want here
//
// NSLog(@"-----PHONE ENTRY -> name:%@ : %@ : %@", fullName, phoneNumberLocalizedLabel, phoneNumberValue);
[numbersArray addObject:CFBridgingRelease(phoneNumberValue)];
CFRelease(phoneNumberLocalizedLabel);
CFRelease(phoneNumberLabel);
CFRelease(phoneNumberValue);
}
// NSLog(@"phone numbers %@", numbersArray);
[contactDictionary setObject:numbersArray forKey:fullName];
CFRelease(record);
}
selectContacts = contactArray;
// NSLog(@"dictionary of array %@", contactDictionary);
//NSLog(@"contacts count %d", [selectContacts count]);
}
-(BOOL)__addressBookAccessStatus:(ABAddressBookRef) addressBook
{
__block BOOL accessGranted = NO;
if (ABAddressBookRequestAccessWithCompletion != NULL) { // we're on iOS 6
dispatch_semaphore_t sema = dispatch_semaphore_create(0);
ABAddressBookRequestAccessWithCompletion(addressBook, ^(bool granted, CFErrorRef error) {
accessGranted = granted;
dispatch_semaphore_signal(sema);
});
dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);
// dispatch_release(sema);
}
else { // we're on iOS 5 or older
accessGranted = YES;
}
return accessGranted;
}
所以numbersArray是空白的一些我不知道爲什麼它正在發生的接觸。
感謝這有助於 –
歡迎哥們...... –
這是很好的,你是釋放'phone',但你不釋放'multi'或'people'或'addressBook'。通過靜態分析器運行它,它會指出泄漏。如果你用'CFBridgingRelease'替換'(NSArray *)'和'(NSString *)'的轉換,那麼ARC將處理這個。 (或者如果你使用'__bridging_transfer',就像OP一樣,也可以完成同樣的事情。)不用說,在iOS 9中,'Contacts'框架完全消除了這個問題,但顯然這個問題早於該框架。 – Rob