我的应用程序使用通讯簿框架。要在 iOS 6 或更高版本中显示所有联系人,我必须为其设置权限。
第一次在模拟器上运行我的应用程序时,我在下面看到了这个警报,但是当我再次尝试显示它时,我再也看不到警报了。
我已重置内容和设置,但对我没有帮助。因此,当我重新运行应用程序时,它始终显示联系人,但起初应用程序必须在我重置模拟器时显示警报。有人遇到过这个问题吗?
下面的代码:
- (void)getPersonOutOfAddressBook
{
if (self.tableData) {
[self.tableData removeAllObjects];
}
ABAddressBookRef addressBook = ABAddressBookCreate();
__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;
}
if (accessGranted)
{
if (addressBook != nil)
{
NSLog(@"Succesful.");
NSArray *allContacts = (__bridge_transfer NSArray
*)ABAddressBookCopyArrayOfAllPeople(addressBook);
NSUInteger i = 0;
for (i = 0; i < [allContacts count]; i++)
{
Contact *contact = [[Contact alloc] init];
ABRecordRef contactPerson = (__bridge ABRecordRef)allContacts[i];
NSString *firstName = (__bridge_transfer NSString
*)ABRecordCopyValue(contactPerson, kABPersonFirstNameProperty);
NSString *lastName = (__bridge_transfer NSString
*)ABRecordCopyValue(contactPerson, kABPersonLastNameProperty);
NSString *fullName = [NSString stringWithFormat:@"%@ %@",
firstName, lastName];
NSData *imgData = (__bridge NSData *)ABPersonCopyImageData(contactPerson);
contact.firstName = firstName;
contact.lastName = lastName;
contact.fullName = fullName;
contact.image = [UIImage imageWithData:imgData];
[self.tableData addObject:contact];
}
CFRelease(addressBook);
}
}
}