3

是否有可能在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当我点击数字时,没有调用任何方法。

我只需要得到检测到的号码。

4

2 回答 2

1

停止检测数字并将它们作为链接。因此,当您按下链接(数字)时,它将带您进入该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-06T15:09:06.473 回答
-1

使用以下委托方法检测电话号码:

-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request
                                            navigationType:(UIWebViewNavigationType)navigationType
{
     if ([url.scheme isEqualToString:@"tel"])
     {
         [[UIApplication sharedApplication] openURL:url];
     }
}

这是参考线程:UIWebView 不检测电话号码链接

于 2013-12-06T13:29:36.540 回答