Search code examples
iosuiwebviewphone-call

How can I know which button Index of UIAlertView is clicked when I use UIWebview to make a call


I am able to generate an alert allowing the user to make a call using a UIWebView with this code:

UIWebView *webV= [[UIWebView alloc] init];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"tel:1834563578"]];
[webV loadRequest:request];

However, when I use UIWebView to make a call, the system presents an alert. I want to know which button index the user selected. I don't know how to retrieve the index of the option the user chooses in the alert.


Solution

  • Just for iOS10+.

    You can detect a call in UIWebViewDelegate:

    - (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
        if ([request.URL.absoluteString hasPrefix:@"tel:"]) {
            // make a call
    
            // this method is available in iOS10
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:phoneNumber] 
                                               options:nil 
                                               completionHandler:^(BOOL success) {
                // if success is NO, user tap cancel button, or other
                NSLog(@"%d", success);
            }];
    
            return NO; // the default view will not show
        }
        return YES;
    }
    

    Set up UIWebView:

    UIWebView *webV= [[UIWebView alloc] init];
    webV.delegate = self;
    NSURLRequest *request = [NSURLRequest requestWithURL: [NSURL URLWithString:@"tel:1834563578"]];
    [webV loadRequest: request];
    

    By this, you will know which button tapped depend on success in completionHandler.