Get device Address Book information and device address book

Source: Internet
Author: User

Get device Address Book information and device address book

Directly run the Code:

# 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: @ "button" 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];

}

 

// If this method is used to select a contact, the contact details page will no longer appear. If you need to see the contact details page, you can only disable this method.

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

NSLog (@ "Call -----------------------------------------" when selecting a contact ---------------------------------");

[Self printContactInfo: contact];

}

 

// Select multiple contacts at the same time

-(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;

}

}

 

// Note: if this method is implemented, the method above cannot be implemented. The two methods can only implement one

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

 

// NSLog (@ "called when an attribute of a contact is selected ");

//}

 

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

CNAuthorizationStatus authorizationStatus = [CNContactStore authorizationStatusForEntityType: CNEntityTypeContacts];

If (authorizationStatus = CNAuthorizationStatusAuthorized ){

NSLog (@ "no authorization ...");

}

// Obtain the specified field. You must specify a specific field instead of all fields.

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; // stop the loop, equivalent to break;

}];

}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.