I have an small embedded UIWebView for my about-section of the app. When the user taps a link in there, the link opens in that small embedded UIWebView which of course has no navigation controls and isn't full-screen. I don't like that. Can I force it to open links with Safari instead?
+1
A:
You can implement the shouldStartLoadWithRequest
method of the UIWebViewDelegate protocol to intercept link clicks. Inside that method you can use UIApplication's openURL
method to cause the url to be opened in the appropriate application (ie. Safari for HTTP/HTTPS).
- (BOOL) webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
NSURL *url = [request URL];
if ([url isEqual:YOUR_HOME_URL_CONTSTANT])
return YES;
[[UIApplication sharedApplication] openURL:url];
return NO;
}
codelark
2010-10-19 21:21:49
When I implement this, the UIWebView doesn't even launch the local content. It remains empty... strange thing. What can cause this? Does this also affect the [v loadRequest:request] method?
BugAlert
2010-10-19 21:31:31
In this code you need to check the URL to see if it should be loaded in safari. For example, maybe you want everything starting with file: to be loaded in the web view in which case this method needs to return YES. My guess is that openURL won't open a file url, and because NO gets returned the webview won't load it either.
Nimrod
2010-10-19 21:34:21
Yeah, in the code snippet I provided, ALL load requests get pushed to the external app. If you want to load specific content differently, you can check the attributes of the URL in whatever manner to determine whether to load locally or remotely. The return value BOOL specifies whether or not the webView should continue and attempt to load the content itself. I'll update the example with a naive implementation.
codelark
2010-10-19 22:20:26