UIWebView cache not working - ios

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);

Related

URLRequest to Alamofire URLConvertible

I have found the same question but not worked for me. thats why I'm posting this again.
The following code gives me an error.
Could not cast value of type 'Foundation.URLRequest' to 'Alamofire.URLConvertible'
the code sample:
let url = (wURL).replacingOccurrences(of: "(f_id)", with: String(conID))
let _url = URL(string: url)
var urlRequest = URLRequest(url: _url!)
urlRequest.timeoutInterval = TimeInterval(exactly: 30)!
let finalUrl = urlRequest as! URLConvertible
What am I missing?
You can't do it, because for URLRequest there is special protocol URLRequestConvertible.
let realURL: URL = URL(string: "https://google.com")!
let url: Alamofire.URLConvertible = realURL
let urlRequest: Alamofire.URLRequestConvertible = URLRequest(url: realURL)
AF.request(urlRequest).responseJSON {
print($0)
}

crash uiwebview without http or https in swift [duplicate]

This question already has answers here:
Add http:// to NSURL if it's not there
(5 answers)
Closed 5 years ago.
I want show the URL in Web View
here is my code
let urlString:String = "https://www.apple.com"
let url:URL = URL(string: urlString)!
let urlRequest:URLRequest = URLRequest(url: url)
webView.load(urlRequest)
urlTextField.text = urlString
if user forget to write http or https the app crashed how can I resolve this error
Just use starts(with:) on string to detect if the url string starts with http/https, and if not, add the "http://" yourself (also, use safe if let instead of force unwrap):
var urlString: String = "www.apple.com"
if !urlString.starts(with: "http://") && !urlString.starts(with: "https://") {
urlString = "http://\(urlString)"
}
if let url: URL = URL(string: urlString) {
let urlRequest: URLRequest = URLRequest(url: url)
webView.load(urlRequest)
urlTextField.text = urlString
}

Spaces in UIWebView

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.

Enabling webcache in WkWebview using WkWebView only(not with the help of NSURLRequest or NSURLSession)

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)
}

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()
}
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.

Resources