I'm loading a javascript which is in my App, the script will try to find a solution, which can takes some time. I don't want to wait more than 5 seconds for that solution, in that case I just want to stop the request and show a message to the user.
I've been trying to do it with NSTimers and with dispatch_async, but the UIWebView request is still using my whole main queue, and the NSTimer is stopped in the meantime I'm waiting for the answer.
The request call:
webViewUIView.delegate = self
var path = NSBundle.mainBundle().pathForResource("page", ofType: "html")
var url = NSURL(fileURLWithPath: path!)
var req = NSURLRequest(URL:url!)
var queue = NSOperationQueue()
dispatch_async(dispatch_get_main_queue()) {
println("********CALLING TO LOADREQUEST")
self.webViewUIView.loadRequest(req)
}
And my NSTimer:
var timer2 = NSTimer()
func webViewDidStartLoad(webView: UIWebView) {
println("********Creating the Timer")
let aSelector : Selector = "cancelWeb"
self.timer2 = NSTimer.scheduledTimerWithTimeInterval(5, target: self, selector: aSelector, userInfo: nil, repeats: false)
println("After create the timer")
}
The cancelWeb:
func cancelWeb(){
println("****Before Stop Load")
webViewUIView.stopLoading()
println("After stopLoading")
timer2.invalidate()
}
My webViewDidFinishLoad:
func webViewDidFinishLoad(webView: UIWebView) {
webViewUIView.stringByEvaluatingJavaScriptFromString("func('\(obj)')")
let result: String = webViewUIView.stringByEvaluatingJavaScriptFromString("document.getElementById('log').innerText")!
timer2.invalidate()
...
...
When I run the app, the process is still running, even after the 5 seconds setted in the Timer, I only see this meesage: about Creating TImer, After Timer, and CALLING TO LOADREQUEST.
Any ideas about how to run the request in parallel at the same time with the NSTimer? As you see the dispatch is not working, maybe because the URL is in the same Bundle?
Well, in the meantime I'm waiting for the solution, the App is frozen, just waiting for the answer...which could take loooong time.
Using the timeout in the request is not working, because the connection is stablished correctly, the thing is that is taking long time in receive the answer.
Thanks fellas!
[NSURLConnection sendAsynchronousRequest]
in the first place? If you want to execute a javascript method in an html page, you can use[UIWebView stringByEvaluatingJavaScriptFromString:]
– Ricoricochet