iOS engineer who likes clean, simple designs and sans-serif.

Loading

Week 1 Day 1 – Saving to Address Book

We did a lot of Git + Github today, which I have used before. We also built a simple “Profile app” (to introduce ourselves) using a Tab Bar View Controller. I gave the app a little Front-End touch and for extra credit, we learned a bit about working with Address Book (YIKES!). I’m kind of proud that my partner Dare and I were able to hack through the C code to get it to work in such a short amount of time. Here’s a snippet of the most interesting code for future reference:

- (IBAction)addContactButtonPressed:(UIBarButtonItem *)sender {
   
    CFErrorRef error = NULL;
    ABAddressBookRef newBook = ABAddressBookCreateWithOptions(NULL, NULL);
   
    if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusNotDetermined) {
        ABAddressBookRequestAccessWithCompletion(newBook, ^(bool granted, CFErrorRef error) {
            // First time access has been granted, add the contact
            [self addContactTo:newBook withError:error];
        });
    }
    else if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusAuthorized) {
        // The user has previously given access, add the contact
        [self addContactTo:newBook withError:error];
    }
    else {
        // The user has previously denied access
        // Send an alert telling user to change privacy setting in settings app
    }
   
}

- (void)addContactTo:(ABAddressBookRef)addressBook withError:(CFErrorRef)error
{
    ABRecordRef newPerson = ABPersonCreate();
   
    // Add First and Last Name
    ABRecordSetValue(newPerson, kABPersonFirstNameProperty, (__bridge CFTypeRef)(self.firstName), &error);
    ABRecordSetValue(newPerson, kABPersonLastNameProperty, (__bridge CFTypeRef)(self.lastName), &error);
   
    // Add Twitter Username
    ABMultiValueRef social = ABMultiValueCreateMutable(kABMultiDictionaryPropertyType);
    ABMultiValueAddValueAndLabel(social, (__bridge CFTypeRef)([NSDictionary dictionaryWithObjectsAndKeys:
                                                               (NSString *)kABPersonSocialProfileServiceTwitter, kABPersonSocialProfileServiceKey,
                                                               self.twitterURL, kABPersonSocialProfileUsernameKey,
                                                               nil]), kABPersonSocialProfileServiceTwitter, NULL);
   
    ABRecordSetValue(newPerson, kABPersonSocialProfileProperty, social, NULL);
    CFRelease(social);
   
    // Add Email Address
    ABMutableMultiValueRef emailMultiValue = ABMultiValueCreateMutable(kABMultiStringPropertyType);
    ABMultiValueAddValueAndLabel(emailMultiValue, (__bridge CFStringRef) self.emailAddress, kABHomeLabel, NULL);
    ABRecordSetValue(newPerson, kABPersonEmailProperty, emailMultiValue, nil);
    CFRelease(emailMultiValue);
   
    // Add Phone Number
    ABMutableMultiValueRef phoneMultiValue = ABMultiValueCreateMutable(kABMultiStringPropertyType);
    ABMultiValueAddValueAndLabel(phoneMultiValue, CFBridgingRetain(self.phoneNumber), kABPersonPhoneMobileLabel, NULL);
    ABRecordSetValue(newPerson, kABPersonPhoneProperty, phoneMultiValue, nil);
    CFRelease(phoneMultiValue);
   
    // Save Address Book Person
    ABAddressBookAddRecord(addressBook, newPerson, &error);
    ABAddressBookSave(addressBook, &error);
    CFRelease(newPerson);
    CFRelease(addressBook);

   
    if (error != NULL)
    {
        CFStringRef errorDesc = CFErrorCopyDescription(error);
        NSLog(@"Contact not saved: %@", errorDesc);
        CFRelease(errorDesc);
    }
    if (!error)
    {
       
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Contact Added" message:@"Contact has been added to the address book" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
        [alert show];

    }
}

Good references:

[ Allow Access To Contacts alert to popup.]
Can’t Access Contacts Sources on Device in iOS 6
Programmatically Request Access to Contacts

[ Accessing Address Book ]
Address Book tutorial for iPhone

[ Adding Info to Address Book ]
Simplest way to add twitter account to ABRecordRef (person)
iOS – add contact into Contacts?
how to add new contact to iphone addressbook?

[ Launching Safari with URL ]
How to launch safari and open URL from iOS app

Leave a Comment