我正在使用 Swift 2 构建的 iOS 9 应用程序中使用 SFSafariViewController。
我正在尝试打开一个由于某种原因失败的 URL。我尝试过的所有其他 URL 都有效,除了这个:
http://www.ctvnews.ca/sci-tech/33-engineers-to-be-recognized-at-sci-tech-oscars-1.2730223
该 URL 在模拟器中的常规移动 Safari、我的 iPhone、iPad 和任何桌面浏览器中都很好。只是当我尝试在这个应用程序中通过 Swift 访问它时它失败了。
代码如下:
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)
})
}
}
这段代码运行良好,正如我所说,其他 URL 加载也很好。我真正需要的只是一种更详细地调试 safariViewController 遇到的导致它失败的方法。
该didLoadSuccessfully == false
行似乎并没有提供更多调试选项来了解问题所在。
换句话说,我该如何调试呢?我似乎在 Apple 的文档中找不到任何可以解释在加载错误的情况下要检查的内容。
请帮忙!谢谢。