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;
}];
}