How to customize the IOS address Book _ios

Source: Internet
Author: User
Tags zip

Apple provides users with their own address book, but according to the needs of the business needs to customize the address book, we need to customize according to business requirements.
First we need to know some of Apple's foundation, Uikit Framework, some of these features, can open the phone to view the Apple system provides the native address book, you can see:

1. Search box
2. Right Search bar
3. Contact Group
4. Add a Contact
5. Get contact Head and contact method

According to the requirements, can be summed up from the definition of the Address book needs to do some functions, a large function can be divided into a number of small functions, and then one implementation, the complex problem difference into small problem solving.

1. Custom Search box
2. Get First letter
3. Authority judgment
4. Sorting
5. Custom Search
6. Customize the Contact Interface
(consider the situation of multivalued, get the head book)

Brief introduction to the next sort:

The iOS project will use the contacts or members of the contact list to sort by name, because Nsarray does not directly support the ordering of Chinese characters, it is necessary to convert Chinese characters into pinyin to complete the order by A~z, which seems to be a headache, because the Chinese characters into pinyin, Gets the first letter of the first word of the Chinese character, such as turning the king into a "W".

The principle of function is: We know that in the Objective C language, strings are encoded in Unicode. In the Unicode character set, the encoding range of Chinese characters is between 4E00 and 9fa5 (that is, the 20,902 characters starting from 19,968th are Chinese characters). We put the phonetic initials of these characters in a char array in order. When we look up the phonetic initials of a Chinese character, we simply subtract the Unicode code of the Chinese character (that is, char cast to int) minus 19968, and then use that number as an index to find the letters stored in the char array.
Give the project source code, the annotation is very clear. Code such as:
RYAddressBook.h

#import <Foundation/Foundation.h> 
#import "RYPersonInfo.h" 
 
typedef void (^addressbookblock) (Nsarray * Personinfos); 
 
@interface ryaddressbook:nsobject 
 
/** 
 * Converts numbers to letters 0~26 1~25=a~z 26=# 
/nsstring* spellfromindex (int index); 
 
/** 
 * Get index 
/INT index (NSString *firstspell); 
/** 
 * Get the user All Address book information 
 * * 
 @return All Address Book data information array/ 
+ (void) Getpersoninfo: (Addressbookblock) Addressbookblock; 
 
/** 
 * Match all user information according to keywords * * 
 @param keyWord matching keyword 
 * 
 * @return Matching Address book data information array * * 
+ (void) Searchpersoninfo: (NSString *) KeyWord Addressbookblock: (addressbookblock) Addressbookblock; 
 
/** 
 * The reordering of arrays by name * 
 * 
 @param personinfos Gets the directory data information array * 
 * 
+ (Nsarray *) Sortpersoninfos: ( Nsarray *) Personinfos; 
 
@end 

Ryaddressbook.m

#import "RYAddressBook.h" @interface Ryaddressbook () @property (nonatomic, copy) Addressbookblock Addressbookblock 
 
; 
 @end @implementation ryaddressbook nsstring* spellfromindex (int index) {if (index = =) return @ "#"; 
else return [NSString stringwithformat:@ "%c", [@ "A" characteratindex:0]+index]; 
 
 int Index (NSString *firstspell) {int i = [Firstspell characteratindex:0]-[@ ' a ' characteratindex:0]; 
 if ([Firstspell isequaltostring:@ "#"] | | < 0 | | | i >) {return 26; 
return [Firstspell characteratindex:0]-[@ "a" characteratindex:0]; /** * Get user All Address Book information */+ (void) Getpersoninfo: (Addressbookblock) Addressbookblock {[[Self alloc] Getpersoninfo:a 
Ddressbookblock]; /** * Match all user information according to keywords * * + (void) Searchpersoninfo: (NSString *) KeyWord Addressbookblock: (addressbookblock) Addressbo 
okblock {[[Self alloc] Searchpersoninfo:keyword Addressbookblock:addressbookblock]; /** * reordering of arrays by name * * * (Nsarray *) SORTPERsoninfos: (Nsarray *) Personinfos {return [[Self alloc] sortpersoninfos:personinfos]; 
 }-(void) Getpersoninfo: (Addressbookblock) addressbookblock {self.addressbookblock = Addressbookblock; 
[Self searchpersoninfo:@ "]; }-(void) Searchpersoninfo: (NSString *) KeyWord Addressbookblock: (addressbookblock) Addressbookblock {Self.addressboo 
 kblock = Addressbookblock; 
[Self Searchpersoninfo:keyword]; }-(Nsarray *) Sortpersoninfos: (Nsarray *) Personinfos {if (![ 
 Personinfos Iskindofclass:[nsarray class]) {return nil; 
 
 } nsmutablearray *arr = [Nsmutablearray array]; 
 for (int i = 0; i < i++) {[arr addobject:[nsmutablearray array]]; For (NSObject *obj in Personinfos) {if (![ 
 obj Iskindofclass:[rypersoninfo class]] {continue; 
  
 } rypersoninfo *personinfo = (rypersoninfo *) obj; 
 Nsmutablearray *subarr = [arr Objectatindex:index (Personinfo.firstspell)]; 
 [Subarr Addobject:personinfo]; 
return arr; }/** * According to the key word checkInquire Address Book Information * *-(void) Searchpersoninfo: (NSString *) KeyWord {cferrorref error = NULL; 
 
 Abaddressbookref addressbook = abaddressbookcreatewithoptions (NULL, &error);  
  Start Query Address Book abaddressbookrequestaccesswithcompletion (addressbook, ^ (bool granted, cferrorref error) {if (granted) { 
 [Self Filtercontentforsearchtext:keyword]; 
} 
 }); /** * Start matching Address book information/-(void) Filtercontentforsearchtext: (nsstring*) SearchText {//exit if no authorization (Abaddressbook 
 Getauthorizationstatus ()!= kabauthorizationstatusauthorized) {return; 
 
 } nsarray *blockarray = [Nsarray array]; 
 Cferrorref error = NULL; 
 
 Abaddressbookref addressbook = abaddressbookcreatewithoptions (NULL, &error);  
  
 if ([SearchText length]==0) {//query all Blockarray = (__bridge Nsarray *) abaddressbookcopyarrayofallpeople (AddressBook); 
 else {//conditional query Cfstringref Cfsearchtext = (cfstringref) cfbridgingretain (SearchText); Blockarray = Cfbridgingrelease (Abaddressbookcopypeoplewithname (AddressBook, Cfsearchtext)); 
 Cfrelease (Cfsearchtext); 
 
 }//Type conversion Blockarray = transformelements (Blockarray); 
Return to Blockarray Self.addressbookblock (Blockarray); /** * Converts all elements into Jxpersoninfo type array/nsarray* transformelements (nsarray* arr) {Nsmutablearray *rtnarray = [NSMU 
 Tablearray array]; 
 for (int i = 0; i < Arr.count i++) {abrecordref recordref = Cfbridgingretain ([arr objectatindex:i]); 
  
 Rypersoninfo *personinfo = [Rypersoninfo personinfowithabrecordref:recordref]; 
 [Rtnarray Addobject:personinfo]; 
return rtnarray; 
 } @end

RYPersonInfo.h

#import <Foundation/Foundation.h> #import <AddressBook/AddressBook.h> @interface rypersoninfo:nsobject 
/** * Single Value information * * * #define PROPERTY_STR_READONLY (name) @property (nonatomic, copy) NSString *name; 
Surname Property_str_readonly (firstName)//Name Property_str_readonly (lastName)//middle name property_str_readonly (middlename) 
Full name Property_str_readonly (fullName)//Search index Property_str_readonly (firstspell)//prefix property_str_readonly (prefix) Suffix property_str_readonly (suffix)//nickname Property_str_readonly (nickname)//Surname _ Phonetic transcription property_str_readonly (firstnamephon etic)//Name _ Phonetic property_str_readonly (lastnamephonetic)//middle Name _ Phonetic transcription property_str_readonly (middlenamephonetic)/company PROP 
Erty_str_readonly (organization)//Work property_str_readonly (jobtitle)/Department Property_str_readonly (Department)//Birthday Property_str_readonly (Birthday)//Memo Property_str_readonly (Note)//time of first creating user information Property_str_readonly (firstknow)/ /last time the user information was changed property_str_readonlyKnow)//Business card type (Company/person) property_str_readonly (kind)//multivalued information #define PROPERTY_ARR_READONLY (name) @property (non 
Atomic, strong) Nsarray *name; Mailbox Property_arr_readonly (email)//address property_arr_readonly (adress)//date property_arr_readonly (dates)//Imessa GE property_arr_readonly (iMessage)//Phone number property_arr_readonly (phone)//URL link property_arr_readonly (URL) #defin 
e property_img_readonly (name) @property (nonatomic, strong) UIImage *name; Picture//property_img_readonly (image)/** * Initialization method * * @param ref Contact Attribute * * @return Instance object/-(ID) INITWITHABR 
 
Ecordref: (abrecordref) ref; 
 
/** * Initialization class method * * @param ref Contact Attribute * * @return Instance object/+ (ID) personinfowithabrecordref: (ABRECORDREF) ref; 
 @end

RYPERSONINFO.M

#import "RYPersonInfo.h" #define NULLSTRTOEMPTY (str) \ [str rangeofstring:@ ' null '].location==0? @ "": Str/** * Single-value information */#define GET_PROPERTY_METHOD (property, Property_key) \-(NSString *) Property {/Return ( 
 
NSString *) cfbridgingrelease (Abrecordcopyvalue (_recordref, property_key)); \} 
/** * Multivalued information */#define DICT_ADD_STR_FOR_KEY (DICT, str, KEY) \ if (str) {\ [DICT setobject:str forkey:key];\} #define Get_property_sigle_value_method (property, Property_key) \-(Nsarray *) property\ {\ nsmutablearray *rtnarray = [ Nsmutablearray array];\ \ Abmultivalueref ref = Abrecordcopyvalue (_recordref, property_key); \ Long Count = ABMultiValue GetCount (ref); \ for (int i = 0; i < count; i++) \ {\ nsstring* label = (__bridge nsstring*) abaddressbookcopylocalized 
Label (Abmultivaluecopylabelatindex (ref, i)); \ 
nsstring* content = (__bridge nsstring*) Abmultivaluecopyvalueatindex (ref, i); \ 
Nsmutabledictionary *dict = [Nsmutabledictionary dictionary];\ Dict_add_str_for_key(Dict, content, label); \ \ [Rtnarray addobject:dict];\} 
 
\ Return Rtnarray;\} 
 
@interface Rypersoninfo () @property (nonatomic, assign) Abrecordref recordref; @end @implementation Rypersoninfo-(ID) Initwithabrecordref: (abrecordref) ref {if (self = [super init]) {_recor 
 Dref = ref; 
return self; 
} + (ID) personinfowithabrecordref: (abrecordref) ref {return [[[Self class] alloc] initwithabrecordref:ref]; 
} get_property_method (FirstName, Kabpersonfirstnameproperty); 
Get_property_method (LastName, Kabpersonlastnameproperty); 
Get_property_method (MiddleName, Kabpersonmiddlenameproperty); 
Get_property_method (prefix, kabpersonprefixproperty); 
Get_property_method (suffix, kabpersonsuffixproperty); 
Get_property_method (nickname, Kabpersonnicknameproperty); 
Get_property_method (organization, Kabpersonorganizationproperty); 
Get_property_method (JobTitle, Kabpersonjobtitleproperty); Get_property_method (department, Kabpersondepartmentproperty); 
Get_property_method (birthday, kabpersonbirthdayproperty); 
Get_property_method (note, kabpersonnoteproperty); 
Get_property_method (Firstknow, Kabpersoncreationdateproperty); 
Get_property_method (Lastknow, Kabpersonmodificationdateproperty); 
Get_property_method (firstnamephonetic, Kabpersonfirstnamephoneticproperty); 
Get_property_method (lastnamephonetic, Kabpersonlastnamephoneticproperty); 
 
Get_property_method (middlenamephonetic, Kabpersonmiddlenamephoneticproperty); Get_property_sigle_value_method (email, kabpersonemailproperty) Get_property_sigle_value_method (dates, kabpersondateproperty) get_property_sigle_value_method (URL, kabpersonurlproperty) get_property_sigle_value_method 
 (Phone, Kabpersonphoneproperty)-(NSString *) Kind {nsstring *rtnstr = nil; 
 Cfnumberref RecordType = Abrecordcopyvalue (_recordref, Kabpersonkindproperty); 
 if (RecordType = = kabpersonkindorganization) {rtnstr = @ "Company"; 
 else {rtnstr = @ "Person"; return rtnstr; 
}-(Nsarray *) iMessage {Nsmutablearray *rtnarray = [Nsmutablearray array]; 
 Abmultivalueref instantmessage = Abrecordcopyvalue (_recordref, Kabpersoninstantmessageproperty); for (int i = 1; i < Abmultivaluegetcount (instantmessage); i++) {nsstring* label = (__bridge nsstring*) abmultivalue 
 Copylabelatindex (Instantmessage, i); 
  
 nsdictionary* content = (__bridge nsdictionary*) abmultivaluecopyvalueatindex (instantmessage, i); 
 Nsmutabledictionary *imessageinfodict = [Nsmutabledictionary dictionary]; 
 nsstring* username = [Content valueforkey: (NSString *) Kabpersoninstantmessageusernamekey]; 
 nsstring* service = [content valueforkey: (NSString *) Kabpersoninstantmessageservicekey]; 
 Dict_add_str_for_key (imessageinfodict, username, @ "username"); 
  
 Dict_add_str_for_key (imessageinfodict, service, @ "service"); 
 Nsdictionary *imessagedict = @{label:imessageinfodict}; 
 [Rtnarray addobject:imessagedict]; 
return rtnarray; }-(Nsarray *) address {NsmutableaRray *rtnarray = [Nsmutablearray array]; 
 Abmultivalueref address = Abrecordcopyvalue (_recordref, Kabpersonaddressproperty); 
 Long Count = Abmultivaluegetcount (address);  for (int i = 0; i < count; i++) {nsstring* Addresslabel = (__bridge nsstring*) abmultivaluecopylabelatindex (address, 
 i); 
  
 nsdictionary* personaddress = (__bridge nsdictionary*) abmultivaluecopyvalueatindex (address, i); 
 Nsmutabledictionary *addressinfodict = [Nsmutabledictionary dictionary]; 
 nsstring* country = [personaddress valueforkey: (NSString *) Kabpersonaddresscountrykey]; 
 nsstring* City = [personaddress valueforkey: (NSString *) Kabpersonaddresscitykey]; 
 nsstring* state = [personaddress valueforkey: (NSString *) Kabpersonaddressstatekey]; 
 nsstring* Street = [personaddress valueforkey: (NSString *) Kabpersonaddressstreetkey]; 
 nsstring* zip = [personaddress valueforkey: (NSString *) Kabpersonaddresszipkey]; nsstring* Coutntrycode = [personaddress valueforkey: (NSString *) KabpersonaddresscountrycoDekey]; 
 Dict_add_str_for_key (addressinfodict, Country, @ "country"); 
 Dict_add_str_for_key (addressinfodict, City, @ "City"); 
 Dict_add_str_for_key (addressinfodict, State, @ "state"); 
 Dict_add_str_for_key (addressinfodict, Street, @ "Street"); 
 Dict_add_str_for_key (addressinfodict, zip, @ "zip"); 
  
 Dict_add_str_for_key (Addressinfodict, Coutntrycode, @ "Coutntrycode"); 
 Nsdictionary *addressdict = @{addresslabel:addressinfodict}; 
 [Rtnarray addobject:addressdict]; 
return rtnarray; 
}//-(UIImage *) image//{//NSData *data = (__bridge nsdata*) abpersoncopyimagedata (_RECORDREF); 
return [UIImage Imagewithdata:data]; #pragma mark-#pragma mark-customproperty/** * Full Name * * (NSString *) fullName {return [NSString string withformat:@ "%@%@%@", Nullstrtoempty (Self.lastname), Nullstrtoempty (Self.middlename), Nullstrtoempty (self.firstNa 
Me)]; 
/** * First Letter * * (NSString *) Firstspell {return Getfirstspell (self.fullname); }/** * Output dieType ALL information * *-(NSString *) Description {return [NSString stringwithformat:@ "%@--Infopacket", self.fullname]; /** * Get First letter * * nsstring* Getfirstspell (nsstring *fullname) {nsmutablestring *ms = [[Nsmutablestring alloc] I 
 Nitwithstring:fullname]; 
 Cfstringtransform ((__bridge cfmutablestringref) MS, 0, Kcfstringtransformmandarinlatin, NO); 
 
 Cfstringtransform ((__bridge cfmutablestringref) MS, 0, Kcfstringtransformstripdiacritics, NO); 
 if (Fullname.length > 0) return [[Ms Substringwithrange:nsmakerange (0, 1)] lowercasestring]; 
else return @ "#"; 
 } @end

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.