首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在UIWebView中获取检测到的号码

如何在UIWebView中获取检测到的号码
EN

Stack Overflow用户
提问于 2013-12-06 19:35:18
回答 2查看 2.4K关注 0票数 3

是否有可能在UIWebView中检测到号码。

当我在我的iPad上点击UIWebView中的一个数字时,弹出窗口显示以下选项:

发送消息、添加到联系人、复制。

如何删除弹出窗口并获取检测到号码?

代码语言:javascript
复制
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方法被调用。

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

EN

回答 2

Stack Overflow用户

发布于 2013-12-06 23:09:06

停止检测数字,而将它们作为链接。因此,当您按下链接(数字)时,它将带您进入shouldStartLoadWithRequest方法。

下面的代码应该对我有帮助,我已经详细说明了每一行做了什么,如果你需要任何其他东西,尽管问。

代码语言:javascript
复制
-(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.
}

代码将在如下链接上工作:

代码语言:javascript
复制
<p>Call us on:<a href="tel://12345678900">12345678900</a></p>

更新

我刚刚意识到你没有设置你的webView的委托。因此,在.h文件中,请确保您拥有:

代码语言:javascript
复制
@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];,这将设置它,所以它应该使用委托方法。

如果你有更多的问题,请留言。

票数 1
EN

Stack Overflow用户

发布于 2013-12-06 21:29:37

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

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

下面是引用线程:UIWebView doesn't detect phone number links

票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20422880

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档