Duarte
Duarte

Reputation: 127

UIWebView won't reload a new URL in Swift

I always used UIWebView without troubles until I wanted to load a new URL. With the code below, the second url will not be loaded for some reason. Can you find why? (The plist has the exceptions for the domains!)

    let webview = UIWebView()
    webview.frame = CGRectMake(0, 0, view.bounds.width, view.bounds.height )
    view.addSubview(webview)

    var url = NSURL (string: "http://google.com")
    var requestObj = NSURLRequest(URL: url!,
        cachePolicy: NSURLRequestCachePolicy.ReloadIgnoringLocalAndRemoteCacheData,
        timeoutInterval: 10.0)
    webview.loadRequest(requestObj)
    webview.reload()

    //Wait 5 seconds to load another page
    let time = dispatch_time(dispatch_time_t(DISPATCH_TIME_NOW), 5 * Int64(NSEC_PER_SEC))
    dispatch_after(time, dispatch_get_main_queue()) {

    url = NSURL (string: "http://yahoo.com")
     requestObj = NSURLRequest(URL: url!,
        cachePolicy: NSURLRequestCachePolicy.ReloadIgnoringLocalAndRemoteCacheData,
        timeoutInterval: 10.0)

    webview.loadRequest(requestObj)
    webview.reload()
    }

Upvotes: 1

Views: 3728

Answers (1)

Brad
Brad

Reputation: 89

Calling webview.loadRequest() begins loading the webview content. Calling webview.reload() reloads the last request that has finished loading. Since you're calling reload immediately after loading the request, reload is loading your first request "google.com" again.
Removing the webview.reload() calls will load your second request.

Upvotes: 4

Related Questions