SoFunction
Updated on 2025-04-10

How to get mobile phone address book on iOS (Latest)

I recently learned how to get mobile address book on iOS, and I will share it with you now. Hope this article will be helpful to you.

1. Contact book framework before iOS 9

AddressBookUI framework: provides a contact list interface, contact details interface, add contact interface, etc., which are generally used to select contacts.

AddressBook framework: a pure C language API, just obtaining contact data. There is no UI interface display provided, you need to build your own contact display interface.

2. The latest address book framework after iOS 9

ContactsUI framework: has all the functions of the AddressBookUI framework, making it more object-oriented to use.

Contacts framework: With all the functions of the AddressBook framework, it is no longer an API in C language, and it is very simple to use.

This time, I will mainly talk about how to obtain mobile phone address book after iOS9:

Required Frame

#import <ContactsUI/>

Follow the agent

<CNContactPickerDelegate>

1. Request authorization judgment

// Determine the current authorization statusif (status != CNAuthorizationStatusAuthorized) {

 UIAlertView * alart = [[UIAlertView alloc]initWithTitle:@"Kind tips" message:@"Please set the APP to access your address book\nSettings-Privacy-Department Book" delegate:self cancelButtonTitle:@"Sure" otherButtonTitles:nil, nil];
 return;
}

Let's talk about the authorization status, which is similar to the AddressBook's

typedef NS_ENUM(NSInteger, CNAuthorizationStatus)
{
/*! The user has not yet made a choice on whether the application can access contact data.  */
CNAuthorizationStatusNotDetermined = 0,

/*! The app does not have permission to access contact data.
  *The user cannot change the status of this application, possibly due to active restrictions (such as parental controls are in place).  */
CNAuthorizationStatusRestricted,

/*! User explicitly denies access to the application's contact data.  */
CNAuthorizationStatusDenied,

/*! The application is authorized to access contact data.  */
CNAuthorizationStatusAuthorized
}

judge

// Determine whether the current authorization status is a state that the user has not selected yetif (status == CNAuthorizationStatusNotDetermined)
{
CNContactStore *store = [CNContactStore new];
[store requestAccessForEntityType:CNEntityTypeContacts completionHandler:^(BOOL granted, NSError * _Nullable error) {
  if (granted){
    NSLog(@"The authorization was successful!");
  }else{
    NSLog(@"Authorization failed!");
  }
}];
}

2. Create a address book controller

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
//iOS 10
//  AB_DEPRECATED("Use CNContactPickerViewController from  instead")
CNContactPickerViewController * contactVc = [CNContactPickerViewController new];
 = self;
[self presentViewController:contactVc animated:YES completion:nil];
}

If the old ABPeoplePickerNavigationController is called on an iOS10 machine, the old method will crash directly. So if you still use the previous method, you need to add a judgment version to judge

3. Implement the proxy method to obtain single-person information (1. Click on the name to display details 2. Do not display details)

// Called when selecting a contact- (void)contactPicker:(CNContactPickerViewController *)picker didSelectContactProperty:(CNContactProperty *)contactProperty
{
CNContact *contact = ;
NSString *name = [CNContactFormatter stringFromContact:contact style:CNContactFormatterStyleFullName];
CNPhoneNumber *phoneValue= ;
NSString *phoneNumber = ;
NSLog(@"%@--%@",name, phoneNumber);
}

Agent method description

// 1. Use when selecting a contact (do not expand details)- (void)contactPicker:(CNContactPickerViewController   *)picker didSelectContact:(CNContact *)contact;

Note:If there is the above method,The following method is not executed

// 2. Called when selecting a certain attribute of the contact (expand details)- (void)contactPicker:(CNContactPickerViewController *)picker didSelectContactProperty:(CNContactProperty *)contactProperty;

// 3. Called when unchecking the contact- (void)contactPickerDidCancel:(CNContactPickerViewController *)picker;

4. Deselected callback

- (void)contactPickerDidCancel:(CNContactPickerViewController *)picker{
[picker dismissViewControllerAnimated:YES completion:nil];
}

From this process, you can get information about a person in the address book. Let’s talk about the overall address book method for obtaining the mobile phone (get all contact information)

5. Obtain all address book information

  // Create a address book object  CNContactStore *contactStore = [CNContactStore new];
  NSArray *keys = @[CNContactPhoneNumbersKey,CNContactGivenNameKey];
  // Get all contacts in the address book  CNContactFetchRequest *request = [[CNContactFetchRequest alloc] initWithKeysToFetch:keys];

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

  // Get the name  //  NSString *firstName = ;
  NSString *lastName = ;

  NSLog(@"name: %@",lastName);

  // Get the phone number
  for (CNLabeledValue *labeledValue in ){

    CNPhoneNumber *phoneValue = ;
    NSString *phoneNumber = ;
    NSLog(@"number: %@",phoneNumber);
  }
    }];

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.