gpt4 book ai didi

ios - 如何简单地检索 iOS7 中的联系人列表?

转载 作者:塔克拉玛干 更新时间:2023-11-01 19:09:02 26 4
gpt4 key购买 nike

我需要在 iOS 中检索 Contacts 列表。

这是我的代码,但不起作用。

NSMutableArray *myContacts = [[NSMutableArray alloc]init];

CFErrorRef error = NULL;
ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, NULL);
if (addressBook!=nil)
{
NSArray *allContacts = (__bridge_transfer NSArray*)ABAddressBookCopyArrayOfAllPeople(addressBook);

NSUInteger i = 0;
for (i = 0; i<[allContacts count]; i++)
{
Person *person = [[Person alloc] init];
ABRecordRef contactPerson = (__bridge ABRecordRef)allContacts[i];
NSString *firstName = (__bridge_transfer NSString*)ABRecordCopyValue(contactPerson, kABPersonFirstNameProperty);

person.firstName = firstName;
[myContacts addObject:person];
}
CFRelease(addressBook);

}
else
{
NSLog(@"Error");

}

如何获取联系人的列表?

最佳答案

您需要先请求访问用户的地址簿。设置标志以检查用户是否允许/拒绝访问。

__block BOOL userDidGrantAddressBookAccess;
CFErrorRef addressBookError = NULL;

if ( ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusNotDetermined ||
ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusAuthorized )
{
addressBook = ABAddressBookCreateWithOptions(NULL, &addressBookError);
dispatch_semaphore_t sema = dispatch_semaphore_create(0);
ABAddressBookRequestAccessWithCompletion(addressBook, ^(bool granted, CFErrorRef error){
userDidGrantAddressBookAccess = granted;
dispatch_semaphore_signal(sema);
});
dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);
}
else
{
if ( ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusDenied ||
ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusRestricted )
{
// Display an error.
}
}

然后您可以调用您编写的方法来获取联系人。请记住首先检查 userDidGrantAddressBookAccess 的值。

关于ios - 如何简单地检索 iOS7 中的联系人列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19655156/

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