自己写了一个调用系统通讯录类,可以直接复制来使用!
#import <Foundation/Foundation.h> #import <AddressBook/AddressBook.h> #import <AddressBookUI/AddressBookUI.h> /*! * 操作本地通讯录API封装 */ @interface HYBContactHelper : NSObject <ABPeoplePickerNavigationControllerDelegate, ABPersonViewControllerDelegate> { @private __weak UIViewController *_targetController; HYBSuccessDictBlock _completionBlock; ABPeoplePickerNavigationController *_pickerView; } - (void)showInController:(UIViewController *)controller completion:(HYBSuccessDictBlock)completion; @end
// // HYBContactHelper.m // XiaoYaoUser // // Created by 黄仪标 on 14/12/9. // Copyright (c) 2014年 xiaoyaor. All rights reserved. // #import "HYBContactHelper.h" @implementation HYBContactHelper - (void)showInController:(UIViewController *)controller completion:(HYBSuccessDictBlock)completion { _completionBlock = [completion copy]; _targetController = controller; _pickerView = [[ABPeoplePickerNavigationController alloc] init]; _pickerView.peoplePickerDelegate = self; [_targetController presentViewController:_pickerView animated:YES completion:nil]; } #pragma mark - ABPeoplePickerNavigationControllerDelegate #if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_8_0 // Called after a person has been selected by the user. - (void)peoplePickerNavigationController:(ABPeoplePickerNavigationController*)peoplePicker didSelectPerson:(ABRecordRef)person { ABMutableMultiValueRef phoneMulti = ABRecordCopyValue(person, kABPersonPhoneProperty); NSString *firstName = (__bridge NSString *)ABRecordCopyValue(person, kABPersonFirstNameProperty); if (firstName==nil) { firstName = @" "; } NSString *lastName=(__bridge NSString *)ABRecordCopyValue(person, kABPersonLastNameProperty); if (lastName==nil) { lastName = @" "; } NSMutableArray *phones = [NSMutableArray arrayWithCapacity:0]; for (int i = 0; i < ABMultiValueGetCount(phoneMulti); i++) { NSString *aPhone = (__bridge NSString *)ABMultiValueCopyValueAtIndex(phoneMulti, i); [phones addObject:aPhone]; } NSDictionary *dic = @{@"fullname": [NSString stringWithFormat:@"%@%@", firstName, lastName], @"phone" : phones.count > 0 ? [phones firstObject] : @"读取失败"}; _completionBlock(dic); [_targetController dismissViewControllerAnimated:YES completion:nil]; return; } // Called after a property has been selected by the user. - (void)peoplePickerNavigationController:(ABPeoplePickerNavigationController*)peoplePicker didSelectPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier { _completionBlock(nil); [_targetController dismissViewControllerAnimated:YES completion:nil]; return; } #else // Called after the user has pressed cancel. - (void)peoplePickerNavigationControllerDidCancel:(ABPeoplePickerNavigationController *)peoplePicker { _completionBlock(nil); [_targetController dismissViewControllerAnimated:YES completion:nil]; } // Deprecated, use predicateForSelectionOfPerson and/or -peoplePickerNavigationController:didSelectPerson: instead. - (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person { ABMutableMultiValueRef phoneMulti = ABRecordCopyValue(person, kABPersonPhoneProperty); NSString *firstName = (__bridge NSString *)ABRecordCopyValue(person, kABPersonFirstNameProperty); if (firstName==nil) { firstName = @" "; } NSString *lastName=(__bridge NSString *)ABRecordCopyValue(person, kABPersonLastNameProperty); if (lastName==nil) { lastName = @" "; } NSMutableArray *phones = [NSMutableArray arrayWithCapacity:0]; for (int i = 0; i < ABMultiValueGetCount(phoneMulti); i++) { NSString *aPhone = (__bridge NSString *)ABMultiValueCopyValueAtIndex(phoneMulti, i); [phones addObject:aPhone]; } NSDictionary *dic = @{@"firstName": firstName,@"lastName":lastName,@"phones":phones}; _completionBlock(dic); [_targetController dismissViewControllerAnimated:YES completion:nil]; return NO; } // Deprecated, use predicateForSelectionOfProperty and/or -peoplePickerNavigationController:didSelectPerson:property:identifier: instead. - (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier { _completionBlock(nil); [_targetController dismissViewControllerAnimated:YES completion:nil]; return NO; } #endif #pragma mark - ABPersonViewControllerDelegate // Called when the user selects an individual value in the Person view, identifier will be kABMultiValueInvalidIdentifier if a single value property was selected. // Return NO if you do not want anything to be done or if you are handling the actions yourself. // Return YES if you want the ABPersonViewController to perform its default action. - (BOOL)personViewController:(ABPersonViewController *)personViewController shouldPerformDefaultActionForPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier { ABMutableMultiValueRef phoneMulti = ABRecordCopyValue(person, kABPersonPhoneProperty); NSString *firstName = (__bridge NSString *)ABRecordCopyValue(person, kABPersonFirstNameProperty); if (firstName==nil) { firstName = @" "; } NSString *lastName=(__bridge NSString *)ABRecordCopyValue(person, kABPersonLastNameProperty); if (lastName==nil) { lastName = @" "; } NSMutableArray *phones = [NSMutableArray arrayWithCapacity:0]; for (int i = 0; i < ABMultiValueGetCount(phoneMulti); i++) { NSString *aPhone = (__bridge NSString *)ABMultiValueCopyValueAtIndex(phoneMulti, i); [phones addObject:aPhone]; } NSDictionary *dic = @{@"firstName": firstName,@"lastName":lastName,@"phones":phones}; _completionBlock(dic); [_targetController dismissViewControllerAnimated:YES completion:nil]; return NO; } @end