IPhone Add/Delete contact group
1. Add addressbook. Framework
2. Add a Contact Group
View code
-( Void ) Addgroup { // Create Group Abaddressbookref AB = Abaddressbookcreate (); cferrorref error; abrecordref Group = Abgroupcreate (); abrecordsetvalue (group, kabgroupnameproperty, @" New Group " ,& Error); abaddressbookaddrecord (AB, group, & Error); abaddressbooksave (AB, & Error ); // Create new person and save to this group Abrecordref record = Abpersoncreate (); bool issuccess; issuccess = Abrecordsetvalue (record, kabpersonnicknameproperty, @" Groupmember nick name " ,& Error); issuccess = Abrecordsetvalue (record, kabpersonmiddlenameproperty, @" Middle name " ,& Error); abmutablemultivalueref copyofphones = Abmultivaluecreatemutable (kabpersonphoneproperty); cftyperef phone = Cfstr ( " 123000222111 " ); Abmultivalueaddvalueandlabel (copyofphones, phone, kabpersonphonecancelabel, null); issuccess = Abrecordsetvalue (record, kabpersonphoneproperty, copyofphones ,&Error); issuccess = Abaddressbookaddrecord (AB, record ,& Error); issuccess = Abaddressbooksave (AB ,& Error); abgroupaddmember (group, record, & Error); nslog ( @" Is success % d " , Issuccess); abaddressbooksave (AB, & Error); cfrelease (Group );}
3. Get the Contact Group
View code
-(Nsarray *) Getgroups {nsmutablearray * Groupnames = [[Nsmutablearray alloc] init] autorelease]; abaddressbookref AB = Abaddressbookcreate (); nsarray * Array = (nsarray * ) Abaddressbookcopyarrayofallgroups (AB ); For ( Int I = 0 ; I <[array count]; I ++ ) {Abrecordref Group = [Array objectatindex: I]; cftyperef groupname =Abrecordcopyvalue (group, kabgroupnameproperty); nsstring * Groupnamestr = [nsstring stringwithformat: @" % @ " , (Nsstring * ) Groupname]; nslog ( @" Groupnames is % @ " , Groupnamestr); [groupnames addobject: groupnamestr]; cfrelease (groupname );} Return Groupnames ;}
4. delete a Contact Group
View code
// Delete all Groups -( Void ) Deletegroups {abaddressbookref AB = Abaddressbookcreate (); cferrorref error; // Get all Groups Cfarrayref groups = Abaddressbookcopyarrayofallgroups (AB ); // Groups 'count Cfindex groupcount = Abaddressbookgetgroupcount (AB ); For ( Int I = 0 ; I <groupcount; I ++ ) {Abrecordref Group = Cfarraygetvalueatindex (groups, I ); // Delete Abaddressbookremoverecord (AB, group ,& Error); abaddressbooksave (AB, & Error); cfrelease (groups );}
5. For more information about contact operations, see myArticle: Add, delete, and modify iPhone address book contacts
6. Reference articles written in this article: iPhone address book operation, abaddressbook delete contacts and Groups