Skip to content

Instantly share code, notes, and snippets.

@robcolburn
Created June 12, 2012 19:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save robcolburn/2919562 to your computer and use it in GitHub Desktop.
Save robcolburn/2919562 to your computer and use it in GitHub Desktop.
How to open only some urls in WebView
/*
From @craigpfau - http://craigpfau.com/2012/02/phonegap-ios-uiwebview-and-safari-app-links/
To just enable UIWebView for the single URL I used this code [projectFolder > Classes > MainViewController.m]
*/
- (BOOL) webView:(UIWebView*)theWebView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType
{
NSURL *url = request.URL;
if ([url.absoluteString rangeOfString:@"URLToOpenInUIWebView.com"].location != NSNotFound) {
return YES;
}
else {
return [super webView:theWebView shouldStartLoadWithRequest:request navigationType:navigationType];
}
}
@nate8684
Copy link

This isn't working for me, I have cordova 1.7, which I know this is for 1.8 but I really don't want to go through the hassle of upgrading just to find out it still doesn't work. I'm getting the following warning, wondered if you might be able to shed some light on this.

'NSObject' may not respond to 'webView:shouldStartLoadWithRequest:navigationType:'

I'm trying to enable this piece of code for vimeo.com but even when I have a plain link to vimeo, it still launches safari...any thoughts? Let me know, thanks!

@robcolburn
Copy link
Author

robcolburn commented Jun 14, 2012 via email

@nate8684
Copy link

Hey - I tried using Craigs old script and that didn't work. So then I updated cordova to 8.1 and now I am getting these warnings:

'UIViewController' may not respond to 'webView.shouldStartLoadWithRequest:navigationType:

I'm really racking my brain, tried pretty much everything imaginable to fix this issue. Any help you can provide would be GREATLY appreciated. Thanks!

@robcolburn
Copy link
Author

Odd man, not sure. I'd try this.

  1. Fire up X-Code, and make a fresh PhoneGap in the normal way. As you normally would, set the whitelist to *, keep that option to open all whitelisted links in view turned off..
  2. Set up an index with some trivial content.
<body>
  <p><a href="http://www.google.com" target="_blank">1. Googe</a></p>
  <p><a href="http://www.yahoo.com" target="_blank">2. Yahoo</a></p>
</body>
  1. Then make the revision in this gist, changing "URLToOpenInUIWebView.com" to "google.com".
  2. That should make it so Google opens in the view, and Yahoo in Safari.

Once you've got that working, try to figure out what's different in your app.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment