Simple IOS address book operation encapsulation (add a contact and check whether the number exists)

Source: Internet
Author: User

Sjabhelper. h

 # Import <Foundation/Foundation. h> Enum  {Abhelpercannotconncettoaddressbook,  /*  * <An error occurred while connecting to the address book (user permission is required to access the address book after ios6)  */  Abhelperexistspecificcontact,  /*  * <The number already exists.  */  Abhelpernotexistspecificcontact  /*  * <The number does not exist.  */ }; Typedef nsuinteger abhelpercheckexistresulttype;  @ Interface  Sjabhelper: nsobject  /*  ** Add contact ** @ Param Name contact name * @ Param num phone number * @ Param label Label label note ** @ return whether the contact is successfully added  */ + (Bool) addcontactname :( nsstring *) Name phonenum :( nsstring *) num withlabel :( nsstring * ) Label;  /*  ** Query whether the specified number already exists in the address book ** @ Param phonenum phone number ** @ return returns the query result. For details, see Enumeration type annotations.  */ + (Abhelpercheckexistresulttype) existphone :( nsstring *) Phonenum;  @ End 

 

Sjabhelper. m

You need to introduce the addressbook framework.

 # Import   "  Sjabhelper. h  "  # Import <Addressbook/addressbook. h> @ Implementation  Sjabhelper + (Sjabhelper * ) Parameter control {  Static Sjabhelper * Instance;  Static Dispatch_once_t oncetoken; dispatch_once ( & Oncetoken, ^ {Instance = [[Sjabhelper alloc] init] ;});  Return  Instance ;} -( ID  ) Init {self = [Super init];  If  (Self ){}  Return  Self ;} + (Bool) addcontactname :( nsstring *) Name phonenum :( nsstring *) num withlabel :( nsstring *) Label {  Return  [[Sjabhelper modify control] addcontactname: Name phonenum: num withlabel: Label];} -(Bool) addcontactname :( nsstring *) Name phonenum :( nsstring *) num withlabel :( nsstring * ) Label {abrecordref record = Abpersoncreate (); cferrorref error;  //  Use the _ bridge_retained keyword to convert the nsstring to the cfstring type and hand over the memory management right from the arc. Cfstringref cf_name = (_ Bridge_retained cftyperef) Name; cfstringref cf_num = (_ Bridge_retained cftyperef) num; cfstringref cf_label =(_ Bridge_retained cftyperef) label; abrecordsetvalue (record, kabpersonfirstnameproperty, cf_name, & Error); abmutablemultivalueref multi = Abmultivaluecreatemutable (kabpersonphoneproperty); abmultivalueaddvalueandlabel (multi, cf_num, cf_label, null); abrecordsetvalue (record, kabpersonphoneproperty, multi, & Error); cfrelease (Multi); abaddressbookref addressbook = Nil;  If ([[Uidevice currentdevice]. systemversion floatvalue]> = 6.0  ) {Addressbook =Abaddressbookcreatewitexceptions (null, null );  //  Execute the following command after your consent Dispatch_semaphore_t Sema = dispatch_semaphore_create ( 0  ); Abaddressbookrequestaccesswithcompletion (addressbook, ^ ( Bool  Granted, cferrorref error) {dispatch_semaphore_signal (SEMA) ;}); dispatch_semaphore_wait (Sema, dispatch_time_forever); dispatch_release (SEMA );}  Else {Addressbook = Abaddressbookcreate ();} bool success = Abaddressbookaddrecord (addressbook, record ,& Error );  If (! Success) {cfrelease (record); cfrelease (addressbook );  Return  No ;}  Else  {Success = Abaddressbooksave (addressbook ,& Error); cfrelease (record); cfrelease (addressbook );  Return Success? Yes: No ;}} + (Abhelpercheckexistresulttype) existphone :( nsstring * ) Phonenum {  Return  [[Sjabhelper internal control] existphone: phonenum];} -(Abhelpercheckexistresulttype) existphone :( nsstring * ) Phonenum {abaddressbookref addressbook = Nil;  If ([[Uidevice currentdevice]. systemversion floatvalue]> = 6.0  ) {Addressbook =Abaddressbookcreatewitexceptions (null, null );  //  Execute the following command after your consent Dispatch_semaphore_t Sema = dispatch_semaphore_create ( 0  ); Abaddressbookrequestaccesswithcompletion (addressbook, ^ ( Bool  Granted, cferrorref error) {dispatch_semaphore_signal (SEMA) ;}); dispatch_semaphore_wait (Sema, dispatch_time_forever); dispatch_release (SEMA );}  Else {Addressbook = Abaddressbookcreate ();} cfarrayref records;  If  (Addressbook) {records = Abaddressbookcopyarrayofallpeople (addressbook );}  Else  {  # Ifdef  Debug nslog (  @"  Can not connect to address book  "  );  # Endif         Return  Abhelpercannotconncettoaddressbook ;}  For ( Int I = 0 ; I <cfarraygetcount (records); I ++ ) {Abrecordref record = Cfarraygetvalueatindex (records, I); cftyperef items = Abrecordcopyvalue (record, kabpersonphoneproperty); cfarrayref phonenums = Abmultivaluecopyarrayofallvalues (items );  If  (Phonenums ){ For ( Int J = 0 ; J <cfarraygetcount (phonenums); j ++ ) {Cfstringref cf_phone = Cfarraygetvalueatindex (phonenums, J); nsstring * Phone = (_ bridge nsstring * ) Cf_phone;  If  ([Phonenum isw.tostring: Phone]) {cfrelease (phonenums); cfrelease (items); cfrelease (records); cfrelease (addressbook );  Return Abhelperexistspecificcontact;} cfrelease (phonenums);} cfrelease (items);} cfrelease (records); cfrelease (addressbook );  Return  Abhelpernotexistspecificcontact ;}  @ End 

 

Note:IsNew user privacy protection in ios6Make the callAbaddressbookcreate ()Alert will pop up to allow the user to confirm whether or not to allow, need to addCode:

 //  For systems above iOS 6, you need to wait for the user to confirm whether to allow access to the address book.      If ([[Uidevice currentdevice]. systemversion floatvalue]> =6.0  ) {Addressbook = Abaddressbookcreatewitexceptions (null, null );  //  Execute the following command after your consent Dispatch_semaphore_t Sema = dispatch_semaphore_create ( 0  ); Abaddressbookrequestaccesswithcompletion (addressbook, ^ ( Bool Granted, cferrorref error) {dispatch_semaphore_signal (SEMA) ;}); dispatch_semaphore_wait (Sema, dispatch_time_forever); dispatch_release (SEMA );} 

Otherwise, the addressbook is null before the user confirms the operation.ProgramCrash.

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.