What I would do to start is create a Person
class (subclass of NSObject
) that also implements the MKAnnotation
protocol. You could create two separate classes ("Person" and "PersonAnnotation") if you wanted to but it's not necessary.
In the Person
class, you could either declare your own person-related properties such as First Name, Last Name, Email Address, etc. or you could just have an ABRecordRef
ivar and let it store the individual fields for you.
I would only create an ABPerson
record when I want to actually show the ABPersonViewController
to keep the AB-specific code isolated and to manage the ABPerson
record's creation and release more easily. Regardless, just creating an ABRecordRef
does not add it to the address book. As the ABPerson Reference documentation says:
Person records don’t necessarily have to be stored in the Address Book
database. You can use person records as a way to group contact
information in memory and present it to the user through, for example,
a person view controller (ABPersonViewController).
So the Person
class could look like this:
@interface Person : NSObject<MKAnnotation>
@property (nonatomic, copy) NSString *firstName;
@property (nonatomic, copy) NSString *lastName;
@property (nonatomic, copy) NSString *emailAddress;
@property (nonatomic, assign) CLLocationCoordinate2D coordinate;
@end
@implementation Person
@synthesize firstName;
@synthesize lastName;
@synthesize emailAddress;
@synthesize coordinate;
-(NSString *)title
{
return [NSString stringWithFormat:@"%@ %@", firstName, lastName];
}
-(NSString *)subtitle
{
return emailAddress;
}
@end
The web service class would create an instance of Person
and set the properties. Then the map view class would add that instance directly to the map (since Person already implements MKAnnotation):
[mapView addAnnotation:person];
When the pin is tapped, the map view will call the didSelectAnnotationView
delegate method. Or, you could add a disclosure button to the annotation's callout in viewForAnnotation
and respond to that in the calloutAccessoryControlTapped
delegate method.
Whichever action method you decide to use, in that method you could then create an ABRecordRef
, set its values from the annotation object, and then show the ABPersonViewController
. In both delegate methods, the Person
annotation object could be retrieved using:
Person *personTapped = (Person *)view.annotation;