1

I'm using SFSafariViewController in an iOS 9 app I'm building using Swift 2.

I am trying to open a URL which fails for some reason. EVery other URL I've tried works, except for this one:

http://www.ctvnews.ca/sci-tech/33-engineers-to-be-recognized-at-sci-tech-oscars-1.2730223

The URL is fine in regular mobile Safari in the simulator, on my iPhone, my iPad, and in any desktop browser. It is just when I try accessing it via Swift in this app that it fails.

Code is as follows:

    func openInSafari(URLtoOpen: String) {
        print("Trying to openURL: " + URLtoOpen)
        let safariVC = SFSafariViewController(URL:NSURL(string: URLtoOpen)!, entersReaderIfAvailable: false)
        safariVC.delegate = self
        self.presentViewController(safariVC, animated: true, completion: nil)
    }

    func safariViewController(controller: SFSafariViewController, didCompleteInitialLoad didLoadSuccessfully: Bool) {
        // SFSafariViewController will present an error message in the browser,
        // but in this case we will dismiss the view controller and present our
        // own error alert.
        if didLoadSuccessfully == false {
            controller.dismissViewControllerAnimated(true, completion: { [unowned self] () -> Void in
                let alert = UIAlertController(title: "Could Not Load", message: "The URL could not be loaded.", preferredStyle: .Alert)
                alert.addAction(UIAlertAction(title: "Okay", style: .Default, handler: nil))
                self.presentViewController(alert, animated: true, completion: nil)
            })
        }
    }

This code works fine, and, as I said, other URLs load just fine. What I really need is just a way to more verbosely debug what safariViewController is encountering that is causing it to fail.

The didLoadSuccessfully == false line doesn't really appear to offer much more debugging options to get a sense of what went wrong.

In other words, how do I debug this? I can't seem to find anything in Apple's docs that would explain what to check in case of a loading error.

Please help! Thanks.

jcaron
  • 17,302
  • 6
  • 32
  • 46
SH10151
  • 954
  • 1
  • 10
  • 17
  • Just to be sure, you have added ATS exceptions for insecure requests? – jcaron Feb 08 '16 at 22:47
  • I have added ATS exceptions during development, yep. – SH10151 Feb 08 '16 at 22:58
  • Nothing showing up in the logs? Is `didCompleteInitialLoad` indeed called with `didLoadSuccessfully` false? If you let it display its own error message, does it say anything useful? – jcaron Feb 08 '16 at 23:04
  • The only thing in the logs is what I'm printing, essentially a "Success loading URL" or "Failure loading URL" in the respective places. Basically what I'm looking for: something like a property of the "controller" object that might shed some more light as to why this failed. – SH10151 Feb 08 '16 at 23:11
  • Is there a way to use a Try / Catch block and use NSError? Would there be any error information in there or some other relevant error tracking / debug object that SFSafariViewController uses? I'm guessing it has something to do with the formatting of the URL. Maybe it doesn't like the hyphen or the period (even though that conforms with the HTTP spec). – SH10151 Feb 11 '16 at 19:49
  • The `SFSafariViewController` is voluntarily shielded from the app (I wouldn't be surprised if it's actually a different process), and there's very little information shared with the app for privacy and security reasons. What you could try is loading the same based in an `UIWebView` or `WKWebView` (which gives you a lot more feedback, but doesn't share cookies or passwords with Safari) to see what happens then. – jcaron Feb 11 '16 at 21:37
  • ahh ok, thanks, I'll give that a try. – SH10151 Feb 11 '16 at 21:44
  • btw, jcaron, if you want to post your comment as a possible solution, I will mark it as the accepted answer, since your suggestion, in my opinion, is about the best answer possible from what I understand of this issue. Thanks again. – SH10151 Feb 15 '16 at 21:10
  • Did you find out what the issue with that page actually was? – jcaron Feb 15 '16 at 22:43
  • Not yet, but I will go back and try out your suggestion. – SH10151 Feb 19 '16 at 23:54

1 Answers1

0

The SFSafariViewController is voluntarily shielded from the app (I wouldn't be surprised if it's actually a different process), and there's very little information shared with the app for privacy and security reasons.

What you could try is loading the same based in an UIWebView or WKWebView (which gives you a lot more feedback, but doesn't share cookies or passwords with Safari) to see what happens then.

jcaron
  • 17,302
  • 6
  • 32
  • 46