[英]Alamofire 4 FAILURE: Error Domain=NSURLErrorDomain Code=-999 "cancelled"
Hi I am try to use Alamofire for my project but the error come out.嗨,我正在尝试将 Alamofire 用于我的项目,但出现错误。 Here is my requesting code //Google testing这是我的请求代码 //Google 测试
Alamofire.request("http://google.com").responseString{
response in
debugPrint(response)
}.session.invalidateAndCancel()
Result]: FAILURE: Error Domain=NSURLErrorDomain Code=-999 "cancelled"结果]:失败:错误域=NSURLErrorDomain 代码=-999“已取消”
UserInfo={NSErrorFailingURLKey= http://google.com/ , UserInfo={NSErrorFailingURLKey= http://google.com/ ,
NSLocalizedDescription=cancelled, NSLocalizedDescription=取消,
NSErrorFailingURLStringKey= http://google.com/ } NSErrorFailingURLStringKey= http://google.com/ }
//Own server testing //自己的服务器测试
Alamofire.request("https://10.68.24.127:4533").responseString{
response in
debugPrint(response)
}.session.invalidateAndCancel()
same result相同的结果
class NetworkManager {
var manager: SessionManager?
init() {
let serverTrustPolicies: [String: ServerTrustPolicy] = [
"https://10.68.24.127:4533" : .disableEvaluation
]
let configuration = URLSessionConfiguration.default
manager = Alamofire.SessionManager(
configuration: configuration,
serverTrustPolicyManager :ServerTrustPolicyManager(policies: serverTrustPolicies)
)
}
}
I set the NSAllowsArbitraryLoads to true and NSExceptionDomains.我将 NSAllowsArbitraryLoads 设置为 true 和 NSExceptionDomains。
Where is the problem?问题出在哪儿?
Check this documentation from Alamofire app-transport-security 从Alamofire app-transport-security查看此文档
try to add the following in your .plist file 尝试在您的.plist文件中添加以下内容
<key>NSAppTransportSecurity</key><dict>
<key>NSExceptionDomains</key>
<dict>
<key>url.com</key>
<dict>
<key>NSExceptionAllowsInsecureHTTPLoads</key>
<true/>
<key>NSExceptionRequiresForwardSecrecy</key>
<false/>
<key>NSIncludesSubdomains</key>
<true/>
</dict>
</dict>
Most likely you should be checking if there is an implementation of authentication challenge delegate method and check if its calling NSURLSessionAuthChallengeCancelAuthenticationChallenge. 您很可能应该检查是否存在身份验证质询委托方法的实现,并检查其调用NSURLSessionAuthChallengeCancelAuthenticationChallenge的方法。
- (void)URLSession:(NSURLSession *)session didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge
completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition disposition, NSURLCredential * _Nullable credential))completionHandler
There could be a lot of reason to why your requests "cancelled".您的请求被“取消”的原因可能有很多。
If you are facing that a request cancels immediately you can refer to this issue in Alamofire repository issues如果您面临请求立即取消的问题,您可以在Alamofire 存储库问题中参考此问题
jshier commented on Oct 10, 2016 jshier于 2016 年 10 月 10 日发表评论
An unexpected error -999 almost always means your SessionManager was deallocated, cancelling any ongoing requests.意外错误 -999 几乎总是意味着您的 SessionManager 被释放,取消任何正在进行的请求。 I suggest you create a singleton value for your custom SessionManager, or perhaps just reevaluate if you really need one.我建议你为你的自定义 SessionManager 创建一个单例值,或者如果你真的需要一个,也许只是重新评估。
if you create a singleton value for your object it remains in memory and prevent from deallocate如果为对象创建单例值,它会保留在内存中并防止解除分配
and another thing that i avoid is to name your variables diffrent, a sessionManager is in Alamofire and your variable is also called sessionManager.我避免的另一件事是将您的变量命名为不同的名称,一个 sessionManager 在 Alamofire 中,您的变量也称为 sessionManager。
import Alamofire
class Networking {
public static let sharedManager: SessionManager = {
let configuration = URLSessionConfiguration.default
configuration.timeoutIntervalForRequest=20
let manager = Alamofire.SessionManager(configuration: configuration, delegate: SessionManager.default.delegate)
return manager
}()
}
import Alamofire
class Networking {
static let APIManager: Session = {
let configuration = URLSessionConfiguration.default
configuration.timeoutIntervalForRequest = 20
let delegate = Session.default.delegate
let manager = Session.init(configuration: configuration,
delegate: delegate,
startRequestsImmediately: true,
cachedResponseHandler: nil)
return manager
}()
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.