0

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.

karel
  • 5,489
  • 46
  • 45
  • 50
BearP
  • 1
  • 1
  • What about URL scheme (search for UIApplication.shared.open) and Info.plist (or whatever your main plist file is named) or App Transport Security. Those are the tips to search for. – ares777 Feb 19 '19 at 10:39
  • I already made some changes to the info.plist, but i don't know anything about the UIApplication.shared.open URL scheme. Do you believe this might be where the problem occurs? – BearP Feb 19 '19 at 11:45
  • Maybe there is a similar issue on this link. It could help you : [Here](https://stackoverflow.com/questions/4299403/how-to-handle-app-urls-in-a-uiwebview?rq=1) – Lapinou Feb 19 '19 at 12:18

2 Answers2

0

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

https://developer.apple.com/documentation/uikit/uiapplication/1648685-openurl?language=objc

  • Thank you for the answer. I will try to implement what you suggested and return if it worked. – BearP Feb 19 '19 at 12:18
  • No luck :( I tried implementing the webView:shouldStartLoadWithRequest:navigationType: method which was also mentioned in another solution but that didnt do anything. The device is currently on IOS 10.2.2 so i also tried the openURL:options:completionHandler:, but nothing worked. – BearP Feb 21 '19 at 12:53
0

So, 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 !

Lapinou
  • 1,467
  • 2
  • 20
  • 39
  • Thank you for the input! I will be sure to try this out and return with the results :) – BearP Feb 19 '19 at 13:34
  • Sadly this didn't solve the problem. I am out of ideas on how to fix this right about not. I did even more research and this seems to be the correct fix for most people but not me. – BearP Feb 21 '19 at 12:12
  • Sorry... Could you post your code (remove all the stuff not needed) please ? So, I will recreate the sample on my side to debug it. I know your situation and really want to help you. Also, on which device do you test it ? Simulator or real device ? On Which iOS version and with which iOS SDK do you compile it ? – Lapinou Feb 21 '19 at 14:13
  • It's quite a big application and I am not sure its allowed to post the code, but i will ask my manager. I test it on a real device, which have a printer connected. It's through another app that this connection happens and its this app i am trying to open in my own app. I compile it for IOS 11 i think and the device runs IOS 10. I don't know if this affect anything. – BearP Feb 22 '19 at 07:25