I am working on a incongnito browser.I am using wkwebview when I clear all the cookies I can see that popular search engine like google remembers the searches that has been made.
I tried cleaning all the cookies in NSHTTPCookieStorage and resetcookies using NSURLSession but its still not working.
Set nonpersistentdatastore for wkwebsitedatastore for wkwebviewconfiguration for wkwebview
Set NSURLrequestreloadcacheignoringlocalandremotecachedata for NSURlrequest in uiwebview
Reference
Creating a non-tracking in-app web browser
Private browsing in iOS using WKWebView
As per apple documentation:
To support private browsing,create a data store object and assign it to the websiteDataStore property of a WKWebViewConfiguration object before you create your web view. The default() method returns the default data store that saves website data persistently to disk. To implement private browsing, create a nonpersistent data store using the nonPersistent() method instead.
let webConfiguration = WKWebViewConfiguration()
webConfiguration.processPool = WKProcessPool()
webConfiguration.websiteDataStore = WKWebsiteDataStore.nonPersistent()
let webView = WKWebView(frame: self.webContainerView.bounds, configuration: webConfiguration)
// Set up request
if let requestURL = URL(string: "enter_url_to_load") {
var request = URLRequest(url: requestURL)
request.httpShouldHandleCookies = false
request.cachePolicy = .reloadIgnoringLocalAndRemoteCacheData
webView.navigationDelegate = self
webView.load(request)
}
self.webContainerView.addSubview(webView)
Related
I need to cache webview url on WKWebview. I'm able to do it using the below configuration
var webViewConfiguration:WKWebViewConfiguration {
get {
// Create WKWebViewConfiguration instance
let webCfg:WKWebViewConfiguration = WKWebViewConfiguration()
// Configure the WKWebViewConfiguration instance with the WKUserContentController
webCfg.userContentController = userContentController
webCfg.websiteDataStore = WKWebsiteDataStore.default()
webCfg.processPool = ProcessPool.shared.processPool
return webCfg
}
}
and while loading the webview I'm using the below code:
let request = URLRequest(url: self.url!, cachePolicy: .returnCacheDataElseLoad, timeoutInterval: 600)
self.webView.load(request)
Issue I'm facing right now is the cache is taking time on every launch. i.e on every launch webview is taking a lot of time to load, after one load it is loading fast.
What I need to achieve is once webview is loaded, it should load faster on consecutive loads.
.useProtocolCachePolicy is the default policy for URL load requests. It should work for your case.
let request = URLRequest(url: self.url!, cachePolicy: .useProtocolCachePolicy, timeoutInterval: 600)
Using the default .useProtocolCachePolicy instead of .returnCacheDataElseLoad is OK. This policy will automatically look at the response from the server to decide whether or not it should actually go and grab the data again. On the server side, set the Cache-Control HTTP headers max age for its responses.
I am developing an application(Swift 3 using UIWebview). I need to load webpages into webview and save some webpages into cache. If there is not internet user will able to see those pages. But I am confused on how to save whole webpage in cache. The main thing is we need to show pages back even if there is not internet.
I used the following documention : http://nshipster.com/nsurlcache/
and https://developer.apple.com/reference/foundation/urlcache
let url = NSURL(string: load_url1)
let request = NSURLRequest(url: url as! URL,cachePolicy: NSURLRequest.CachePolicy.returnCacheDataElseLoad, timeoutInterval: 60)
self.webView.loadRequest(request as URLRequest);
Has anyone implemented this before. Please provide some demo code as this is my first attempt on cache
Just use
let url = URL(string: urlString)
var urlRequest = URLRequest(url: url!)
urlRequest.cachePolicy = .returnCacheDataElseLoad
webView.loadRequest(urlRequest)
In a class my project is using have a var to store the alamofire manager:
var alamoManager: Manager!
A method is called repeatedly in the app to config this manager like so:
func configAlamoManager() {
let configuration = NSURLSessionConfiguration.defaultSessionConfiguration()
configuration.timeoutIntervalForRequest = 20
//ETC
alamoManager = Alamofire.Manager(configuration: configuration)
}
I have a HTTP call in my app that is ocasiaonally returning a 999 canceled error code. I suspect this is because the manager currently trying to perform the request is replaced by another one from the configAlamoManager() method. Is there any way to just change the config settings in the manager without creating a new instance? alamoManager.session.configuration has no setter. Any pointers on this would be really appreciated! Thanks
Instead of changing the configuration and creating a new Manager, you should override the configuration in the actual NSURLRequest.
let urlRequest = NSURLRequest(url: url)
urlRequest.timeoutInterval = 20
Alamofire.request(urlRequest).responseJSON { response in
debugPrint(response)
}
For more info on what you can override using an NSURLRequest, I'd check out the docs.
Is it possible to send cookie with the AVPlayer url?I have a livestream which is AES encrypted and needs a key to decrypt.It will hit the server and the server returns the key only if session is there.So I want to send phpsessionid along with the url to AVPlayer.
Is it possible? I saw AVURLAssetHTTPHeaderFieldsKey.I don't know if it is what I have to set.If so how to do it?
This is how you can set Signed Cookies (Headers) in AVPlayer URL Request :
fileprivate func setPlayRemoteUrl() {
if playUrl.isEmpty { return }
let cookiesArray = HTTPCookieStorage.shared.cookies!
let values = HTTPCookie.requestHeaderFields(with: cookiesArray)
let cookieArrayOptions = ["AVURLAssetHTTPHeaderFieldsKey": values]
let assets = AVURLAsset(url: videoURL! as URL, options: cookieArrayOptions)
let item = AVPlayerItem(asset: assets)
player = AVPlayer(playerItem: item)
playerLayer = AVPlayerLayer(player: player)
playerLayer?.videoGravity = AVLayerVideoGravityResizeAspectFill
playerLayer?.contentsScale = UIScreen.main.scale
layer.insertSublayer(playerLayer!, at: 0)
}
In your case FPS(FairPlay Streaming) by apple will work. FairPlay Streaming is DRM(Digital Right Management) support where you will get content key along with your content data and you need to pass through delegate which supports AES-128 encrypt. Please refer below link which i shared below
https://developer.apple.com/streaming/fps/
I haven't really tried it myself but it seems there's an API that let's you create AVURLAsset with options. One of the possible option key is AVURLAssetHTTPCookiesKey. You might want to look into that.
What I want to do is create an NSURLSession that does not automatically give a value to the Cookie HTTP header when I'm making an NSMutableURLRequest, because I set that value on my own. However, I want to be able to store the cookie that comes back to me when I make a sign in API request.
I use the following code to instantiate my session:
func createSession() {
let configuration = NSURLSessionConfiguration.defaultSessionConfiguration()
// Set cookie policies.
configuration.HTTPCookieAcceptPolicy = .Always
configuration.HTTPCookieStorage = NSHTTPCookieStorage.sharedHTTPCookieStorage()
configuration.HTTPShouldSetCookies = false
// ...
return NSURLSession(configuration: configuration, delegate: nil, delegateQueue: nil)
}
When the NSURLSessionDataTask comes back to me in its completionHandler, I dig into the shared cookie storage to get the cookies that came with the response. However, I always get an empty array.
if let cookies = NSHTTPCookieStorage.sharedHTTPCookieStorage().cookiesForURL(response.URL!) {
println("cookies: \(cookies)") // Prints an empty array.
if let cookie = cookies.last as? NSHTTPCookie {
// I save the cookie to disk here.
}
}
Why is this happening even when I've set the NSURLSession to always accept cookies, and even provided a cookie storage?
I can only get the behavior that I want by setting the HTTPShouldSetCookies to true and supplying a Cookie HTTP header in my NSMutableURLRequest anyway. However, the Apple documentation on HTTPShouldSetCookies says that I should set it to false if I were to provide the cookie on my own in the request level:
If you want to provide cookies yourself, set this value to NO and provide a Cookie header either through the session’s HTTPAdditionalHeaders property or on a per-request level using a custom NSURLRequest object.