addressBook = ABAddressBookCreate();
//注册通讯录更新回调
ABAddressBookRegisterExternalChangeCallback(addressBook, addressBookChanged, (__bridge void *)(self));
// iOS 6
-(bool)checkAddressBookAuthorizationStatus:(UITableView*)tableView;
{
//取得授权状态
ABAuthorizationStatus authStatus =
ABAddressBookGetAuthorizationStatus();
if (authStatus != kABAuthorizationStatusAuthorized)
{
ABAddressBookRequestAccessWithCompletion
(addressBook, ^(bool granted, CFErrorRef error)
{
dispatch_async(dispatch_get_main_queue(), ^{
if (error)
NSLog(@"Error: %@", (__bridge NSError *)error);
else if (!granted) {
UIAlertView *av = [[UIAlertView alloc]
initWithTitle:@"Authorization Denied"
message:@"Set permissions in Settings>General>Privacy."
delegate:nil
cancelButtonTitle:nil
otherButtonTitles:@"OK", nil];
[av show];
}
else
{
//还原 ABAddressBookRef
ABAddressBookRevert(addressBook);
myContacts = [NSArray arrayWithArray:
(__bridge_transfer NSArray*)
ABAddressBookCopyArrayOfAllPeople(addressBook)];
[tableView reloadData];
}
});
});
}
return authStatus == kABAuthorizationStatusAuthorized;
2、如果通讯录有更新,可能及时调用回调方法,请看上面第一段代码 ABAddressBookRegisterExternalChangeCallback 方法
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
if (nil == myContacts)
{
addressBook = ABAddressBookCreate();
//注册通讯录更新回调
ABAddressBookRegisterExternalChangeCallback(addressBook, addressBookChanged, (__bridge void *)(self));
if ([self checkAddressBookAuthorizationStatus:tableView])
myContacts = [NSArray arrayWithArray:(__bridge_transfer NSArray*)
ABAddressBookCopyArrayOfAllPeople(addressBook)];
}
return 1;
}
void addressBookChanged(ABAddressBookRef addressBook, CFDictionaryRef info, void* context)
{
NSLog(@"Address Book Changed");
//__bridge arc显式转换。 与__unsafe_unretained 关键字一样 只是引用。被代入对象的所有者需要明确对象生命周期的管理,不要出现异常访问的问题
//__bridge_retained 类型被转换时,其对象的所有权也将被变换后变量所持有
//__bridge_transfer 本来拥有对象所有权的变量,在类型转换后,让其释放原先所有权 就相当于__bridge_retained后,原对像执行了release操作
RootViewController *viewController = objc_unretainedObject(context);
//更新通讯录
[viewController updateAddressBook];
//注销通讯录更新回调
// ABAddressBookUnregisterExternalChangeCallback(addressBook, addressBookChanged, context);
}
3、拿出数组中的联系人数据
NSString *firstName = (__bridge_transfer NSString *)ABRecordCopyValue(person, kABPersonFirstNameProperty);
NSString *lastName = (__bridge_transfer NSString *)ABRecordCopyValue(person, kABPersonLastNameProperty);
//用户头像
NSData *d = (__bridge_transfer NSData*)ABPersonCopyImageData(objc_unretainedPointer([myContacts objectAtIndex:indexPath.row]));
//取电话数据
-(void)handleRowSelection:(int)rowIndex
{
ABRecordRef person = objc_unretainedPointer([myContacts objectAtIndex:rowIndex]);
ABMultiValueRef phoneNumbers = ABRecordCopyValue(person,
kABPersonPhoneProperty);
if (ABMultiValueGetCount(phoneNumbers) == 1)
[self callThisNumber:(__bridge_transfer NSString*)ABMultiValueCopyValueAtIndex(phoneNumbers, 0)];
else if (ABMultiValueGetCount(phoneNumbers) > 1)
{
UIAlertView *av = [[UIAlertView alloc] initWithTitle:@"Pick A Number"
message:@"Which number would you like to call?"
delegate:self
cancelButtonTitle:@"Cancel"
otherButtonTitles:nil];
for (int i=0; i < ABMultiValueGetCount(phoneNumbers); i++)
[av addButtonWithTitle:(__bridge_transfer NSString*)ABMultiValueCopyValueAtIndex(phoneNumbers, i)];
[av show];
}
if (phoneNumbers)
CFRelease(phoneNumbers);
}
4、删除通讯录数据
ABRecordRef person = objc_unretainedPointer([myContacts objectAtIndex:indexPath.row]);CFErrorRef *error;ABAddressBookRemoveRecord(addressBook, person, error);ABAddressBookSave(addressBook, error);myContacts = nil; [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
网友评论