ios6上读取通讯录的方法

ios升级到6.0以后的变化之一就是增加了通讯录的隐私设置,防止程序未通过许可就访问用户的通讯录。因此在app开发中,读取通讯录的方法也有了一些变化。上代码:

-(BOOL)isABAddressBookCreateWithOptionsAvailable {
return &ABAddressBookCreateWithOptions != NULL;
}

-(void)loadContacts {
ABAddressBookRef addressBook;
if ([self isABAddressBookCreateWithOptionsAvailable]) {

// ios6以上读取方法
CFErrorRef error = nil;
addressBook = ABAddressBookCreateWithOptions(NULL,&error);
ABAddressBookRequestAccessWithCompletion(addressBook, ^(bool granted, CFErrorRef error) {
// callback can occur in background, address book must be accessed on thread it was created on
dispatch_async(dispatch_get_main_queue(), ^{
if (error) {

} else if (!granted) {

} else {
// access granted
AddressBookUpdated(addressBook, nil, (__bridge void *)(self));
CFRelease(addressBook);
}
});
});
} else {
// iOS 4/5
addressBook = ABAddressBookCreate();
AddressBookUpdated(addressBook, NULL, (__bridge void *)(self));
CFRelease(addressBook);
}
}

void AddressBookUpdated(ABAddressBookRef addressBook, CFDictionaryRef info, void *context) {
ABAddressBookRevert(addressBook);
CFArrayRef people = ABAddressBookCopyArrayOfAllPeople(addressBook);
};

 

本文转自:http://bluevt.org/?p=41

发表评论

电子邮件地址不会被公开。