开发者

MKAnnotation: Clicking on the text in MKAnnotation info view should call a number

开发者 https://www.devze.com 2022-12-12 15:29 出处:网络
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 an

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消