Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Force a WebView link to launch Safari?

I have a UIWebView embedded within an iPhone app of mine. I want to be able to have certain links within that webview open into the full Mobile Safari app (i.e. not my embedded version of it).

Is there a simple way to structure some of my hrefs to force this, instead of every link opening within my embedded webview?

Thanks.

like image 811
tbacos Avatar asked Mar 28 '10 09:03

tbacos


People also ask

How do I make a link open in Safari?

Right-click the link and select Open Link in New Tab. This is how you can take control of the links you open in Safari. Open them in new tabs, switch right to them or open them in the background and use shortcuts. It's all in your hands!

Does Webview share cookies with Safari?

Cookies do appear to be shared between Safari and WebViews.

Which browser is used in iOS Webview?

Which browser iOS use in their webview? Safari browser installed in iOS & webview browser (if safari) are same or there are difference?


2 Answers

To expand upon what Randy said, this is what I use in my application to make every http://, https://, and mailto:// URL open in the external Safari or Mail applications:

-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType;  {     NSURL *requestURL =[ [ request URL ] retain ];      if ( ( [ [ requestURL scheme ] isEqualToString: @"http" ] || [ [ requestURL scheme ] isEqualToString: @"https" ] || [ [ requestURL scheme ] isEqualToString: @"mailto" ])          && ( navigationType == UIWebViewNavigationTypeLinkClicked ) ) {          return ![ [ UIApplication sharedApplication ] openURL: [ requestURL autorelease ] ];      }      [ requestURL release ];      return YES;  } 

As Randy says, you'll want to implement this within whatever class you set to be the delegate of the UIWebView. To have only select URLs launch Safari, you could change their scheme from http:// to safari://, or something similar, and only kick those URLs off to the system (after replacing the custom URL scheme with http://).

I do this within my internal help documentation, which is HTML displayed in a UIWebView, so that I don't run into issues in the review process with having a general-purpose web browser embedded in my application.

like image 102
Brad Larson Avatar answered Oct 11 '22 11:10

Brad Larson


Ok I got it. Maybe its not the perfect solution, but you can do it like this:

Only in your WebViewController.m:

add the line webView.delegate = self; to the viewDidLoad procedure:

- (void)viewDidLoad {     webView.delegate = self;     .... your code .... } 

Then you can add as described above somewhere in the Controller.m File following boolean resulting function:

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {     if (navigationType == UIWebViewNavigationTypeLinkClicked) {         [[UIApplication sharedApplication] openURL:request.URL];         return false;     }     return true; } 
like image 43
aqm Avatar answered Oct 11 '22 10:10

aqm