Created
January 16, 2010 01:53
-
-
Save joepestro/278587 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Create address book reference | |
ABAddressBookRef addressBook = ABAddressBookCreate(); | |
CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(addressBook); | |
CFIndex nPeople = ABAddressBookGetPersonCount(addressBook); | |
// Pull all records out of address book | |
NSMutableArray* users = [[NSMutableArray alloc] init]; | |
for (int i = 0; i < nPeople; i++) { | |
ABRecordRef ref = CFArrayGetValueAtIndex(allPeople, i); | |
CFStringRef firstName = ABRecordCopyValue(ref, kABPersonFirstNameProperty); | |
CFStringRef lastName = ABRecordCopyValue(ref, kABPersonLastNameProperty); | |
NSString* contactFirstLast = [NSString stringWithFormat:@"%@ %@", firstName, lastName]; | |
[users addObject:contactFirstLast]; | |
} | |
// Set up array to hold friends | |
NSError *error = nil; | |
NSMutableArray* friends = [[[NSMutableArray alloc] init] autorelease]; | |
// Generate query string | |
NSMutableDictionary* friendsQuery = [[NSMutableDictionary alloc] init]; | |
int i = 0; | |
for (NSString* user in users) { | |
NSString* key = [[NSString alloc] initWithFormat:@"friend[%i]", i]; | |
user = [user stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; | |
[user replaceOccurrencesOfString:@" " withString:@"+" options:NSCaseInsensitiveSearch range:NSMakeRange(0, [user length])]; | |
[user replaceOccurrencesOfString:@"'" withString:@"" options:NSCaseInsensitiveSearch range:NSMakeRange(0, [user length])]; | |
[friendsQuery setObject:user forKey:key]; | |
i++; | |
if (i >= 100) { | |
[friends addObjectsFromArray:[Friend findRemote:[@"" stringByAddingQueryDictionary:friendsQuery] withResponse:&error]]; | |
[friendsQuery release]; | |
friendsQuery = [[NSMutableDictionary alloc] init]; | |
i = 0; | |
} | |
} | |
[friends addObjectsFromArray:[Friend findRemote:[@"" stringByAddingQueryDictionary:friendsQuery] withResponse:&error]]; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment