是否有可能在UIWebView中检测到号码。
当我在我的iPad上点击UIWebView中的一个数字时,弹出窗口显示以下选项:
发送消息、添加到联系人、复制。
如何删除弹出窗口并获取检测到号码?
UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(0,100,1024,768)];
webView.dataDetectorTypes = UIDataDetectorTypePhoneNumber;
NSURL *url = [NSURL URLWithString:@"somepage"];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
[webView loadRequest:urlRequest]; UIWebView会检测电话号码,当我点击号码时,系统会显示弹出窗口。
有趣的是,当我点击数字时,没有一个UIWebViewDelegate方法被调用。
我只需要得到检测到的号码。
发布于 2013-12-06 23:09:06
停止检测数字,而将它们作为链接。因此,当您按下链接(数字)时,它将带您进入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];,这将设置它,所以它应该使用委托方法。
如果你有更多的问题,请留言。
发布于 2013-12-06 21:29:37
使用以下委托方法检测电话号码:
-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request
navigationType:(UIWebViewNavigationType)navigationType
{
if ([url.scheme isEqualToString:@"tel"])
{
[[UIApplication sharedApplication] openURL:url];
}
}https://stackoverflow.com/questions/20422880
复制相似问题