Recentemente, tive que criar e salvar contatos em um aplicativo iOS e preferi usar uma biblioteca Objective-C para interagir com o AddressBook – acabei usando RHAddressBook.
Embora a documentação tenha sido útil, eu poderia ter usado um exemplo, especificamente quando chegou a hora de salvar endereços e identificadores sociais. Portanto, aqui está um exemplo que criei de como salvar um contato com RHAddressBook (inclui salvar vários telefones, e-mails, endereços sociais e endereços físicos).
//Granted permission to use address book
RHAddressBook *ab = [[RHAddressBook alloc] init];
RHPerson *newPerson = [addressBook newPersonInDefaultSource];
newPerson.firstName = @"John";
newPerson.lastName = @"Smith";
[newPerson setOrganization:@"Google"]
[newPerson setImage: image];
//Phone Numbers
RHMultiStringValue *phoneMultiValue = [newPerson phoneNumbers];
RHMutableMultiStringValue *mutablePhoneMultiValue = [phoneMultiValue mutableCopy];
mutablePhoneMultiValue = [[RHMutableMultiStringValue alloc] initWithType:kABMultiStringPropertyType];
//if the person has multiple numbers, iterate through the phone numbers and add them to the RHMutableMultiStringValue
for (KBPhone *phone in phoneNumbers) {
//choose the appropriate label based on the number type, here we just assigned a home label.
[mutablePhoneMultiValue addValue:phone.phoneNumber withLabel:RHHomeLabel];
}
newPerson.phoneNumbers = mutablePhoneMultiValue;
//
//Social accounts
RHMultiDictionaryValue *socialMultiDictionaryValue = [newPerson socialProfiles];
RHMutableMultiDictionaryValue *mutablesocialMultiValue = [socialMultiDictionaryValue mutableCopy];
mutablesocialMultiValue = [[RHMutableMultiStringValue alloc] initWithType:kABMultiDictionaryPropertyType];
//if the person has multiple socials, iterate through the social accounts and add them to the RHMutableMultiDictionaryValue
NSInteger index = 0;
for (KBSocial *social in socials) {
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
//set the social network for the serviceKey
//for example below, twitter. You can assign any label where "RHPersonSocialProfileServiceTwitter" is, but if it isn't a reconized service provider
//when tapped it will not open the app. You can set the URL that opens the app below.
[dict setValue:RHPersonSocialProfileServiceTwitter forKey:RHPersonSocialProfileServiceKey];
//set the username for the UsernameKey
[dict setValue:social.handle forKey:RHPersonSocialProfileUsernameKey];
//can set the URL that opens when a network is clicked. Don't need this for Facebook and other built in networks, but do for instagram and others.
[dict setValue:@"instagram://..." forKey:RHPersonSocialProfileURLKey];
[mutablesocialMultiValue insertValue:dict withLabel:@"social" atIndex:index];
index++;
}
newPerson.socialProfiles = mutablesocialMultiValue;
//
//emails
RHMultiStringValue *emailMultiValue = [newPerson emails];
RHMutableMultiStringValue *mutableEmailMultiValue = [emailMultiValue mutableCopy];
mutableEmailMultiValue = [[RHMutableMultiStringValue alloc] initWithType:kABMultiStringPropertyType];
//if the person has multiple emails, iterate through the emails and add them to the RHMutableMultiStringValue
NSInteger index = 0;
for (KBEmail *email in emails) {
//choose the appropriate label based on the number type, here we just assigned a home label.
[mutableEmailMultiValue addValue:email.email withLabel:RHHomeLabel];
}
newPerson.emails = mutableEmailMultiValue;
//
//addresses
RHMultiValue *addressMultiValue = [newPerson addresses];
RHMutableMultiValue *mutableAddressMultiValue = [addressMultiValue mutableCopy];
mutableAddressMultiValue = [[RHMutableMultiValue alloc] initWithType:kABMultiDictionaryPropertyType];
NSInteger index = 0;
//if the person has multiple addresses, iterate through the addresses and add them to the RHMutableMultiValue
for (ATBAddress *address in addresses) {
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
[dict setValue:address.street1 forKey:RHPersonAddressStreetKey];
[dict setValue:address.city forKey:RHPersonAddressCityKey];
[dict setValue:address.state forKey:RHPersonAddressStateKey];
[dict setValue:address.zip forKey:RHPersonAddressZIPKey];
// [dict setValue:[self randomString] forKey:RHPersonAddressCountryKey];
// [dict setValue:@"us" forKey:RHPersonAddressCountryCodeKey]; //must be in 2 letter country code, so assume usa
//choose the appropriate label based on the number type, here we just assigned a home label.
[mutableAddressMultiValue insertValue:dict withLabel:RHHomeLabel atIndex:index];
index++;
}
newPerson.addresses = mutableAddressMultiValue;
//
NSError* error = nil;
if (![addressBook saveWithError:&error]) {
//error saving contact
}
else{
//contact saved
}