I am using a UIWebView in my app to load the contents of a website. I then use javascript to log into the website, and then I parse the contents of the page after I've logged in to grab the data I need. I do this every 5 minutes, which works fine in the foreground. However, when I background the app, I make a call to the UIViewController that holds the UIWebView and I hit the method that loads the URL into the UIWebView, but nothing happens after that.
I am fairly sure that the UIViewController must be active to update the UIWebView, but is there a way around this? Checking this data in the background is really the entire point of the app.
EDIT
jakepeterson - Based on your code, I tried to subclass a UIWebView starting in Swift. This is as far as I got:
class CustomWebView: UIWebView, UIWebViewDelegate
{
var webView: UIWebView
var requestURL: NSURL
var urlRequest: NSURLRequest
init()
{
webView = UIWebView()
requestURL = NSURL(string: "http://www.google.com")
urlRequest = NSURLRequest(URL: requestURL)
super.init(frame: CGRectMake(0, 0, 100, 100))
webView.delegate = self
webView.addSubview(webView.window)
webView.loadRequest(request)
}
func webViewDidFinishLoad(sender: UIWebViewDelegate)
{
//Do stuff
}
}
I am still not hitting the webViewDidFinishLoad delegate method. I think it has to do possibly with this line:
webView.addSubview(webView.window)
In your code you have self.view but that doesn't show up as an available option.
UPDATE
Here is what I am doing now and the delegates are now getting triggered:
import UIKit
class CustomWebView: UIWebView
{
var uiView = UIView()
init(frame: CGRect)
{
super.init(frame: frame)
self.delegate = self
}
convenience init(frame:CGRect, request:NSURLRequest)
{
self.init(frame: frame)
uiView.addSubview(self)
loadRequest(request)
}
}
extension CustomWebView: UIWebViewDelegate
{
func webViewDidStartLoad(webView: UIWebView!)
{
println("webViewDidStartLoad")
}
func webViewDidFinishLoad(webView: UIWebView)
{
println("webViewDidFinishLoad")
}
func webView(webView: UIWebView!, didFailLoadWithError error: NSError!)
{
println("An error occurred while loading the webview")
}
}