Skip to content

Instantly share code, notes, and snippets.

@shoota
Created March 6, 2015 06:28
Show Gist options
  • Save shoota/a5c1e0c6cdaa262f75c4 to your computer and use it in GitHub Desktop.
Save shoota/a5c1e0c6cdaa262f75c4 to your computer and use it in GitHub Desktop.
Copy Contacts (name, phone, phoneLabel) from AddressBook with Record ID.
+ (NSMutableDictionary *)getAddressWithID:(NSNumber *)recordId {
NSMutableDictionary *address = [NSMutableDictionary dictionary];
ABAddressBookRef ab = ABAddressBookCreateWithOptions(NULL, NULL);
ABRecordRef person = ABAddressBookGetPersonWithRecordID(ab, (ABRecordID)[recordId intValue]);
// 名前
NSString *firstName = (__bridge_transfer NSString *)(ABRecordCopyValue(person, kABPersonFirstNameProperty));
NSString *lastName = (__bridge_transfer NSString *)(ABRecordCopyValue(person, kABPersonLastNameProperty));
ABMultiValueRef phoneRef = ABRecordCopyValue(person, kABPersonPhoneProperty);
NSMutableArray *phones = [NSMutableArray array];
for (CFIndex j = 0; j < ABMultiValueGetCount(phoneRef); j++) {
NSMutableDictionary *phoneSet = [NSMutableDictionary dictionary];
CFStringRef phoneNumberRef = ABMultiValueCopyValueAtIndex(phoneRef, j);
CFStringRef locLableRef = ABMultiValueCopyLabelAtIndex(phoneRef, j);
NSString *phoneNumber = (__bridge_transfer NSString *) phoneNumberRef;
NSString *phoneLabel = (__bridge_transfer NSString *) ABAddressBookCopyLocalizedLabel(locLableRef);
LogD(@"number -- %@, label -- %@", phoneNumber, phoneLabel);
[phoneSet setObject:phoneNumber forKey:kABPersonKeyPhoneNumber];
[phoneSet setObject:phoneLabel forKey:kABPersonKeyPhoneLabel];
[phones addObject:phoneSet];
CFRelease(locLableRef);
}
if ([phones count] > 0) {
[address setObject:phones forKey:kABPersonKeyPhone];
}
if (firstName) {
[address setObject:firstName forKey:kABKeyFirstName];
}
if (lastName) {
[address setObject:lastName forKey:kABKeyLastName];
}
CFRelease(phoneRef);
CFRelease(ab);
return address;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment