JasonC February 2016

safariViewController debugging - URL not loading

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 didLo

Answers


jcaron February 2016

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.

Post Status

Asked in February 2016
Viewed 1,498 times
Voted 9
Answered 1 times

Search




Leave an answer