gpt4 book ai didi

iphone - IOS AddressBook 没有从联系人列表中获取所有电话号码

转载 作者:可可西里 更新时间:2023-11-01 04:30:38 24 4
gpt4 key购买 nike

我在我的 IOS 应用程序中使用 Addressbook 来获取联系人姓名和号码,但奇怪的是它显示了一些联系人的电话号码。

我希望它能显示所有联系人列表和电话号码。

下面是我获取电话号码的代码:

-(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 是空白的,我不知道为什么会这样。

最佳答案

试试这个电话号码。

    ABAddressBookRef addressBook = ABAddressBookCreate();
NSArray *people = (NSArray*)ABAddressBookCopyArrayOfAllPeople(addressBook);
for(id person in people){
//fetch multiple phone nos.
ABMultiValueRef multi = ABRecordCopyValue(person, kABPersonPhoneProperty);
for (CFIndex j=0; j < ABMultiValueGetCount(multi); j++) {
NSString* phone = (NSString*)ABMultiValueCopyValueAtIndex(multi, j);
[numbersArray addObject:phone];
[phone release];
}
}

并且您必须在使用前分配您的数组。在 viewDidLoad 方法中为分配数组写这个

           numbersArray=[[NSMutableArray alloc] init];

关于iphone - IOS AddressBook 没有从联系人列表中获取所有电话号码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16607229/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com