내 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'을 방출하지 않는 ... –
환영 친구를 도왔다. 정적 분석기를 통해이를 실행하면 누설이 지적됩니다. '(NSArray *)'와'(NSString *)'의 캐스트를'CFBridgingRelease'로 바꾸면 ARC가 이것을 처리합니다. (또는 OP와 마찬가지로'__bridging_transfer'를 사용하면 똑같은 일을 할 수 있습니다.) 말할 필요도없이 iOS 9에서는'Contacts' 프레임 워크가이 문제를 완전히 없애줍니다. –
을 – Rob