MKAnnotation: Clicking on the text in MKAnnotation info view should call a number
I am using a MKMapview to display a map in my native iPhone application and I add two markers to the map v开发者_运维问答iew. On clicking the annotation marker I want a phone number to be displayed and clicking on the phone number should make a call to that number.
How can I do this ?
The way this works is: you provide a "tel:" URL for your application to open. This calls the Phone application, which automatically starts dialling the provided number. To do this by clicking on an annotation callout, you need to provide a mapView:annotationView:calloutAccessoryControlTapped:
method in your MKMapViewDelegate
.
If, for example, the phone that you wish to dial is the annotation's title, this is how you'd do it:
- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control {
NSString *phoneNo = view.annotation.title;
NSString *telString = [NSString stringWithFormat:@"tel:%@", phoneNo];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:telString]];
}
In practice, you would also need to check whether the current device is indeed a phone (i.e. not an iPod touch or an iPad). It would also be good to let the user know that they are about to make a phone call, prior to launching the Phone app. E.g. show them an action sheet that allows them to decide whether to make the call or cancel.
精彩评论