我的Swift应用程序与UIWebView崩溃与exc_bad_access当试图获得当前URL。它也只在某些情况下崩溃(通常取决于用户在UIWebView中执行的操作)。尝试加载代码中提供的url,然后点击取消。如果我不实现UIWebViewDelegate
协议的方法,它永远不会崩溃。
class AuthViewController: UIViewController, UIWebViewDelegate {
@IBOutlet var authWebView: UIWebView
init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: NSBundle?) {
super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
// Custom initialisation
}
override func viewDidLoad() {
super.viewDidLoad()
self.authWebView.delegate = self
var url = NSURL(string:"http://oauth.vk.com/authorize?client_id=4423823&scope=audio&display=mobile&v=5.21")
var urlRequest = NSMutableURLRequest(URL: url)
self.authWebView.loadRequest(urlRequest)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func webViewDidFinishLoad(webView:UIWebView) {
NSLog( webView.request!.URL!.absoluteString )
}
}
我也试图实现这个方法来检查请求对象是否存在之前获得URL,但它没有帮助。
func webView(webView: UIWebView!,
shouldStartLoadWithRequest request: NSURLRequest!,
navigationType navigationType: UIWebViewNavigationType) -> Bool {
if !self.authWebView.request {
return false
} else {
return true
}
}
我发现了错误:
部分URL包含特殊字符,如%@或#,用于在NSLog中格式化。如果在NSLog的第一个参数字符串中使用了这些字符中的任何一个,则需要更多的参数来格式化。
如
NSLog("http://someurl.com/") // this is fine (no special chars used)
NSLog("http://someurl.com/#somehash?x=%@") // this is not fine (%@ is used in URL, NSLog thinks that I'm formatting the string
var someURLString = "http://someurl.com/#somehash?x=%@"
NSLog("%@", someURLString) // this is fine and the way it has to be done
谢谢大家看我的问题!