Showing posts with label ContactUI. Show all posts
Showing posts with label ContactUI. Show all posts

Sunday, 15 January 2017

Contact Picker Controller

One of the useful framework provided by Apple is "ContactsUI Framework". ContactUI Framework display user's contact list in a graphical interface.  The ContactUI Framework is useful for display, edit, select and create contact within the app. Before enter in code lets discuss about some important classes of this framework.

CNContactPickerViewController:
CNContactPickerViewController present the entire list of contact to user. This class allows to user to select one or multiple contacts. Its supports both single selection and multi-selection. The app does not need access to the user’s contacts and the user will not be prompted for access. It will only have access to the final selection of the user.

CNContactViewController:
The CNContactViewController is used to display a contact. It can display a new contact, unknown contact, or existing contact.

Now we are ready for display entire list of contacts  within the application.

Here code snippet for  display CNContactPickerViewController
//Create an instance of  CNContactPickerViewController
CNContactPickerViewController *contactController = [[CNContactPickerViewController alloc] init];
//Set the CNContactPickerDelegate
    contactController.delegate = self;
//Present picker controller
    [self presentViewController:contactController animated:YES completion:nil];

Implement the delegate method  for getting details of selected contant.
(In this example we are going to handle onle single selection of contact)

 - (void)contactPicker:(CNContactPickerViewController *)picker didSelectContact:(CNContact *)contact{
            NSLog(@"Given Name = %@",contact.givenName);
           //Get numbers
           NSArray *phoneNumbers= contact.phoneNumbers;
    for(CNLabeledValue *phoneNumber in phoneNumbers){
        if ([phoneNumber isKindOfClass:[CNLabeledValue class]]) {
            NSLog(@"CNLabeledValue - %@", ((CNLabeledValue *)phoneNumber).value);
            id valueType = ((CNLabeledValue *)phoneNumber).value;
            if ([valueType isKindOfClass:[CNPhoneNumber class]]) {
                    NSLog(@"phone number - %@",[valueType valueForKey:@"digits"]);
                    NSLog(@"Country -> %@", [valueType valueForKey:@"countryCode"]);
            }
         }
    }
    [picker dismissViewControllerAnimated:YES completion:nil];
}


// contactPickerDidCancel invoked, when user cancel picker selection
- (void)contactPickerDidCancel:(CNContactPickerViewController *)picker{
    [picker dismissViewControllerAnimated:YES completion:nil];
}


For more detail Click here