Ich habe etwas herumprobiert wie man sich mit Hilfe des “AddressBook“-Frameworks Adressen aus dem Adressbuch herausholen kann. Hier ein kurzes Beispiel welches den Vorgang dokumentiert.
Ich habe ein neues Projekt (view-based Application) angelegt und ein ganz einfaches Interface gestaltet: Auf den View habe ich einen Button angelegt – und wenn man auf diesen klickt wird die Action getAddress: aufgerufen.
So sieht das Header-File meines ViewControllers aus:
#import <UIKit/UIKit.h>
#import <AddressBook/AddressBook.h>
#import <AddressBookUI/AddressBookUI.h>
@interface ABPickerTestViewController : UIViewController <ABPeoplePickerNavigationControllerDelegate> {
}
- (IBAction)getAddress:(id)sender;
Man muss also das Protokoll “ABPeoplePickerNavigationControllerDelegate” implementieren. Hier der komplett Code der .m-Files. Ich erkläre diesen später:
- (IBAction)getAddress:(id)sender {
ABPeoplePickerNavigationController *pp = [[ABPeoplePickerNavigationController alloc] init];
pp.peoplePickerDelegate = self;
[self presentModalViewController:pp animated:YES];
[pp release];
}
#pragma mark -
#pragma mark ABPeoplePickerNavigationControllerDelegate
- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker
shouldContinueAfterSelectingPerson:(ABRecordRef)person
{
// "Normale" Infos
NSString *vorname = (NSString *)ABRecordCopyValue(person, kABPersonFirstNameProperty);
NSString *nachname = (NSString *)ABRecordCopyValue(person, kABPersonLastNameProperty);
NSString *firma = (NSString *)ABRecordCopyValue(person, kABPersonOrganizationProperty);
NSLog(@"Vorname: %@", vorname);
NSLog(@"Nachname: %@", nachname);
NSLog(@"Firma: %@", firma);
[vorname release];
[nachname release];
[firma release];
// Infos zu Telefonnummern
ABMultiValueRef multi = ABRecordCopyValue(person, kABPersonPhoneProperty);
for (CFIndex i = 0; i < ABMultiValueGetCount(multi); i++) {
NSString *telLabel = (NSString *)ABMultiValueCopyLabelAtIndex(multi, i);
NSString *telNr = (NSString *)ABMultiValueCopyValueAtIndex(multi, i);
NSString *typ = @"";
if ([telLabel rangeOfString:@"Work"].location != NSNotFound) {
typ = @"Arbeit";
}
else if ([telLabel rangeOfString:@"Mobile"].location != NSNotFound) {
typ = @"Mobil";
}
else if ([telLabel rangeOfString:@"Home"].location != NSNotFound) {
typ = @"Heim";
}
else {
typ = @"unbekannt";
}
NSLog(@"Label: %@ Nr: %@ Typ: %@", telLabel, telNr, typ);
[telLabel release];
[telNr release];
}
CFRelease(multi);
// Infos zu Adressen
ABMultiValueRef address = ABRecordCopyValue(person, kABPersonAddressProperty);
for (CFIndex i = 0; i < ABMultiValueGetCount(address); i++) {
NSString *addrLabel = (NSString *)ABMultiValueCopyLabelAtIndex(address, i);
NSDictionary *addrDict = (NSDictionary *)ABMultiValueCopyValueAtIndex(address, i);
NSString *strasse = [addrDict objectForKey:@"Street"];
NSString *plz = [addrDict objectForKey:@"ZIP"];
NSString *ort = [addrDict objectForKey:@"City"];
NSLog(@"Label: %@ Str: %@, PLZ: %@, Ort: %@", addrLabel, strasse, plz, ort);
[addrLabel release];
[addrDict release];
}
CFRelease(address);
// ModalViewController dismissen
[self dismissModalViewControllerAnimated:YES];
return NO;
}
- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker
shouldContinueAfterSelectingPerson:(ABRecordRef)person
property:(ABPropertyID)property
identifier:(ABMultiValueIdentifier)identifier
{
return NO;
}
- (void)peoplePickerNavigationControllerDidCancel:(ABPeoplePickerNavigationController *)peoplePicker
{
[self dismissModalViewControllerAnimated:YES];
}
Nehmen wir uns zuerst einmal die Action vor die aufgerufen wird wenn der Button geklickt wird:
- (IBAction)getAddress:(id)sender {
ABPeoplePickerNavigationController *pp = [[ABPeoplePickerNavigationController alloc] init];
pp.peoplePickerDelegate = self;
[self presentModalViewController:pp animated:YES];
[pp release];
}
Hier wird zuerst ein Objekt initialisiert welches im Framework “AddressBookUI” vorkommt, nämlich den “ABPeoplePickerNavigationController“. Ihr kennt diesen Controller, das ist der gleiche wenn man das Adressbuch öffnet und in den Kontakten sucht.
Diesem Controller wird ein Delegate bekannt gemacht, der Einfachheit halber nehmen wir das gleiche Objekt (self), im Interface haben wir ja auch gesagt dass das Protokoll “ABPeoplePickerNavigationControllerDelegate” eingehalten wird.
Zuguterletzt wird der Controller als “ModalView” angezeigt.
Das Verhalten des “ABPeoplePickerNavigationController” richtet sich nach den Delegate-Methoden welche wir eingeführt haben.
Fangen wir mit den einfachsten an:
- (void)peoplePickerNavigationControllerDidCancel:(ABPeoplePickerNavigationController *)peoplePicker
{
[self dismissModalViewControllerAnimated:YES];
}
Diese Delegate-Methode wird aufgerufen wenn der “Abbruch” Button geklickt wird, in unserem Falle entfernen wir einfach den ModalView.
- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker
shouldContinueAfterSelectingPerson:(ABRecordRef)person
property:(ABPropertyID)property
identifier:(ABMultiValueIdentifier)identifier
{
return NO;
}
Diese Delegate Methode wird aufgerufen wenn der User eine der User-Eigenschaften anklickt. In unserem Fall tritt dies nicht ein da die nächste Delegate-Methode gar nicht zulässt dass man in einen Benutzer reinspringt:
- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker
shouldContinueAfterSelectingPerson:(ABRecordRef)person
{
// Jede Menge Code hier, s. oben
// ModalViewController dismissen
[self dismissModalViewControllerAnimated:YES];
return NO;
}
Diese Delegate-Methode wird aufgerufen sobald man in der Kontakt-Liste einen Kontakt anklickt. Die Rückgabe NO gibt an dass nicht in den Benutzer reingesprungen wird sondern er direkt ausgewählt wird. Im Quellcode sind einige Beispiele aufgeführt wie man an Informationen des Eintrags kommt, ich hoffe Ihr könnt damit was anfangen