How can I open an external link in Safari not the app's UIWebView?

后端 未结 10 1435
甜味超标
甜味超标 2020-11-28 10:47

I have a Phonegap (cordova) application where I want to load some external webpages within the phonegap WebView and I have other external webpages that I want to load in saf

10条回答
  •  情书的邮戳
    2020-11-28 11:00

    1. Add target="_blank" to your links. ie:

      
      
    2. Make sure access has an origin of * /> in your config.xml (make sure its the one in the root of the app directory, above the www folder. ie:

      
      
    3. Add the following code to MainViewController.m

      - (BOOL)webView:(UIWebView *)theWebView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
      {
          NSURL *url = [request URL];
      
          // Intercept the external http requests and forward to Safari.app
          // Otherwise forward to the PhoneGap WebView
          if ([[url scheme] isEqualToString:@"http"] || [[url scheme] isEqualToString:@"https"]) {
              [[UIApplication sharedApplication] openURL:url];
              return NO;
          }
          else {
              return [ super webView:theWebView shouldStartLoadWithRequest:request navigationType:navigationType ];
          }
      } 
      

    I made a quick video explaining how to fix this issue:

    http://www.youtube.com/watch?v=zqbjXSnAR-Q&feature=c4-overview&list=UUefS6KLvFQVjhmL6hiBq4Sg

    Hope it helps!

提交回复
热议问题