Last Updated: March 07, 2016
·
740
· brandonskane

RHAddressBook - Saving a Contact

I recently had to create and save contacts in an iOS app, and preferred to use an Objective-C library to interact with the AddressBook -- I ended up using RHAddressBook.

While the documentation was helpful, I could have used an example, specifically when it came time to save addresses and social handles. So here is an example I came up with of how to save a contact with RHAddressBook (includes saving multiple phones, emails, social handles and physical addresses).

//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
}