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()
}
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.
Related
I went through a few SO post and can't seem to fix my issues.
The codes works fine, but if i update my profile pics, the UIWebView, will still show the cached image instead of the updated image.
if i close my app and reopen, the webview will update to the new image.
any pointer are greatly appreciated.
func getLeaderBoard()
{
leaderWebView.loadRequest(getStaticWebView(urlStr: getLeaderBoardUrl) as URLRequest)
}
func getStaticWebView(urlStr:String) -> NSMutableURLRequest
{
let url = URL.init(string: urlStr)
let request = NSMutableURLRequest.init(url: url!)
request.httpMethod = "GET"
request.setValue("token \(kToken)", forHTTPHeaderField: "Authorization")
request.cachePolicy = .reloadIgnoringLocalAndRemoteCacheData
URLCache.shared.removeAllCachedResponses()
URLCache.shared.diskCapacity = 0
URLCache.shared.memoryCapacity = 0
return request
}
I am working on an app in which I am using UIWebView, when the webpage loads it adds space in the screen see below
Below is the code I have tried,
let requestURL = NSURL(string: "url")
let request = NSURLRequest(url: requestURL! as URL)
webView.loadRequest(request as URLRequest)
self.automaticallyAdjustsScrollViewInsets = false
//webView.scrollView.contentInset.right = UIEdgeInsets.zero.right
nothing works.
I come from UIWebView where I had following code:
let url2 = NSURL(string: URL+"/auth?id="+self.username!+"&pw="+self.password!)
let task2 = NSURLSession.sharedSession().dataTaskWithURL(url2!) {(data, response, error) in
print(NSString(data: data!, encoding: NSUTF8StringEncoding))
}
task2.resume()
Doing this I was automatically logged in.
But now with the WKWebView this is not working anymore.
The Code is executed just before loading the request:
self.webView = WKWebView()
let request = NSURLRequest(URL: portalURL!)
self.webView!.loadRequest(request)
Username and Password are stored on the Device, when the User installs the Application.
How can I get a similar effect with WKWebView?
You'd better add user info to request header. Like this:
let url = NSURL(string: urlString)
let urlRequest = NSMutableURLRequest(URL: url!)
if needAddHttpHeader {
urlRequest.addValue("login", forHTTPHeaderField: "User-Login")
urlRequest.addValue("token", forHTTPHeaderField: "User-Token")
UBLog(urlRequest)
}
webView.loadRequest(urlRequest)
I know I can enable caching with NSURLRequest
if reachability.isReachable {
urlRequestCache=NSURLRequest(URL: url!, cachePolicy: NSURLRequestCachePolicy.UseProtocolCachePolicy, timeoutInterval: 10)
}
else {
urlRequestCache = NSURLRequest(URL: url!, cachePolicy: NSURLRequestCachePolicy.ReturnCacheDataElseLoad, timeoutInterval: 60)
}
theWebView.loadRequest(urlRequestCache)
But I couldn't find any way directly on WkWebView to achieve this.
Swift 4:
var webView = WKWebView()
if let url = URL(string: string) {
var request = URLRequest(url: url)
request.cachePolicy = .returnCacheDataElseLoad
webView.load(request)
}
I am trying to cache my UIWebView for when I have no network connection but it doesn't work. I tried looking into HanekeSwift but that only works with JSON requests as far as I could find.
This is what I currently have:
override func viewDidLoad() {
super.viewDidLoad()
let id = NSUserDefaults.standardUserDefaults().objectForKey("id") as! String
let url = NSURL (string: "http://****.php?id=\(id)");
let requestObj = NSURLRequest(URL: url!);
subtitleLabel.loadRequest(requestObj);
}
My question was answered here:
Getting error with cachePolicy
Thanks to KudoCC for pointing me in the right direction I got this as my final code:
var url = NSURL(string: "****.php?page=over_polen")
let requestObj = NSURLRequest(URL: url!, cachePolicy: NSURLRequestCachePolicy.ReturnCacheDataElseLoad, timeoutInterval: 5);
webViewLabel.loadRequest(requestObj);