Using iPhone contact data


/ Published in: Objective C
Save to your folder(s)

Function to retrieve contact data from iPhone.


Copy this code and paste it in your HTML
  1. -(void)retrieveContactList
  2. {
  3. ABAddressBookRef myAddressBook = ABAddressBookCreate();
  4. NSArray *allPeople = (NSArray *)ABAddressBookCopyArrayOfAllPeople(myAddressBook);
  5. contactList = [[NSMutableArray alloc]initWithCapacity:[allPeople count]];
  6. for (id record in allPeople) {
  7. CFTypeRef phoneProperty = ABRecordCopyValue((ABRecordRef)record, kABPersonPhoneProperty);
  8. NSArray *phones = (NSArray *)ABMultiValueCopyArrayOfAllValues(phoneProperty);
  9. //NSLog(@"phones array: %@", phones);
  10. CFRelease(phoneProperty);
  11. NSString* contactName = (NSString *)ABRecordCopyCompositeName((ABRecordRef)record);
  12.  
  13. NSMutableDictionary *newRecord = [[NSMutableDictionary alloc] init];
  14. [newRecord setObject:contactName forKey:@"name"];
  15. //[contactName release];
  16. NSMutableString *newPhone = [[NSMutableString alloc] init];
  17. for (NSString *phone in phones) {
  18. //NSString *fieldData = [NSString stringWithFormat:@"%@: %@", contactName, phone];
  19. if(![newPhone isEqualToString:@""])
  20. [newPhone appendString:@", "];
  21. [newPhone appendString:phone];
  22.  
  23. }
  24. [newRecord setObject:newPhone forKey:@"phone"];
  25. [newPhone release];
  26. [phones release];
  27. [contactList addObject:newRecord];
  28. //[newPhone release];
  29. }
  30. CFRelease(myAddressBook);
  31. // NSLog(@"Final data: %@", contactList);
  32. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.