擷取裝置通訊錄資訊,擷取裝置通訊錄

來源:互聯網
上載者:User

擷取裝置通訊錄資訊,擷取裝置通訊錄

直接上代碼:

#import "ViewController.h"

 

#import <Contacts/Contacts.h>

#import <ContactsUI/ContactsUI.h>

 

@interface ViewController ()<CNContactPickerDelegate>

 

@property (nonatomic, strong) UIButton *btn;

@property (nonatomic, strong) UILabel *label;

@property (nonatomic, strong) UILabel *labelNum;

 

@end

 

@implementation ViewController

 

- (void)viewDidLoad {

    [super viewDidLoad];

 

    _btn = [UIButton buttonWithType:UIButtonTypeSystem];

    _btn.frame = CGRectMake(10, 20, 50, 30);

    [_btn setTitle:@"按鈕" forState:0];

    [_btn addTarget:self action:@selector(btnAction) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:_btn];

    _label = [[UILabel alloc] initWithFrame:CGRectMake(10, 60, 50, 30)];

    [self.view addSubview:_label];

    _labelNum = [[UILabel alloc] initWithFrame:CGRectMake(10, 120, 50, 30)];

    [self.view addSubview:_labelNum];

}

 

- (void)btnAction {

    

    CNContactPickerViewController *contactPickerViewController = [[CNContactPickerViewController alloc] init];

    contactPickerViewController.delegate = self;

    

    [self presentViewController:contactPickerViewController animated:YES completion:nil];

}

 

// 如果實現該方法當選中連絡人時就不會再出現連絡人詳情介面, 如果需要看到連絡人詳情介面只能不實現這個方法

- (void)contactPicker:(CNContactPickerViewController *)picker didSelectContact:(CNContact *)contact {

    NSLog(@"選中某一個連絡人時調用---------------------------------");

    

    [self printContactInfo:contact];

}

 

// 同時選中多個連絡人

- (void)contactPicker:(CNContactPickerViewController *)picker didSelectContacts:(NSArray<CNContact *> *)contacts {

    for (CNContact *contact in contacts) {

        NSLog(@"================================================");

        [self printContactInfo:contact];

    }

}

 

- (void)printContactInfo:(CNContact *)contact {

    

    NSString *givenName = contact.givenName;

    NSString *familyName = contact.familyName;

    NSLog(@"givenName=%@, familyName=%@", givenName, familyName);

    NSArray * phoneNumbers = contact.phoneNumbers;

    _label.text = familyName;

    

    for (CNLabeledValue<CNPhoneNumber*>*phone in phoneNumbers) {

        NSString *label = phone.label;

        CNPhoneNumber *phonNumber = (CNPhoneNumber *)phone.value;

        NSLog(@"label=%@, value=%@", label, phonNumber.stringValue);

        _labelNum.text = phonNumber.stringValue;

    }

}

 

// 注意:如果實現該方法,上面那個方法就不能實現了,這兩個方法只能實現一個

// - (void)contactPicker:(CNContactPickerViewController *)picker didSelectContactProperty:(CNContactProperty *)contactProperty {

 

//  NSLog(@"選中某個連絡人的某個屬性時調用");

// }

 

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {

    CNAuthorizationStatus authorizationStatus = [CNContactStore authorizationStatusForEntityType:CNEntityTypeContacts];

    if (authorizationStatus == CNAuthorizationStatusAuthorized) {

        

        NSLog(@"沒有授權...");

}

    

    // 擷取指定的欄位,並不是要擷取所有欄位,需要指定具體的欄位

    NSArray *keysToFetch = @[CNContactGivenNameKey, CNContactFamilyNameKey, CNContactPhoneNumbersKey];

    CNContactFetchRequest *fetchRequest = [[CNContactFetchRequest alloc] initWithKeysToFetch:keysToFetch];

    CNContactStore *contactStore = [[CNContactStore alloc] init];

    [contactStore enumerateContactsWithFetchRequest:fetchRequest error:nil usingBlock:^(CNContact * _Nonnull contact, BOOL * _Nonnull stop) {

        NSLog(@"-------------------------------------------------------");

        NSString *givenName = contact.givenName;

        NSString *familyName = contact.familyName;

        NSLog(@"givenName=%@, familyName=%@", givenName, familyName);

        

        

        NSArray *phoneNumbers = contact.phoneNumbers;

        for (CNLabeledValue *labelValue in phoneNumbers) {

            

            NSString *label = labelValue.label;

            CNPhoneNumber *phoneNumber = labelValue.value;

            

            NSLog(@"label=%@, phone=%@", label, phoneNumber.stringValue);

        }

        

        // *stop = YES; // 停止迴圈,相當於break;

    }];

}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.