iOS Programming the Address Book (1)

Source: Internet
Author: User
Tags uikit

iOS Address Book technology provides a way to store user contact information and other private information in a centralized database, and to share this information among applications.

Address Book technology consists of four aspects: the Addresses Books framework provides an interface to access user information the URL for the UI framework is used to display user information about the databases store information contacts Application provides a way for users to access their contact information when you include address Book technology in your application, users will be able to use the contact information they use in other applications, such as mail and text, in your application. Follow these steps: 1, access the user Address Book database 2, prompt the user to access information 3, display contact information 4, modify the user Address Book database to maximize the understanding of Address Book technology, you should first understand navigation controller and view Controller and delegation and protocolNote: The Address Book Technology interface in Mac OS X is not the same as in iOS The first step: simply start building a simple application that prompts the user to select a person from their list of contacts, and then displays the selected person's name and phone number 1, builds a new project---------Single View Application Template2, link the Address Book UI, and frameworks into the projectNote: If there are no links, these framework projects will be compiled with an error3. Create UI and header files when you create a user interface, you use Xcode to declare the necessary actions and properties, create some header files   Open the main storyboard file (Mainstoryboard.storyboard), add a button and two tabs, such as the arrangement (in fact, with the general Xib is the same) to connect the button and the View controller events, the event name is Showpicker, and then the two tags and the corresponding properties of the view controller connected together, The property name is FirstName and phonenumber  in the header file ViewController.h, @interface the last to declare this view Controller inherits Abpeoplepickernavigationcontrollerdelegate Way is directly add <abpeoplepickernavigationcontrollerdelegate >  Code for
 #import <uikit/uikit.h> 
 #import <addressbookui/addressbookui.h> 
&NBSP;
 @interface Viewcontroller:uiviewcontroller <abpeoplepickernavigationcontrollerdelegate 
&NBSP;
 @property (weak, Nona
  Tomic) Iboutlet UILabel *firstname; 
 @property (weak, nonatomic) Iboutlet UILabel *phonenumber; 
&NBSP;
-(ibaction) Showpicker: (ID) sender;
&NBSP;
 @end 
In the. m file this is written in the Showpicker method, creating a abpeoplepickernavigationcontroller that takes the view controller as the delegate of the picker, And then take picker as modal view controller.
-(Ibaction) Showpicker: (ID) sender
{
    Abpeoplepickernavigationcontroller *picker =
            [[Abpeoplepickernavigationcontroller alloc] init];
    Picker.peoplepickerdelegate = self;
    [Self presentmodalviewcontroller:picker animated:yes];
}

This picker invokes the method in its own delegate to respond to the user's actions.

1. If the user cancels, call-(void) Peoplepickernavigationcontrollerdidcancel: to dismiss this picker

2. If the user selects a person in the Address book, the

-(BOOL) Peoplepickernavigationcontroller: (Abpeoplepickernavigationcontroller *) peoplepicker Shouldcontinueafterselectingperson: (abrecordref) person

Copy the name and number into the label and then dismiss Picker

3. When the user clicks on a property of the person selected in picker, it will be called

-(BOOL) Peoplepickernavigationcontroller: (Abpeoplepickernavigationcontroller *) peoplepicker Shouldcontinueafterselectingperson: (abrecordref): (Abpropertyid) Property identifier: ( Abmultivalueidentifier) identifier

In this application, when a user selects a person, picker always dismiss, so there is no way to let the user select a property of this person. This means that this method will no longer be called. But if this method is excluded, the implementation of the whole protocol is incomplete.

-(void) Peoplepickernavigationcontrollerdidcancel:
            (Abpeoplepickernavigationcontroller *) peoplepicker
{
    [Self dismissmodalviewcontrolleranimated:yes];
}
-(BOOL) Peoplepickernavigationcontroller:
            (Abpeoplepickernavigationcontroller *) peoplepicker
      Shouldcontinueafterselectingperson: (abrecordref) Person {
    [Self Displayperson:person];
    [Self dismissmodalviewcontrolleranimated:yes];
    return NO;
}
-(BOOL) Peoplepickernavigationcontroller:
            (Abpeoplepickernavigationcontroller *) peoplepicker
      Shouldcontinueafterselectingperson: (abrecordref) person
                                Property: (Abpropertyid) property
                              Identifier: (abmultivalueidentifier) identifier
{
    return NO;
}
As shown in-(void) Displayperson: (abrecordref) person used to display names and numbersNote: The code for both is differentThe name is a string property, the record is named in the field (first name), this may also be a null phone number is a multivalued attribute, a person may have 0, 1 or more phone numbers in this example, only the first phone number in the list is used.
-(void) Displayperson: (abrecordref) person
{
    nsstring* name = (__bridge_transfer nsstring*) abrecordcopyvalue (person,
                                               Kabpersonfirstnameproperty);
    Self.firstName.text = name;
    nsstring* phone = nil;
    Abmultivalueref phonenumbers = Abrecordcopyvalue (person,
                                    Kabpersonphoneproperty);
    if (Abmultivaluegetcount (phonenumbers) > 0) {
       Phone = (__bridge_transfer nsstring*)
               Abmultivaluecopyvalueatindex (phonenumbers, 0);
    } else {
        Phone = @ "[None]";
    }
    Self.phoneNumber.text = phone;
}
Then run the program when you run the program to see a button and two empty tags click the button, pop a picker and then you choose a person, you can see the information

iOS Programming the Address Book (1)

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.