Posts Tagged ‘addressbook’

iPhone OS SDK – Contact Address Property

March 17th, 2010 iPhone SDK Tips

Today something quick. I’m working with addressbook information and I am extracting data from a contact, I already know I want in the addressbook (addressbookID=45 lets say).
How do I get their address?
And their Name, Company, Phone Number and Email!


Use the following.

NSLog(@”Looking Up Contact Now”);
ABAddressBookRef ab = ABAddressBookCreate();
ABRecordRef person = ABAddressBookGetPersonWithRecordID(ab,item2.integerValue);

NSLog(@”person: %d”,person);
if (person != nil) {

//Name
myName = (NSString *)ABRecordCopyCompositeName(person);

//Company
myCompany = (NSString *)ABRecordCopyValue(person, kABPersonOrganizationProperty);

//Email Address (this one is tricky)
ABMutableMultiValueRef emailMulti = ABRecordCopyValue(person, kABPersonEmailProperty);
NSMutableArray *emails = [[NSMutableArray alloc] init];
for (int i = 0; i < ABMultiValueGetCount(emailMulti); i++) {
NSString *anEmail = [(NSString*)ABMultiValueCopyValueAtIndex(emailMulti, i) autorelease];
[emails
addObject:anEmail];
}


if([emails count] > 0) myEmail = [emails objectAtIndex:0];

//Phone Numbers
ABMultiValueRef phoneNumberProperty = ABRecordCopyValue(person, kABPersonPhoneProperty);
NSArray* phoneNumbers = (NSArray*)ABMultiValueCopyArrayOfAllValues(phoneNumberProperty);
CFRelease(phoneNumberProperty);

// Do whatever you want with the phone numbers
NSLog(@”Phone numbers = %@”, phoneNumbers);

//Location Address
ABMutableMultiValueRef addressMulti = ABRecordCopyValue(person, kABPersonAddressProperty);
NSMutableArray *address = [[NSMutableArray alloc] init];
int i;
for (i = 0; i < ABMultiValueGetCount(addressMulti); i++) {
NSString *city = [(NSString*)ABMultiValueCopyValueAtIndex(addressMulti, i) autorelease];
[address
addObject:city];
}
NSLog(@”addresses: %@”,address);

} else {
UIAlertView *baseAlert = [[UIAlertView alloc] initWithTitle:@”Address Link” message:@”Sorry, no address book link available” delegate:self cancelButtonTitle:nil otherButtonTitles:@”OK”, nil];
[baseAlert
show];
}

iPhoneOS Addressbook Create New Client & Get Data

March 5th, 2010 iPhone SDK Tips


I just finished a snippet of code for an upcoming 1 day iphonedev project which is very simple. The snippet needs to show the user an add address book contact box, let the user add the contact info and then Dismiss the add contact window to get on with the rest of the app, and of course saving the data into memory.
The code is attached below, and be sure to be watching the NSLog.




iPhone Development SDK – Working with the Address BookRight

January 26th, 2010 iPhone SDK Tips

Lately I’ve spent a bit of time learning the API for the iPhone Contacts Address Book. I’d like to share the process and help with some of the bits I found difficult.

Including the Frameworks

First the framework must be included in your Xcode project.

  1. Right Click on your project target and in the linked  libraries add AddressBook.framework, and AddressBookUI.framework
  2. To add these, click the + button at the bottom of the properties box. In my case, the AddressbookUI.framework was not in the list. To add it I clicked ‘Add Other’ and navigated to /Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS2.2.1.sdk/System/Library/Frameworks. AddressBookUI.framework is in here. (if you don’t have folder 2.2.1, try any of the others).
  3. If done correctly, your project should look like the one below. (more…)