IOS--------Apps referencing system contacts

Source: Internet
Author: User
Tags uikit

Transferred from: http://www.cnblogs.com/ygm900/p/3472288.html

Due to the iOS system's control over user privacy, third-party applications can only invoke the system address book via Apple's official interface and not operate the contacts database directly as Android does.
In general, there are two ways to use the system's own address book, one is to direct the entire address book into the application, and the other is to read each contact information in the Address Book. Below we will be one by one detailed.

1 direct reference to the entire address book

Class used: Abpeoplepickernavigationcontroller
Method:

#import <UIKit/UIKit.h> #import <AddressBook/AddressBook.h> #import in the LocalAddressBookController.h file <AddressBookUI/AddressBookUI.h> @interface localaddressbookcontroller:uiviewcontroller< Abpersonviewcontrollerdelegate,abpeoplepickernavigationcontrollerdelegate,abnewpersonviewcontrollerdelegate    >{Abpeoplepickernavigationcontroller *picker; Abnewpersonviewcontroller *personviewcontroller;} @end #import "LocalAddressBookController.h" #import "PublicHeader.h" @ in the localaddressbookcontroller.m file Implementation localaddressbookcontroller-(ID) initwithnibname: (NSString *) Nibnameornil Bundle: (NSBundle *)    nibbundleornil{self = [super Initwithnibname:nibnameornil Bundle:nibbundleornil]; if (self) {//Custom initialization} return to self;}    -(void) didreceivememorywarning{//Releases the view if it doesn ' t has a superview.        [Super didreceivememorywarning]; Release any cached data, images, etc-aren ' t in use.} #pragma mark-view lifecycle-(void) viewdidload{[Super Viewdidload];    Picker = [[Abpeoplepickernavigationcontroller alloc]init];    Picker.view.frame = CGRectMake (0, 0, screen_width, screen_height-48);    Picker.peoplepickerdelegate = self;                   Picker.delegate = self;    In order to hide the "Cancel" button in the upper right corner [picker sethidesbottombarwhenpushed:yes]; [Picker Setnavigationbarhidden:no animated:no];//Displays the above Navigationbar and search box [Self.view AddSubview:picker.view];} #pragma mark Uinavigationcontrollerdelegate methods//hide the Cancel button-(void) Navigationcontroller: (Uinavigationcontroller * ) Navigationcontroller Willshowviewcontroller: (Uiviewcontroller *) Viewcontroller animated: (BOOL) animated{UIView *    custom = [[UIView alloc] Initwithframe:cgrectmake (0.0f,0.0f,0.0f,0.0f)];    Uibarbuttonitem *btn = [[Uibarbuttonitem alloc] initwithcustomview:custom];    [Viewcontroller.navigationitem setrightbarbuttonitem:btn Animated:no];    [BTN release]; [Custom release];} #pragma mark-peoplepickerdelegate methods-(BOOL) PeoplepickErnavigationcontroller: (Abpeoplepickernavigationcontroller *) Peoplepicker Shouldcontinueafterselectingperson: (    ABRECORDREF) person{//[picker setnavigationbarhidden:no animated:no];     NSLog (@ "%@", (nsstring*) abrecordcopycompositename (person)); return YES;} -(BOOL) Peoplepickernavigationcontroller: (Abpeoplepickernavigationcontroller *) peoplepicker Shouldcontinueafterselectingperson: (abrecordref): (Abpropertyid) Property identifier: ( Abmultivalueidentifier) identifier{return YES;} -(BOOL) Personviewcontroller: (Abpersonviewcontroller *) Personviewcontroller Shouldperformdefaultactionforperson: ( ABRECORDREF): (Abpropertyid) Property identifier: (Abmultivalueidentifier) identifier{return YES;}//" Cancel the delegate response method of the button-(void) Peoplepickernavigationcontrollerdidcancel: (Abpeoplepickernavigationcontroller *) PeoplePicker {//assigning control back to the main controller [Picker dismissmodalviewcontrolleranimated:yes];}
......
@end

IOS6 Operation Effect:

iOS7 Operation Effect:

Experience:

When we introduce the whole system address book, there is a "cancel" button in the upper right corner of the address book that comes with the system. How can I hide this cancel button?

(1) Method 1: If your application is developed with an enterprise certificate and does not need to be submitted to AppStore for review, the answer is very simple, add the following code to the Piker of the response:

[Picker Setallowscancel:no];

(2) Method Two: The above method is a non-public method, is unable to pass the AppStore audit. If you want to pass the audit. Here are some ways to try it:

Premise: Set picker.delegate = self, then implement the following delegate method-(void) Navigationcontroller: (Uinavigationcontroller *) Navigationcontroller Willshowviewcontroller: (Uiviewcontroller *) Viewcontroller animated: (BOOL) animated {  UIView *custom = [UIView Alloc] Initwithframe:cgrectmake (0.0f,0.0f,0.0f,0.0f)];   Uibarbuttonitem *btn = [[Uibarbuttonitem alloc] initwithcustomview:custom];   Uibarbuttonitem *btn = [[Uibarbuttonitem alloc] Initwithbarbuttonsystemitem:uibarbuttonsystemitemadd target:self Action: @selector (Addaction)];   [Viewcontroller.navigationitem setrightbarbuttonitem:btn Animated:no];   [BTN release];   [Custom release]; }

Or: (better than above)

Premise: Set picker.delegate = self, and then implement the following delegate method, the following effect is better than the above. The above effect, when clicked on the "Search" box, the "Cancel" button will also appear again. -(void) Navigationcontroller: (Uinavigationcontroller *) Navigationcontroller      Willshowviewcontroller: ( Uiviewcontroller *) Viewcontroller                    animated: (BOOL) Animated {        //Here we want to remove the ' Cancel ' button, but onl Y if we ' re showing    //either of the Abpeoplepickernavigationcontroller ' s top of the controllers    if ([navigationcontr Oller.viewcontrollers Indexofobject:viewcontroller] <= 1) {        ViewController.navigationItem.rightBarButtonItem = nil;    }}

2. Read each contact information in the Address Book by article.

Method: In the above class, directly add the following method can be

#import <UIKit/UIKit.h> #import <AddressBook/AddressBook.h> #import in the LocalAddressBookController.h file <AddressBookUI/AddressBookUI.h> @interface Localaddressbookcontroller: {Uitextview *textview;} @end #import "LocalAddressBookController.h" #import "PublicHeader.h" @ in the localaddressbookcontroller.m file Implementation localaddressbookcontroller-(ID) initwithnibname: (NSString *) Nibnameornil Bundle: (NSBundle *)    nibbundleornil{self = [super Initwithnibname:nibnameornil Bundle:nibbundleornil]; if (self) {//Custom initialization} return to self;}    -(void) didreceivememorywarning{//Releases the view if it doesn ' t has a superview.        [Super didreceivememorywarning]; Release any cached data, images, etc-aren ' t in use.}        #pragma mark-view lifecycle-(void) viewdidload{[Super Viewdidload];    TextView = [[Uitextview alloc]initwithframe:cgrectmake (0, 0, Screen_width, screen_height)];    [Self getaddressbook]; [Self.view Addsubview:textview];}//gets all the attributes in the Address Book and is stored in the TextView, verified, and practical. Compatible with Io6 and iOS 7, and iOS7 does not have permission confirmation prompts.        -(void) getaddressbook{abaddressbookref addressbook = Abaddressbookcreate ();        Cfarrayref results = abaddressbookcopyarrayofallpeople (addressbook);        for (int i = 0; i < Cfarraygetcount (results); i++) {Abrecordref person = cfarraygetvalueatindex (results, i);        Read FirstName NSString *personname = (nsstring*) abrecordcopyvalue (person, kabpersonfirstnameproperty);        if (personname! = nil) Textview.text = [Textview.text stringbyappendingformat:@ "\ n Name:%@\n", personname];        Read LastName NSString *lastname = (nsstring*) abrecordcopyvalue (person, kabpersonlastnameproperty);        if (LastName! = nil) Textview.text = [Textview.text stringbyappendingformat:@ "%@\n", LastName];        Read MiddleName nsstring *middlename = (nsstring*) abrecordcopyvalue (person, kabpersonmiddlenameproperty); if (middlename! = nil) Textview.text = [TEXTVIew.text stringbyappendingformat:@ "%@\n", MiddleName];        Read prefix prefix nsstring *prefix = (nsstring*) abrecordcopyvalue (person, kabpersonprefixproperty);        if (prefix! = nil) Textview.text = [Textview.text stringbyappendingformat:@ "%@\n", prefix];        Read suffix suffix nsstring *suffix = (nsstring*) abrecordcopyvalue (person, kabpersonsuffixproperty);        if (suffix! = nil) Textview.text = [Textview.text stringbyappendingformat:@ "%@\n", suffix];        Read nickname called NSString *nickname = (nsstring*) abrecordcopyvalue (person, kabpersonnicknameproperty);        if (nickname! = nil) Textview.text = [Textview.text stringbyappendingformat:@ "%@\n", nickname]; Read FirstName phonetic transcription nsstring *firstnamephonetic = (nsstring*) abrecordcopyvalue (person, Kabpersonfirstnamephoneticprop        Erty);         if (firstnamephonetic! = nil) Textview.text = [Textview.text stringbyappendingformat:@ "%@\n", firstnamephonetic]; Read LastNamePhonetic transcription nsstring *lastnamephonetic = (nsstring*) abrecordcopyvalue (person, kabpersonlastnamephoneticproperty);        if (lastnamephonetic! = nil) Textview.text = [Textview.text stringbyappendingformat:@ "%@\n", lastnamephonetic]; Read MiddleName phonetic transcription nsstring *middlenamephonetic = (nsstring*) abrecordcopyvalue (person, Kabpersonmiddlenameph        Oneticproperty); if (middlenamephonetic! = nil) Textview.text = [Textview.text stringbyappendingformat:@ "%@\n", middlenamephonetic        ];        Read Organization company NSString *organization = (nsstring*) abrecordcopyvalue (person, kabpersonorganizationproperty);        if (organization! = nil) Textview.text = [Textview.text stringbyappendingformat:@ "%@\n", organization];        Read JobTitle work NSString *jobtitle = (nsstring*) abrecordcopyvalue (person, kabpersonjobtitleproperty);        if (jobtitle! = nil) Textview.text = [Textview.text stringbyappendingformat:@ "%@\n", JobTitle]; Read the DePartment Department NSString *department = (nsstring*) abrecordcopyvalue (person, kabpersondepartmentproperty);        if (department! = nil) Textview.text = [Textview.text stringbyappendingformat:@ "%@\n", department];        Read Birthday birthday NSDate *birthday = (nsdate*) abrecordcopyvalue (person, kabpersonbirthdayproperty);        if (Birthday! = nil) Textview.text = [Textview.text stringbyappendingformat:@ "%@\n", birthday];        Read Note Memo nsstring *note = (nsstring*) abrecordcopyvalue (person, kabpersonnoteproperty);        if (Note! = nil) Textview.text = [Textview.text stringbyappendingformat:@ "%@\n", note];        The first time the record was added nsstring *firstknow = (nsstring*) abrecordcopyvalue (person, kabpersoncreationdateproperty);        NSLog (@ "The first time the record was added%@\n", Firstknow);        The last time the record was modified nsstring *lastknow = (nsstring*) abrecordcopyvalue (person, kabpersonmodificationdateproperty);            NSLog (@ "Last modified time of the record%@\n", Lastknow);    Get email multi-value Abmultivalueref email = abrecordcopyvalue (person, kabpersonemailproperty);        int emailcount = abmultivaluegetcount (email); for (int x = 0; x < Emailcount; + +) {//Get email Label nsstring* Emaillabel = (nsstring*) A            Baddressbookcopylocalizedlabel (Abmultivaluecopylabelatindex (email, x));            Get Email value nsstring* emailcontent = (nsstring*) abmultivaluecopyvalueatindex (email, x);        Textview.text = [Textview.text stringbyappendingformat:@ "%@:%@\n", emaillabel,emailcontent];        }//Read address multi-value abmultivalueref addressing = Abrecordcopyvalue (person, kabpersonaddressproperty);                int count = Abmultivaluegetcount (address); for (int j = 0; J < Count; J + +) {//Get Address label nsstring* Addresslabel = (nsstring*) Abmultiva            Luecopylabelatindex (address, j);            Textview.text = [Textview.text stringbyappendingformat:@ "%@\n", Addresslabel]; Get theLabel under the Address 6 property nsdictionary* personaddress = (nsdictionary*) Abmultivaluecopyvalueatindex (address, j);            nsstring* country = [personaddress valueforkey: (NSString *) Kabpersonaddresscountrykey];            if (country! = nil) Textview.text = [Textview.text stringbyappendingformat:@ "Country:%@\n", country];            nsstring* City = [personaddress valueforkey: (NSString *) Kabpersonaddresscitykey];            if (city = nil) Textview.text = [Textview.text stringbyappendingformat:@ "Cities:%@\n", urban];            nsstring* state = [personaddress valueforkey: (NSString *) Kabpersonaddressstatekey];            if (state = nil) Textview.text = [Textview.text stringbyappendingformat:@ "province:%@\n", State];            nsstring* Street = [personaddress valueforkey: (NSString *) Kabpersonaddressstreetkey];            if (street! = nil) Textview.text = [Textview.text stringbyappendingformat:@ "Street:%@\n", street]; nsstring* zip = [personAddress Valueforkey: (NSString *) Kabpersonaddresszipkey];            if (zip = nil) Textview.text = [Textview.text stringbyappendingformat:@ "ZIP code:%@\n", zip];            nsstring* Coutntrycode = [personaddress valueforkey: (NSString *) Kabpersonaddresscountrycodekey];        if (coutntrycode! = nil) Textview.text = [Textview.text stringbyappendingformat:@ "Country code:%@\n", Coutntrycode];        }//Get dates Multi-value abmultivalueref dates = Abrecordcopyvalue (person, kabpersondateproperty);        int datescount = Abmultivaluegetcount (dates); for (int y = 0; y < Datescount; y++) {//Get dates Label nsstring* Dateslabel = (nsstring*) A            Baddressbookcopylocalizedlabel (Abmultivaluecopylabelatindex (dates, y));            Gets the dates value nsstring* datescontent = (nsstring*) abmultivaluecopyvalueatindex (dates, y);       Textview.text = [Textview.text stringbyappendingformat:@ "%@:%@\n", dateslabel,datescontent]; }//Get kind value cfnumberref RecordType = Abrecordcopyvalue (person, kabpersonkindproperty);        if (RecordType = = kabpersonkindorganization) {//it ' s a company NSLog (@ "It's a company\n");        } else {//it ' s a person, resource, or NSLog (@ "It's a person, resource, or room\n"); }//Get IM Multi-valued abmultivalueref instantmessage = Abrecordcopyvalue (person, KABPERSONINSTANTMESSAG        Eproperty); for (int l = 1; l < Abmultivaluegetcount (instantmessage); l++) {//Get IM Label nsstring* in            Stantmessagelabel = (nsstring*) abmultivaluecopylabelatindex (Instantmessage, L);            Textview.text = [Textview.text stringbyappendingformat:@ "%@\n", Instantmessagelabel]; Gets the 2 property under the label nsdictionary* instantmessagecontent = (nsdictionary*) abmultivaluecopyvalueatindex (InstantMessag            E, L); nsstring* username = [Instantmessagecontent valueforkey: (NSString *) Kabpersoninstantmessageusernamekey];                        if (username! = nil) Textview.text = [Textview.text stringbyappendingformat:@ "username:%@\n", username];             nsstring* service = [instantmessagecontent valueforkey: (NSString *) Kabpersoninstantmessageservicekey]; if (service! = nil) Textview.text = [Textview.text stringbyappendingformat:@ "service:%@\n", servi        CE];        }//Read phone multi-value Abmultivalueref phone = abrecordcopyvalue (person, kabpersonphoneproperty); for (int k = 0; K<abmultivaluegetcount (phone); k++) {//Get Phone label NSString * Personphonela            Bel = (nsstring*) Abaddressbookcopylocalizedlabel (Abmultivaluecopylabelatindex (phone, k));                        Get the phone value under the label NSString * Personphone = (nsstring*) abmultivaluecopyvalueatindex (phone, k);      Textview.text = [Textview.text stringbyappendingformat:@ "%@:%@\n", Personphonelabel,personphone];  }//Get URL multi-value abmultivalueref URL = abrecordcopyvalue (person, kabpersonurlproperty); for (int m = 0; m < abmultivaluegetcount (URL); m++) {//Get Phone label NSString * URLLabel = (N            sstring*) Abaddressbookcopylocalizedlabel (Abmultivaluecopylabelatindex (URL, m));                        Gets the phone value under the label NSString * urlcontent = (nsstring*) abmultivaluecopyvalueatindex (url,m);        Textview.text = [Textview.text stringbyappendingformat:@ "%@:%@\n", urllabel,urlcontent];                        }//Read photo NSData *image = (nsdata*) abpersoncopyimagedata (person);        Uiimageview *myimage = [[Uiimageview alloc] Initwithframe:cgrectmake (200, 0, 50, 50)];        [MyImage setimage:[uiimage imagewithdata:image];        Myimage.opaque = YES;    [TextView Addsubview:myimage];    } cfrelease (results); Cfrelease (addressbook);}
......
@end

iOS7 Operation Effect:

Reference:

To expand the top right corner of the "Cancel" button can see http://d2100.com/questions/6864

How to hide the "Cancel" button in the upper right corner (with viable code) http://stackoverflow.com/questions/1611499/ Abpeoplepickernavigationcontroller-remove-cancel-button-without-using-privat

How to use iOS AddressBook summary is comprehensive, there is generality http://www.cnblogs.com/y041039/archive/2012/03/22/2411771.html

IOS--------Apps referencing system contacts

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.