iOS通過訪問系統通訊錄,獲取選擇用戶的全名和電話
#import "ViewController.h"import <AddressBookUI/AddressBookUI.h>
@interface ViewController ()<ABPeoplePickerNavigationControllerDelegate>
@property (weak, nonatomic) IBOutlet UILabel *nameLabel;
@end
@implementation ViewController
(void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
pragma mark 點擊獲取指定練習人的 姓名/電話
(IBAction)btnclick:(UIButton *)sender {
//1. 創建聯系人選擇導航控制器
ABPeoplePickerNavigationController *picker = [ABPeoplePickerNavigationController new];
//2. 設置代理 注意不要設置錯了
picker.peoplePickerDelegate = self;
//3. 以模態視圖彈出
[self presentViewController:picker animated:YES completion:nil];
}
pragma mark 實現代理方法
/// 當選擇了聯系人的時候調用此方法
///
/// @param person 選中的聯系人
- (void)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker didSelectPerson:(ABRecordRef)person
{
//獲取聯系人姓名
//NSString *firstName = CFBridgingRelease(ABRecordCopyValue(person, kABPersonFirstNameProperty));
// NSString *lastName = CFBridgingRelease(ABRecordCopyValue(person, kABPersonLastNameProperty));
//self.nameLabel.text = [lastName stringByAppendingString:firstName];
NSString *fullName = CFBridgingRelease(ABRecordCopyCompositeName(person));
self.nameLabel.text = fullName;
//獲取聯系人電話
ABMultiValueRef phones = ABRecordCopyValue(person, kABPersonPhoneProperty);
//遍歷phones, 拿到每一個電話
CFIndex count = ABMultiValueGetCount(phones);
for (int i = 0; i < count; i++) {
// 電話label
NSString *label = CFBridgingRelease(ABMultiValueCopyLabelAtIndex(phones, i));
// 電話
NSString *value = CFBridgingRelease(ABMultiValueCopyValueAtIndex(phones, i));
NSLog(@"label:%@, value: %@",label, value);
}
//釋放CF對象.如果沒有紀錄電話,phone是nil,不能釋放。
if (phones != nil) {
CFRelease(phones);
}
}
@end
</pre>
本文由用戶 cnffe 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!