我使用以下代码在iOS 7.1中获取联系人
CFErrorRef *error=nil;
ABAddressBookRef addressbook=ABAddressBookCreateWithOptions(NULL, error);
CFArrayRef allpeople=ABAddressBookCopyArrayOfAllPeople(addressbook);
CFIndex npeople=ABAddressBookGetPersonCount( addressbook );
ABAddressBookRef addressBookRef = ABAddressBookCreateWithOptions(NULL, NULL);
if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusNotDetermined) {
ABAddressBookRequestAccessWithCompletion(addressBookRef, ^(bool granted, CFErrorRef error) {
if (granted) {
NSLog(@"access granteddsddsdd");
for ( int i = 0; i < npeople; i++ )
{
NSLog(@"access granted");
ABRecordRef ref = CFArrayGetValueAtIndex( allpeople, i );
NSString *firstName = (__bridge NSString *)(ABRecordCopyValue(ref, kABPersonFirstNameProperty));
NSString *lastName = (__bridge NSString *)(ABRecordCopyValue(ref, kABPersonLastNameProperty));
NSLog(@"Name:%@ %@", firstName, lastName); }
// First time access has been granted, add the contact
} else {
// User denied access
// Display an alert telling user the contact could not be added
}
});
}
else if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusAuthorized) {
NSLog(@"access granted1");
for ( int i = 0; i < npeople; i++ )
{
ABRecordRef ref = CFArrayGetValueAtIndex( allpeople, i );
NSString *firstName = (__bridge NSString *)(ABRecordCopyValue(ref, kABPersonFirstNameProperty));
NSString *lastName = (__bridge NSString *)(ABRecordCopyValue(ref, kABPersonLastNameProperty));
NSLog(@"Name:%@ %@", firstName, lastName); }
// The user has previously given access, add the contact
}
else {
NSLog(@"access granted2");
for ( int i = 0; i < npeople; i++ )
{
ABRecordRef ref = CFArrayGetValueAtIndex( allpeople, i );
NSString *firstName = (__bridge NSString *)(ABRecordCopyValue(ref, kABPersonFirstNameProperty));
NSString *lastName = (__bridge NSString *)(ABRecordCopyValue(ref, kABPersonLastNameProperty));
NSLog(@"Name:%@ %@", firstName, lastName); }
// The user has previously denied access
// Send an alert telling user to change privacy setting in settings app
}
当我在我的设备中运行此代码时,第一次在点击&#34之后请求许可;确定&#34;在权限弹出窗口中,如果我关闭应用程序&amp;再次打开它令人惊讶地显示了所有的名字 所以任何人都可以告诉我第一次和第一次的问题是什么?如何修复
答案 0 :(得分:1)
我遇到了你的问题。实际上它会检查许可和同时它试图获取结果。
以下是简要的解决方案:
创建课程:
<强> VSContacts.h 强>
#import <Foundation/Foundation.h>
#import <AddressBook/AddressBook.h>
@interface VSContacts : NSObject
-(BOOL)canGetContacts;
-(NSArray *)getContacts;
-(ABAddressBookRef)addressBookRef;
@property (nonatomic) ABAddressBookRef addressBook;
@end
<强> VSContacts.m 强>
#import "VSContacts.h"
@implementation VSContacts
@synthesize addressBook = _addressBook;
-(BOOL)canGetContacts
{
self.addressBook = [self addressBookRef];
if (self.addressBook != NULL || self.addressBook != nil) {
return YES;
}
return NO;
}
#pragma mark - Get AddressBook
-(ABAddressBookRef)addressBookRef
{
ABAddressBookRef aBook = NULL;
if(ABAddressBookCreateWithOptions)
{
CFErrorRef error;
aBook = ABAddressBookCreateWithOptions(NULL, &error);
__block BOOL accessGranted = NO;
if (ABAddressBookRequestAccessWithCompletion != NULL) {
// we're on iOS 6
dispatch_semaphore_t sema = dispatch_semaphore_create(0);
ABAddressBookRequestAccessWithCompletion(aBook, ^(bool granted, CFErrorRef error) {
accessGranted = granted;
dispatch_semaphore_signal(sema);
dispatch_async(dispatch_get_main_queue(), ^{
if(granted && !error)
{
ABAddressBookRevert(aBook);
}
});
});
dispatch_semaphore_wait(sema, DISPATCH_TIME_NOW);
dispatch_release(sema);
}
else
{
// we're on iOS 5 or older
accessGranted = YES;
aBook = ABAddressBookCreate();
}
}
else
{
aBook = ABAddressBookCreate();
}
return aBook ;
}
-(NSArray *)getContacts
{
__block NSArray *contacts = nil;
ABAddressBookRef addressBook = self.addressBook;
if(addressBook != NULL || addressBook != nil)
{
CFArrayRef people = ABAddressBookCopyArrayOfAllPeople(addressBook);
contacts = (__bridge NSArray *)people;
}
return contacts;
}
@end
现在:
比:
在Appdelagate.m文件中:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
if ([contactsHandler canGetContacts])
{
NSLog(@"Access Granted");
}
else
{
NSLog(@"Access Not Granted");
}
}
完成上述操作后,请在获取联系人的页面中输入以下行:
[NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(doFetchContacts) userInfo:nil repeats:NO];
-(void)doFetchContacts
{
CFErrorRef *error=nil;
ABAddressBookRef addressbook=ABAddressBookCreateWithOptions(NULL, error);
CFArrayRef allpeople=ABAddressBookCopyArrayOfAllPeople(addressbook);
CFIndex npeople=ABAddressBookGetPersonCount( addressbook );
for ( int i = 0; i < npeople; i++ )
{
ABRecordRef ref = CFArrayGetValueAtIndex( allpeople, i );
NSString *firstName = (__bridge NSString *)(ABRecordCopyValue(ref, kABPersonFirstNameProperty));
NSString *lastName = (__bridge NSString *)(ABRecordCopyValue(ref, kABPersonLastNameProperty));
NSLog(@"Name:%@ %@", firstName, lastName);
}
}
希望这会有所帮助。
Enjoyyy:)