Reputation: 704
My Swift app with UIWebView crashes with exc_bad_access when trying to get current URL. It also crashes only in some cases (often depends on the actions performed by user within that UIWebView). Try loading the url provided in code and then tapping cancel. It never crashes though if I don't implement methods from UIWebViewDelegate
protocol.
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 )
}
}
I was also trying to implement this method to check if request object exists before getting the URL but it didn't help.
func webView(webView: UIWebView!,
shouldStartLoadWithRequest request: NSURLRequest!,
navigationType navigationType: UIWebViewNavigationType) -> Bool {
if !self.authWebView.request {
return false
} else {
return true
}
}
Upvotes: 2
Views: 1904
Reputation: 704
I've found the error:
Some URL contains special characters, such as %@ or # which is used for formatting in NSLog. If any of those characters is used in the string the first argument for NSLog, than more arguments required for formatting.
E.G.
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
Thanks everyone who took a look at my question!
Upvotes: 4