为什么它会缓存响应.它返回先前获取的响应.它甚至可以在关闭网络连接时工作.重置iOS模拟器似乎也不起作用.提出请求,然后再次使用Internet脱机工作(缓存).
public let urlSession: NSURLSession
public init()
{
// ...
var configuration = NSURLSessionConfiguration.defaultSessionConfiguration()
configuration.requestCachePolicy = NSURLRequestCachePolicy.ReloadIgnoringLocalCacheData
urlSession = NSURLSession(configuration: configuration)
}
func makeRequest(path: String, httpMethod:String, body:String?, baseUrl: String?, headers:[String:String]=[:], callback: JsonRequestCallback)
{
let urlString = (baseUrl ?? customOAuth2Manager.API_URL) + path
let url = NSURL(string: urlString)
let request = oauthInstance.request(forURL: url!)
request.HTTPMethod = httpMethod
self.customOAuth2Manager.setupRequest(request)
for (key, value) in headers {
request.setValue(value, forHTTPHeaderField:key)
}
if let body = body where body != "" {
let postData = (body as NSString).dataUsingEncoding(NSUTF8StringEncoding) …Run Code Online (Sandbox Code Playgroud) caching nsurlcache nsurlsession nsurlsessionconfiguration swift
我有一个简单的应用程序,我以这种方式从存储在我自己的服务器中的 JSON 文件获取数据 - 我正在使用SwiftyJSON:
func queryData(_ fileName:String) {
guard let url = URL(string: JSON_PATH + fileName + ".json") else {return} // JSON_PATH + fileName + ".json" is the complete path to my db.json file (see below)
let task = URLSession.shared.dataTask(with: url) { (data, response, error) in
guard let dataResponse = data, error == nil else {
self.simpleAlert(error!.localizedDescription)
return
}
let jsonData = try! JSON(data: dataResponse)
print("ARRAY: \(jsonData)")
}
task.resume()
}
Run Code Online (Sandbox Code Playgroud)
这是我的db.json文件:
[
{
"objID":"GNoW3vszYz",
"string":"First …Run Code Online (Sandbox Code Playgroud)