How to handle app URLs in a UIWebView?

前端 未结 3 1706
既然无缘
既然无缘 2020-12-04 07:08

I recently found that my UIWebView was choking on ITMS links. Specifically, from the UIWebView in my app, if I navigate to a site such as this one and click the \"Available

3条回答
  •  猫巷女王i
    2020-12-04 07:43

    Starting with Theory's code, examine the URL for "itms" scheme(s) (this method can be called multiple times due to redirects). Once you see an "itms" scheme, stop the webView from loading and open the URL with Safari. My WebView happens to be in a NavigationController, so I pop out of that after opening Safari (less flashing).

    - (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request 
      navigationType:(UIWebViewNavigationType)navigationType 
    {
        if ([[[request URL] scheme] isEqualToString:@"itms-apps"]) {
            [webView stopLoading];
            [[UIApplication sharedApplication] openURL:[request URL]];
            [self.navigationController popViewControllerAnimated:YES];
            return NO;
        } else {
            return YES;
        }
    }
    

提交回复
热议问题