How to get detected number in UIWebView

2.5k Views Asked by At

is there a possibility to get number detected in UIWebView.

When I click a number inside UIWebView on my iPad I get popover with following choices:

Send Message, Add to Contacts, Copy.

How can I remove that popover and get detected number?

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 detects phone numbers and when I click on number, system shows me popover.

Interesting thing is that none of the UIWebViewDelegate methods is called when I tap on the number.

I only need to get detected number.

2

There are 2 best solutions below

4
On

Stop detecting numbers and have them as links instead. So when you press the link (Number) it will take you into the shouldStartLoadWithRequest method.

The code below should help I have commented to detail what each line does if you need anything else just ask.

-(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.
}

The code will work on a link like:

<p>Call us on:<a href="tel://12345678900">12345678900</a></p>

UPDATE

I have just realized that you aren't setting your webViews delegate. So in your .h file make sure you have:

@interface MyClassName : MySuperClass <UIWebViewDelegate> // Obviously 'MyClassName' and MySuperClass' you need to replace with your classes.

then after UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(0,100,1024,768)]; you need to do [webView setDelegate:self]; this will set it up so it should use the delegate methods.

If you have any more issues just leave a comment.

0
On

Use following delegate method to detect phone number :

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

Here is the reference thread : UIWebView doesn't detect phone number links