Alamofire loading from cache even when cache policy set to ReloadIgnoringLocalAndRemoteCacheData - ios

I set cache policy to request in Alamofire to ignore local cache.
Then I load a viewcontroller with network connection, then I disconnect network connection, kill the app and run it again.
Now no network available error is not shown(ie alamofire doesnt create nserror object) created, instead app runs as if the request succeeded getting data from cache obviously.And the odd thing is the when I tried to inspect the cached data using
NSURLCache.sharedURLCache().cachedResponseForRequest(request)
nil is returned eventhough the data was from cache ..
The only way I could prevent cached responses is perform NSURLCache.sharedURLCache().removeAllCachedResponses()
let request = NSURLRequest(URL: NSURL(string: url)!, cachePolicy: NSURLRequestCachePolicy.ReloadIgnoringLocalAndRemoteCacheData, timeoutInterval: 100)
Alamofire.manager.request(method, request, parameters:params)
.responseJSON { (request, response, data, error) in
if let anError = error {
if anError.code == NSURLErrorNotConnectedToInternet {
UIAlertView(title: "Alert", message: "No Network Connection Available", delegate: nil, cancelButtonTitle: "ok").show()
}
} else if let data: AnyObject = data {
println(NSURLCache.sharedURLCache().cachedResponseForRequest(request))
//prints nil
}
}
}
What I want to do is load data from cache only if network connection is not available, something like limited offline mode.How to do this?

I'm using this way in a project and it's working:
let mutableURLRequest = NSMutableURLRequest(URL: SERVICEURL)
mutableURLRequest.HTTPMethod = "POST"
mutableURLRequest.HTTPBody = self.createJson()
mutableURLRequest.setValue("application/json", forHTTPHeaderField: "Content-Type")
mutableURLRequest.cachePolicy = NSURLRequestCachePolicy.ReloadIgnoringCacheData
request(mutableURLRequest).validate().responseJSON{ response in...
Hope it helps.

Thanks to #FábioSalata I solved my problem like this.
var req = URLRequest(url: URL(string: "<URL>")!)
req.httpMethod = "GET"
req.setValue("application/json", forHTTPHeaderField: "Content-Type")
req.setValue("<Auth KEY>", forHTTPHeaderField:"Authorization" )
req.cachePolicy = NSURLRequest.CachePolicy.reloadIgnoringCacheData
Alamofire.request(req).validate().responseJSON { response in ...

ReloadIgnoringLocalAndRemoteCacheData is not implemented.
https://developer.apple.com/reference/foundation/nsurlrequestcachepolicy/1408722-reloadignoringlocalandremotecach
http://nshipster.com/nsurlcache/
Update: Starting with iOS 13, NSURLRequest.CachePolicy.reloadRevalidatingCacheData and NSURLRequest.CachePolicy.reloadIgnoringLocalAndRemoteCacheData are implemented. https://developer.apple.com/documentation/ios_ipados_release_notes/ios_13_release_notes

Related

iOS - HTTP Method not making POST Request but instead making GET

I'm trying to make a POST call in my app using URLSession (and AlamoFire) no luck with either, I'm checking the network call on Charles Proxy and it's always being sent as a GET call even though I've specified the HTTPMethod to be "POST" or .post in both.
Weirdest part is updating the call to "DELETE" or "PUT" will work...just not "POST" or .post
I've tried just building the request from ground up in URLSession instead of using AlamoFire, tried changing the HTTPMethod (which works, just not for POST)
let session = URLSession.shared
let url = URL(string: endpointURL)!
var request = URLRequest(url: url)
request.httpMethod = "POST"
let jsonData: Data = try! requestProto.serializedData()
let task = session.uploadTask(with: request, from: jsonData) { data, response, error in
print("request made")
}
task.resume()
Expected: Charles records a POST call
Actual: Charles is recording GET call for both GET and POST calls, but DELETE and PUT are working fine.
Try this with URLSession
// create a `URLRequest` because post are urlRequest used with urlSession.shared.uploadTask
var request = URLRequest(url: url)
// here pass in data format the body of the request
request.httpBody = body
// the request method.
request.httpMethod = "post"
// required when send a json type in request
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
URLSession.shared.uploadTask(with: request, from: nil) { (data, response, error) in
let response = (response as? HTTPURLResponse)?.statusCode
if (response! == 500 ){
print("error")
}
print("request made")
}.resume()
I don't know why, but make sure that you are sending https request (not http).

Swift URLSession.shared.dataTask GET Request -1001 returns timeout

Sending a POST request to our NGINX Server works good with URLRequest and URLSession.shared.dataTask.
I can login to my app but when I try a GET request my server has no log that the request reached him. Finally I get the timeout error. Important, I am using https. I also tried to use http for GET Requests. on my NGINX Server I set TLS to 1.2 only (I am not a system specialist, I did that in the nginx cfg file.
Error Domain=NSURLErrorDomain Code=-1001 "Zeitüberschreitung bei der Anforderung." UserInfo={NSUnderlyingError=0x60400005abe0 {Error Domain=kCFErrorDomainCFNetwork Code=-1001 "(null)" UserInfo={_kCFStreamErrorCodeKey=-2102, _kCFStreamErrorDomainKey=4}}, NSErrorFailingURLStringKey=https://myurl, NSErrorFailingURLKey=https://myurl, _kCFStreamErrorDomainKey=4, _kCFStreamErrorCodeKey=-2102, NSLocalizedDescription=Zeitüberschreitung bei der Anforderung.}
I am sure that my code for the URLRequest and URLSession is correct because against localhost and our development environment I do not have any of those problems.
Thats my code to create my URLRequest
private func buildUrlRequest(jsonObject:[String: Any], connectionType:ConnectionTypes, url:String) -> NSMutableURLRequest? {
let jsonDataSerialized:Data
do {
jsonDataSerialized = try JSONSerialization.data(withJSONObject: jsonObject)
} catch {
return nil
}
var request:NSMutableURLRequest
if nextData {
request = URLRequest(url: URL(string: self.nextRequestPage)!) as! NSMutableURLRequest
} else {
let tString = self.mBaseUrl + url
if let encoded = tString.addingPercentEncoding(withAllowedCharacters: .urlFragmentAllowed),
var finalurl = URL(string: encoded)
{
if connectionType == .GET {
var tString:String
tString=finalurl.absoluteString
tString = tString.replacingOccurrences(of: "https", with: "http")
finalurl = URL(string:tString)!
}
request = URLRequest(url: finalurl) as! NSMutableURLRequest
} else {
return nil
}
}
request.httpMethod = connectionType.rawValue // i am using enum here
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
request.setValue("no-cache)", forHTTPHeaderField: "Cache-Control")
request.httpBody = jsonDataSerialized
if mUser != nil && mUser.getSessionId() != nil {
request.addValue("Token " + mUser.getSessionId()!, forHTTPHeaderField: "Authorization")
}
request.cachePolicy = .reloadIgnoringLocalAndRemoteCacheData
request.timeoutInterval = 30.0
return request
}
This is how I create the task... after the method body I am using task.resume()
let task = URLSession.shared.dataTask(with: request as URLRequest) { data, response, error in .... (and so on)
I spend more than hours to solve this problem... But I have no idea.
I am not sure if the Problem is the Server Config or the Swift code...
The major problem was jsonObject:[String: Any] !
I changed it to jsonObject:[String: Any]?
In my code I've created the body for each request no matter if it was a POST GET PUT or what ever. Without the http-body in a GET request I have no problem anymore, no timeout! I correctly receive the data as expected.
I've also found out that it is not necessary to set the nginx's or lets encrypt sll_protocols to TLS 1.2 only in my case.
I hope if someone else runs into this issue, they will find this post :)

Swift - urlsession cookies

I have a php webAPI which works well and I want to login with that in my app. I want to use cookies for that. So, I save the cookies when the user signs in and that works. I store it in userdefaults when I want to use only place cookies into HTTPCookieStorage.shared.
I try to use this for my new request, and my question is how can I add the cookies to my request?
I tried this but it's not working...
let cookiesArray = HTTPCookieStorage.shared.cookies
print(cookiesArray)
//HTTPCookieStorage.shared.setCookies(cookiesArray!, for: url, mainDocumentURL: url)
let headers = HTTPCookie.requestHeaderFields(with: cookiesArray!)
var urlRequest = URLRequest(url: url)
urlRequest.httpMethod = "POST"
urlRequest.httpShouldHandleCookies = true
urlRequest.httpBody = postString.data(using: String.Encoding.utf8)
urlRequest.allHTTPHeaderFields = headers
//urlRequest.setValue("PHPSESSID=oe22uf92poc5c7s23u4ukl83g0", forHTTPHeaderField: "Cookie")
//URLSessionConfiguration().httpCookieAcceptPolicy = .always
let session = URLSession.shared
session.configuration.httpCookieStorage = HTTPCookieStorage.shared
session.configuration.httpCookieAcceptPolicy = .always
session.configuration.httpShouldSetCookies = true
session.configuration.httpAdditionalHeaders = headers
let task = session.dataTask(with: urlRequest) { (data, response, error) in
print(data)
print(response)
print(error)
print("itt az end\n")
}
task.resume()
I can't comment yet, but I agree that more info is needed. If you are getting errors those would be helpful to post. Also, what you are getting for your
data
response
error
would be helpful.
Other than that, I would first look at setting up Charles as an HTTP Proxy so you can see exactly what is happening when you make the request. Knowing what response you are receiving will let you know what is going wrong.
https://www.charlesproxy.com/

Making HTTP Request with header in Swift

I am trying to make an HTTP request to the Imgur API. I am trying to retrieve all images associated with the tag "cats." The url, according to the Imgur API is: https://api.imgur.com/3/gallery/t/cats
the Imgur API states the following about the authorization needed to make get requests:
For public read-only and anonymous resources, such as getting image
info, looking up user comments, etc. all you need to do is send an
authorization header with your client_id in your requests. This also
works if you'd like to upload images anonymously (without the image
being tied to an account), or if you'd like to create an anonymous
album. This lets us know which application is accessing the API.
Authorization: Client-ID YOUR_CLIENT_ID
I've looked at the following questions and tried things suggested there, but none of them have helped.
JSON NSURLRequest with credentials
Swift GET request with parameters
How to make a Http get and set httpHeader in Swift?
My current code is this:
let string = "https://api.imgur.com/3/gallery/t/cats"
let url = NSURL(string: string)
let request = NSMutableURLRequest(URL: url!)
request.setValue("clientIDhere", forHTTPHeaderField: "Authorization")
//request.addValue("clientIDhere", forHTTPHeaderField: "Authorization")
request.HTTPMethod = "GET"
let session = NSURLSession.sharedSession()
let tache = session.dataTaskWithRequest(request) { (data, response, error) -> Void in
if let antwort = response as? NSHTTPURLResponse {
let code = antwort.statusCode
print(code)
}
}
tache.resume()
But I continually get a status code of 403, meaning authorization is required. What am I doing wrong?
I think you need to prepend Client-ID string to your actual client ID as for the header value:
request.setValue("Client-ID <your_client_id>", forHTTPHeaderField: "Authorization")
Updated for swift 4 :
func fetchPhotoRequest(YOUR_CLIENT_ID: String) {
let string = "https://photoslibrary.googleapis.com/v1/albums"
let url = NSURL(string: string)
let request = NSMutableURLRequest(url: url! as URL)
request.setValue(YOUR_CLIENT_ID, forHTTPHeaderField: "Authorization") //**
request.httpMethod = "GET"
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
let session = URLSession.shared
let mData = session.dataTask(with: request as URLRequest) { (data, response, error) -> Void in
if let res = response as? HTTPURLResponse {
print("res: \(String(describing: res))")
print("Response: \(String(describing: response))")
}else{
print("Error: \(String(describing: error))")
}
}
mData.resume()
}

NSURLRequest produce different result than HTTP proxy client

I send the same HTTP message from a HTTP proxy client and with NSURLRequest + NSURLConnection, and get back different result. It is an authentication request. From HTTP proxy authentication request is accepted, sending from app not. Why? Accepted means after redirection HTML will contains no Oops substring.
let url = NSURL(string: "http://www.swisshttp.weact.ch/en/user/login")
let request = NSMutableURLRequest(URL: url)
request.HTTPMethod = "POST"
request.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
let email2 = (viewController!.email.text as NSString).stringByReplacingOccurrencesOfString("#", withString: "%40")
let str = "name=\(email2)&pass=\(viewController!.password.text)&form_id=user_login" as NSString
let d = str.dataUsingEncoding(NSUTF8StringEncoding)
if let d2 = d {
request.HTTPBody = d2
let urlConnection = NSURLConnection(request: request, delegate: self)
}
UPDATE
I have put #teamnorge's code below into playground and into an empty Single View Application project. Returned HTML in project contains the Oops substring, code used in playground not containes it, any idea what is going on, why same request produce different HTML result? I get failed message also from iOS device and from simulator too.
UPDATE
Removed NSURLRequest cache like here recommended, but still not works as expected. And here.
UPDATE
Tried to remove all the credentials like here, but didn't help, no credential was found.
It looks like when you receive HTTP 302 and new Location URL, iOS does automatically fetch the page by this URL, so I guess your response is in fact the HTML content of the redirection page. Please verify.
UPDATE:
import UIKit
import XCPlayground
let url = NSURL(string: "http://www.swisshttp.weact.ch/en/user/login")
let request = NSMutableURLRequest(URL: url!)
let str = "name=kukodajanos%40icloud.com&pass=jelszo&form_id=user_login" as NSString
let d = str.dataUsingEncoding(NSUTF8StringEncoding)
request.HTTPBody = d
request.HTTPMethod = "POST"
request.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.currentQueue()) { response, maybeData, error in
if let data = maybeData {
let contents = NSString(data:data, encoding:NSUTF8StringEncoding)
println(contents)
if contents!.rangeOfString("Oops").length == 0 {
println("success")
} else {
println("failed")
}
} else {
println(error.localizedDescription)
}
}
XCPSetExecutionShouldContinueIndefinitely()

Resources