停止检测数字并将它们作为链接。因此,当您按下链接(数字)时,它将带您进入该shouldStartLoadWithRequest
方法。
下面的代码应该有助于我评论详细说明每行的功能,如果您需要其他任何内容,只需询问即可。
-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request
navigationType:(UIWebViewNavigationType)navigationType
{
static NSString *urlPrefix = @"tel://";
NSString *url = [[request url] absoluteString]; // Notice that we are getting the obsoluteString of the url
if([url hasPrefix:urlPrefix]) { // We then check that the url has a prefix of our urlPrefix otherwise why bother doing anything at all.
if([[UIApplication sharedApplication] canOpenUrl:url]) { // This is to check that we can actually open a url as iPads can't make phone calls.
[[UIApplication sharedApplication] openUrl:url]; // And if everything is successful we are good to make the phone call.
return NO; // We don't want the UIWebView to go navigating somewhere crazy so tell it to stop navigating away.
} else {
return NO; // If it does contain the prefix but we can't open the url we don't want to navigate away so return NO.
}
}
return YES; // If all else fails it most be a standard request so return YES.
}
该代码将在如下链接上运行:
<p>Call us on:<a href="tel://12345678900">12345678900</a></p>
更新
我刚刚意识到你没有设置你webView
的代表。所以在你的 .h 文件中确保你有:
@interface MyClassName : MySuperClass <UIWebViewDelegate> // Obviously 'MyClassName' and MySuperClass' you need to replace with your classes.
然后在UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(0,100,1024,768)];
您需要执行[webView setDelegate:self];
此操作后对其进行设置,以便它应该使用委托方法。
如果您还有任何问题,请发表评论。