问题描述
我已经成功获取了令牌的钥匙串,并将其传递给如下所示的AccessTokenAdapter类.http127.0.0.1:8000/api2/projects/?format = json作为projectsURL传递.
class AccessTokenAdapter: RequestAdapter {
private let accessToken: String
init(accessToken: String) {
self.accessToken = accessToken
}
func adapt(_ urlRequest: URLRequest) throws -> URLRequest {
var urlRequest = urlRequest
// print("JWT \(accessToken)")
urlRequest.setValue("JWT \(accessToken)", forHTTPHeaderField: "Authorization")
return urlRequest
}
}
let sessionManager = SessionManager()
sessionManager.adapter = AccessTokenAdapter(accessToken: self.keychain["token"]!)
sessionManager.request(self.projectsURL, method: .get, encoding: JSONEncoding.default).responseJSON{ response in
switch response.result {
case .success:
print("yey I made it")
case .failure(let error):
print(error)
}
}
但是,从print(error),我的Xcode显示如下错误错误域= NSURLErrorDomain代码= -999已取消" UserInfo = {NSErrorFailingURLKey = http://127.0.0.1:8000/api2/projects/?format=json ,NSLocalizedDescription = cancelled,NSErrorFailingURLStringKey = http127.0.0.1:8000/api2/projects/?format = json}
有什么想法吗?
Alamofire 4.0
钥匙扣
Xcode 8.1
Swift3
使用JWT进行身份验证
将邮递员与标头一起使用,密钥="Authentication",值="JWT(在此处生成令牌)"可以正常使用
当您将sessionManager设为一个let常量时,它的生存期不会超过接受它的功能,因此,一旦释放经理,会话就会结束. >
要解决此问题,请延长sessionManager的寿命.例如,在我的情况下,我将其设置为类属性:
class NetworkRequest: {
var sessionManager = Alamofire.SessionManager()
...
func performRequest(_ call: APICall, customLink: String = "", parameters: Dictionary<String, Any> = ["":"" as Any]) {
let sessionConfig = URLSessionConfiguration.default
sessionConfig.timeoutIntervalForRequest = call.suggestedTimeout()
sessionConfig.timeoutIntervalForResource = call.suggestedTimeout()
sessionManager = Alamofire.SessionManager(configuration: sessionConfig)
sessionManager.adapter = AccessTokenAdapter()
sessionManager.request(urlString,
method: call.method(),
parameters: parameters,
encoding: call.chooseEncoding(),
headers: [:])
.responseJSON
{ response in
...
}
}
解决方案可能会因您的情况而有所不同,但其想法是保持sessionManager处于活动状态,直到网络请求结束.
I already have successfully got keychain for my token and passing it to AccessTokenAdapter class shown below.http127.0.0.1:8000/api2/projects/?format=json is passed as projectsURL.
class AccessTokenAdapter: RequestAdapter {
private let accessToken: String
init(accessToken: String) {
self.accessToken = accessToken
}
func adapt(_ urlRequest: URLRequest) throws -> URLRequest {
var urlRequest = urlRequest
// print("JWT \(accessToken)")
urlRequest.setValue("JWT \(accessToken)", forHTTPHeaderField: "Authorization")
return urlRequest
}
}
let sessionManager = SessionManager()
sessionManager.adapter = AccessTokenAdapter(accessToken: self.keychain["token"]!)
sessionManager.request(self.projectsURL, method: .get, encoding: JSONEncoding.default).responseJSON{ response in
switch response.result {
case .success:
print("yey I made it")
case .failure(let error):
print(error)
}
}
however, from print(error), my Xcode shows error likeError Domain=NSURLErrorDomain Code=-999 "cancelled" UserInfo={NSErrorFailingURLKey=http://127.0.0.1:8000/api2/projects/?format=json, NSLocalizedDescription=cancelled, NSErrorFailingURLStringKey=http127.0.0.1:8000/api2/projects/?format=json}
Any ideas?
Alamofire 4.0
Keychain
Xcode 8.1
Swift3
Using JWT for authentication
Using Postman with header, key = "Authentication", value = "JWT (token generated here)" works fine
When you make sessionManager a let constant it won't live longer than the embracing it function, thus the session ends as soon as the manager is deallocated.
To fix this, make sessionManager live longer. For example in my case I made it a class property:
class NetworkRequest: {
var sessionManager = Alamofire.SessionManager()
...
func performRequest(_ call: APICall, customLink: String = "", parameters: Dictionary<String, Any> = ["":"" as Any]) {
let sessionConfig = URLSessionConfiguration.default
sessionConfig.timeoutIntervalForRequest = call.suggestedTimeout()
sessionConfig.timeoutIntervalForResource = call.suggestedTimeout()
sessionManager = Alamofire.SessionManager(configuration: sessionConfig)
sessionManager.adapter = AccessTokenAdapter()
sessionManager.request(urlString,
method: call.method(),
parameters: parameters,
encoding: call.chooseEncoding(),
headers: [:])
.responseJSON
{ response in
...
}
}
The solution might be different according to your situation, but the idea is to keep sessionManager alive until the network request ends.
这篇关于Alamofire错误域= NSURLErrorDomain代码= -999“已取消";的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!