Created
February 8, 2010 05:07
-
-
Save atr000/297888 to your computer and use it in GitHub Desktop.
absearch.m
This file contains hidden or 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
#import <Foundation/Foundation.h> | |
#import <AddressBook/AddressBook.h> | |
int main (int argc, const char * argv[]) { | |
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; | |
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults]; | |
NSString *phoneSearchString = [userDefaults stringForKey:@"phone"]; | |
if (!phoneSearchString) { | |
printf("Error: Missing phone search string (-phone)\n"); | |
return 1; | |
} | |
ABAddressBook *addressBook = [ABAddressBook sharedAddressBook]; | |
ABSearchElement *phoneNumberContainsNumbers = [ABPerson searchElementForProperty:kABPhoneProperty | |
label:nil | |
key:nil | |
value:phoneSearchString | |
comparison:kABContainsSubStringCaseInsensitive]; | |
NSArray *peopleFound =[addressBook recordsMatchingSearchElement:phoneNumberContainsNumbers]; | |
NSEnumerator *enumerator = [peopleFound objectEnumerator]; | |
ABPerson *person; | |
while (person = [enumerator nextObject]) { | |
NSString *personFirstName = [person valueForProperty:kABFirstNameProperty]; | |
NSString *personLastName = [person valueForProperty:kABLastNameProperty]; | |
NSString *personCompanyName = [person valueForProperty:kABOrganizationProperty]; | |
NSString *personEntry; | |
if (personFirstName && personLastName && personCompanyName) { | |
personEntry = [NSString stringWithFormat:@"%@\t%@, %@ (%@)\n", [person uniqueId], personLastName, personFirstName, personCompanyName]; | |
} else if (!personFirstName && personLastName && personCompanyName) { | |
personEntry = [NSString stringWithFormat:@"%@\t%@ (%@)\n", [person uniqueId], personLastName, personCompanyName]; | |
} else if (!personFirstName && !personLastName && personCompanyName) { | |
personEntry = [NSString stringWithFormat:@"%@\t%@\n", [person uniqueId], personCompanyName]; | |
} else if (!personFirstName && !personLastName && !personCompanyName) { | |
personEntry = [NSString stringWithFormat:@"%@\t%@\n", [person uniqueId], [NSString stringWithFormat:@"Unknown name"]]; | |
} else if (!personFirstName && personLastName && !personCompanyName) { | |
personEntry = [NSString stringWithFormat:@"%@\t%@\n", [person uniqueId], personLastName]; | |
} else if (personFirstName && !personLastName && !personCompanyName) { | |
personEntry = [NSString stringWithFormat:@"%@\t%@\n", [person uniqueId], personFirstName]; | |
} else if (personFirstName && !personLastName && personCompanyName) { | |
personEntry = [NSString stringWithFormat:@"%@\t%@ (%@)\n", [person uniqueId], personFirstName, personCompanyName]; | |
} | |
printf("%s", [personEntry UTF8String]); | |
} | |
[pool drain]; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment