"iOS Dev-114" Abaddressbook Address Book: Simple Introduction to additions and deletions, cf frame conversion between f frames

Source: Internet
Author: User

(1) After importing the Abaddressbook library, and then adding the header file, you can use it.

-Because this library method is basically the C language style, so the class is basically a CF beginning, such as Cfstringref and Cfarrayref, it and OC NSString and Nsarray similar, can be converted to each other, using __bridge to cast, such as (__ Bridge NSString *) XXX, you can convert the original cfstringref xxx to nsstring type, the advantage is that there is no need to release operations in many places Cfrelease ().

--Using the Address book, the first need to send an access request, to obtain a request, before a series of operations, access to the request is the method of Abaddressbookrequestaccesswithcompletion, It is recommended that in the subsequent implementation of some address book operations can determine whether the authorization we access is still there, if the user changed the authorization, we do not have to further the operation, and the method is judged:

Before clicking, first determine if the IF (Abaddressbookgetauthorizationstatus ()!=kabauthorizationstatusauthorized) return is allowed;

--In addition to query, add, modify, delete, all need to finally save the Address Book, equivalent to do an update operation.

Save Abaddressbooksave (Bookref, NULL);

--The delete operation is:

Abaddressbookremoverecord (Bookref, people, NULL);


The main code is as follows:

#import "ViewController.h" #import <AddressBook/AddressBook.h> @interface Viewcontroller () @end @implementation    viewcontroller-(void) viewdidload {[Super viewdidload];    Create 3 buttons UIButton *btnaccess=[[uibutton alloc]init];    Btnaccess.frame=cgrectmake (20, 40, 280, 30);    [Btnaccess settitle:@ "Access to Contacts" forstate:uicontrolstatenormal];    [Btnaccess Setbackgroundcolor:[uicolor Bluecolor];    [Btnaccess addtarget:self Action: @selector (Btnaccessclick) forcontrolevents:uicontroleventtouchupinside];        [Self.view addsubview:btnaccess];    UIButton *btnupdate=[[uibutton Alloc]init];    Btnupdate.frame=cgrectmake (20, 80, 280, 30);    [Btnupdate settitle:@ "Change Contact" forstate:uicontrolstatenormal];    [Btnupdate Setbackgroundcolor:[uicolor Bluecolor];    [Btnupdate addtarget:self Action: @selector (Btnupdateclick) forcontrolevents:uicontroleventtouchupinside];        [Self.view Addsubview:btnupdate];    UIButton *btnadd=[[uibutton Alloc]init]; Btnadd.frame=cgrectmake (20, 120, 280,30);    [Btnadd settitle:@ "Add contact person" forstate:uicontrolstatenormal];    [Btnadd Setbackgroundcolor:[uicolor Bluecolor];    [Btnadd addtarget:self Action: @selector (Btnaddclick) forcontrolevents:uicontroleventtouchupinside];        [Self.view Addsubview:btnadd];    Send Access Address Book request Abaddressbookref bookref=abaddressbookcreatewithoptions (null, NULL); Abaddressbookrequestaccesswithcompletion (Bookref, ^ (bool granted, cferrorref error) {if (granted) {NSL        OG (@ "accessible");        }else{NSLog (@ "inaccessible");    }    }); C language Create, copy, retain can use the following to release Cfrelease (BOOKREF);}    -(void) btnaccessclick{//Because it is a CF framework Core Foundation, it is a C language, requires a lot of CF classes, and needs to be released//[self btnaccessclickwithc]; Some CF classes can be cast directly with __bridge to NS classes, such as arrays, characters, etc., and the benefit is that there is no need to release//convert between two frames with __bridge [self btnaccessclickwithoc];} -(void) btnaccessclickwithc{//Click First to determine if the IF (Abaddressbookgetauthorizationstatus ()! = is allowed    kabauthorizationstatusauthorized) return; After getting permission, get the data in the Address book Abaddressbookref Bookref=aBaddressbookcreatewithoptions (null, NULL);    Cfarrayref allpeople=abaddressbookcopyarrayofallpeople (BOOKREF);    Cfindex Count=cfarraygetcount (allpeople);        for (Cfindex i=0; i<count; i++) {abrecordref People=cfarraygetvalueatindex (allpeople, i);        Cfstringref Firstname=abrecordcopyvalue (people, kabpersonfirstnameproperty);        Cfstringref Lastname=abrecordcopyvalue (people, kabpersonlastnameproperty);                NSLog (@ "%@,%@", lastname,firstname);        Abmultivalueref Phone=abrecordcopyvalue (people, kabpersonphoneproperty);        Cfarrayref phones=abmultivaluecopyarrayofallvalues (phone);        Cfindex countphone=cfarraygetcount (phones);            for (int j=0; j<countphone; J + +) {cfstringref Phonetype=abmultivaluecopylabelatindex (phone, j);            Cfstringref Phonenum=abmultivaluecopyvalueatindex (phone, j);            NSLog (@ "%@-%@", phonetype,phonenum);            Release Cfrelease (Phonetype);        Cfrelease (Phonenum); }        Release Cfrelease (FirstName);        Cfrelease (LastName);        Cfrelease (phones);    Cfrelease (phone);    }//Release cfrelease (Allpeople); Release Cfrelease (BOOKREF);} -(void) btnaccessclickwithoc{//Click First to determine if the IF (Abaddressbookgetauthorizationstatus ()! = is allowed    kabauthorizationstatusauthorized) return;    After getting permission, get the data in the Address Book Abaddressbookref bookref=abaddressbookcreatewithoptions (null, NULL);    Nsarray *allpeople= (__bridge nsarray *) abaddressbookcopyarrayofallpeople (BOOKREF);        for (int i=0; i<allpeople.count; i++) {abrecordref people= (__bridge abrecordref) allpeople[i];        NSString *firstname= (__bridge NSString *) abrecordcopyvalue (people, kabpersonfirstnameproperty);        NSString *lastname= (__bridge NSString *) abrecordcopyvalue (people, kabpersonlastnameproperty);                NSLog (@ "%@,%@", lastname,firstname);        Abmultivalueref Phone=abrecordcopyvalue (people, kabpersonphoneproperty); Nsarray *phones= (__bridge nsarray *) AbmultivaLuecopyarrayofallvalues (phone); for (int j=0; j<phones.count; J + +) {nsstring *phonetype= (__bridge NSString *) Abmultivaluecopylabelatindex (P            Hone, J);            NSString *phonenum= (__bridge NSString *) Abmultivaluecopyvalueatindex (phone, j);        NSLog (@ "%@-%@", phonetype,phonenum);    }//Release cfrelease (phone); }//Release cfrelease (BOOKREF);} -(void) btnupdateclick{//Click First to determine if the IF (Abaddressbookgetauthorizationstatus ()! = is allowed    kabauthorizationstatusauthorized) return;    After getting permission, get the data in the Address Book Abaddressbookref bookref=abaddressbookcreatewithoptions (null, NULL);    Nsarray *allpeople= (__bridge nsarray *) abaddressbookcopyarrayofallpeople (BOOKREF);    Get 1th data abrecordref people= (__bridge abrecordref) allpeople[0];    Modified Abrecordsetvalue (People, Kabpersonlastnameproperty, ((__bridge cfstringref) @ "Sima"), NULL);    All the additions and deletions will eventually need to call the Save method Abaddressbooksave (Bookref, NULL); Release Cfrelease (BOOKREF);} -(void) btnaddclick{//Click to determineWhether the if (Abaddressbookgetauthorizationstatus ()!=kabauthorizationstatusauthorized) return is allowed;    After obtaining the permission, get the address book, need to add the contact to this address book, so need to create this abaddressbookref bookref=abaddressbookcreatewithoptions (null, NULL);    Create a contact Abrecordref people=abpersoncreate ();    Set the data abrecordsetvalue for the contact (people, Kabpersonlastnameproperty, ((__bridge cfstringref) @ "Zhuge"), NULL);        Abrecordsetvalue (People, Kabpersonfirstnameproperty, ((__bridge cfstringref) @ "bright"), NULL);    Abmultivalueref phone=abmultivaluecreatemutable (Kabmultistringpropertytype);    Abmultivalueaddvalueandlabel (Phone, ((__bridge cfstringref) @ "10086"), Kabpersonphonemainlabel, NULL);    Abmultivalueaddvalueandlabel (Phone, ((__bridge cfstringref) @ "13514678920"), Kabpersonphonemobilelabel, NULL);        Abrecordsetvalue (People, Kabpersonphoneproperty, phone, NULL);        Add Contact Abaddressbookaddrecord (bookref, people, NULL);        Save Abaddressbooksave (Bookref, NULL);    Release cfrelease (phone);    Cfrelease (people);Cfrelease (BOOKREF);} @end


"iOS Dev-114" Abaddressbook Address Book: Simple Introduction to additions and deletions, cf frame conversion between f frames

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.