經過訪問系統通信錄,獲取選擇用戶的全名和電話

#import "ViewController.h"atom

#import <AddressBookUI/AddressBookUI.h>spa


@interface ViewController ()<ABPeoplePickerNavigationControllerDelegate>.net

@property (weak, nonatomic) IBOutlet UILabel *nameLabel;3d


@end 代理


@implementation ViewControllerorm


- (void)viewDidLoad {對象

    [super viewDidLoad];get

    // Do any additional setup after loading the view, typically from a nib.string

}it


#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對象.若是沒有紀錄電話,phonenil,不能釋放。

    if (phones != nil) {

         CFRelease(phones);

    }

    

}


@end

相關文章
相關標籤/搜索