I'm trying to fix an old iOS app where UIWebView won't open an URL to another app. The app says "URL cannot be shown", which might indicate the URL is wrong, but I am able to open it directly in Safari.
I received the responsibility for fixing an app for my company and I do not have much experience with Objective C, so I hope you can help me. The app have previously worked, but suddenly stopped working in 2016. This is probably because of an update that happened at this time, which changed somURL can't be showne functionality.
The app opens a UIWebView and through this accesses a webservice from where the user can input some information, etc. This webservice then calls another app to print a label, but instead just displays a pop-up with URL can't be shown.
When I write this URL directly into Safari, it opens the app and works with no problems. This made me think the problem might be somewhere in UIWebView, but I cannot locate exactly where this might be.
I do not know what cope snippet or information might be worth including, but if you ask what you need I can provide it.
32 Answers
Presumably if it used to work, there are calls to UIApplication's canOpenUrl: and openUrl: (likely you'll find these in your web view delegate's webView:shouldStartLoadWithRequest:navigationType:)
iOS9 made changes to how UIApplication canOpenUrl: works - mostly it doesn't any more.
If you're OK with supporting only iOS10 - look at using openURL:options:completionHandler: instead
2So, in addition to my comment, try to do this :
Go to your controller where you deal with your webview, and add this piece of code to that method:
- (BOOL)webView:(UIWebView *)wv shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType { // Determine if we want the system to handle it. NSURL *url = request.URL; if (![url.scheme isEqual:@"http"] && ![url.scheme isEqual:@"https"]) { if ([[UIApplication sharedApplication]canOpenURL:url]) { [[UIApplication sharedApplication]openURL:url]; return NO; } } return YES; } Then, also, handle some error that can be ignored :
- (void)webView:(UIWebView *)wv didFailLoadWithError:(NSError *)error { // Ignore NSURLErrorDomain error -999. if (error.code == NSURLErrorCancelled) return; // Ignore "Fame Load Interrupted" errors. Seen after app store links. if (error.code == 102 && [error.domain isEqual:@"WebKitErrorDomain"]) return; // Normal error handling… } Let me know if it's working for you. You can read the full post of that solution here.
Happy coding !
4