iOS 中的 NSURLErrorDomain 错误代码 -999
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16073519/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
NSURLErrorDomain error code -999 in iOS
提问by user1597438
I've been trying to use Corona SDK's Facebook API to post the score on the game I'm developing on facebook. However, I'm having a problem with it. During the first time I try to post to facebook, I get this error after login and user authentication:
我一直在尝试使用 Corona SDK 的 Facebook API 来发布我在 facebook 上开发的游戏的分数。但是,我遇到了问题。在我第一次尝试发布到 facebook 时,我在登录和用户身份验证后收到此错误:
NSURLErrorDomain error code -999
NSURLErrorDomain 错误代码 -999
Then, it won't post on facebook. What are possible causes of this error and how can I address it? I tried searching the web but couldn't find information about it. Thanks in advance.
然后,它不会在facebook上发布。此错误的可能原因是什么,我该如何解决?我尝试在网上搜索,但找不到有关它的信息。提前致谢。
By the way, I am not using webview on my app. Just the widget api and a show_dialog listener in my Facebook class.
顺便说一下,我没有在我的应用程序上使用 webview。只是我的 Facebook 课程中的小部件 api 和一个 show_dialog 侦听器。
回答by hjpotter92
The error has been documented on the Mac Developer Library(iOS docs)
该错误已记录在Mac 开发人员库(iOS 文档)中
The concerned segment from the documentation will be:
文档中的相关部分将是:
URL Loading System Error Codes
These values are returned as the error code property of an NSError object with the domain “NSURLErrorDomain”.
enum { NSURLErrorUnknown = -1, NSURLErrorCancelled = -999, NSURLErrorBadURL = -1000, NSURLErrorTimedOut = -1001,
URL 加载系统错误代码
这些值作为域“NSURLErrorDomain”的 NSError 对象的错误代码属性返回。
enum { NSURLErrorUnknown = -1, NSURLErrorCancelled = -999, NSURLErrorBadURL = -1000, NSURLErrorTimedOut = -1001,
As you can see; -999
is caused by ErrorCancelled
. This means: another request is made before the previous request is completed.
如你看到的; -999
是由 引起的ErrorCancelled
。这意味着:在前一个请求完成之前发出另一个请求。
回答by thanhbinh84
hjpotter92 is absolutely right, I just want to provide solution for my case. Hopefully it is useful for you as well. Here is my situation:
hjpotter92 是绝对正确的,我只是想为我的案例提供解决方案。希望它对你也有用。这是我的情况:
On log in page > press log in > pop up loading dialog > call log in service > dismiss dialog > push another screen > call another service --> cause error -999
在登录页面>按登录>弹出加载对话框>调用登录服务>关闭对话框>推送另一个屏幕>调用另一个服务->导致错误-999
To fix it, I put a delay between dismissing dialog and pushing new screen:
为了解决这个问题,我在关闭对话框和推送新屏幕之间设置了一个延迟:
[indicatorAlert dismissWithClickedButtonIndex:0 animated:YES];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 0.01 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
[self performSegueWithIdentifier:@"HomeSegue" sender:nil];
});
It is strange that this issue happens on iOS 7 only.
奇怪的是,这个问题只发生在 iOS 7 上。
回答by Ramon
Just wanted to add here, when receiving a -999 "cancelled"
the problem usually is one of two things:
只是想在这里补充一下,当收到-999 "cancelled"
问题时,通常是两件事之一:
- You're executing the exact same request again.
- You're maintaining a weak reference to your
manager
object that gets deallocated prematurely. (Create strong reference)
- 您再次执行完全相同的请求。
- 您正在维护对
manager
过早解除分配的对象的弱引用。(创建强引用)
回答by Pablo Blanco
I have faced the same error with Alamofire and it was because the certificate pinning. The certificate wasn't valid anymore, so I had to remove it and add the new one. Hope it helps.
我在使用 Alamofire 时遇到了同样的错误,这是因为证书固定。该证书不再有效,因此我不得不将其删除并添加新证书。希望能帮助到你。
回答by Dan Lee
Our company's app has many -999 error in iOS. I have searched around, find the reason has two, like the network task has been dealloc or the certificate isn't valid. But I have checked our code, these two aren't possible. I am using Alamofire which is using URLSession. Luckily, our company's android app's network is normal. So we check the difference. We found the http request from iOS is Http2.0, while android is Http1.1. So we force the backend http support version down to http1.1, then -999 error count descends!!!
我们公司的应用程序在iOS中有很多-999错误。找了一圈,发现原因有两个,比如网络任务被dealloc或者证书无效。但是我检查了我们的代码,这两个是不可能的。我正在使用使用 URLSession 的 Alamofire。还好我们公司的android app的网络是正常的。所以我们检查差异。我们发现来自iOS的http请求是Http2.0,而android是Http1.1。所以我们强制后端http支持版本降到http1.1,然后-999错误计数下降!!!
I think there maybe some bug in Apple's URLSession. Check the link New NSURLSession for every DataTask overkill?for some detail thoughts
我认为 Apple 的 URLSession 中可能存在一些错误。检查每个 DataTask 矫枉过正的链接New NSURLSession?一些细节的想法
回答by Chris Graf
In addition to what Ramonwrote, there is a third possible reason when receiving a NSURLErrorDomain -999 cancelled
:
除了Ramon所写的内容之外,在收到NSURLErrorDomain -999 cancelled
:时还有第三种可能的原因:
You cancelled the task while it was executing either by calling .cancel()
on the datatask object or because you used .invalidateAndCancel()
on the session object. If you are creating a custom session with a delegate, you should call .invalidateAndCancel()
or .finishTasksAndInvalidate()
to resolve the strong reference between the session and its delegate, as mentioned in the Apple Developer Documentation:
通过调用.cancel()
datatask 对象或因为您.invalidateAndCancel()
在会话对象上使用,您在它执行时取消了该任务。如果您要使用委托创建自定义会话,则应调用.invalidateAndCancel()
或.finishTasksAndInvalidate()
解决会话与其委托之间的强引用,如Apple 开发人员文档中所述:
The session object keeps a strong reference to the delegate until your app exits or explicitly invalidates the session. If you don't invalidate the session, your app leaks memory until it exits.
会话对象保持对委托的强引用,直到您的应用程序退出或显式地使会话无效。如果您不使会话无效,您的应用程序就会泄漏内存,直到它退出。
If you are wondering about this logging behaviour, I found the following explanation in the Apple Developer forums:
如果您对这种日志记录行为感到疑惑,我在Apple Developer 论坛中找到了以下解释:
By way of explanation, back in iOS 10 we introduced a new logging system-wide logging architecture (watch WWDC 2016 Session 721 Unified Logging and Activity Tracingfor the details) and lots of subsystem, including CFNetwork, are in the process of moving over to that. Until that move is fully finished you're going to encounter some weird edge cases like this one.
作为解释,在 iOS 10 中,我们引入了一个新的日志系统范围的日志架构(观看WWDC 2016 Session 721 Unified Logging and Activity Tracing了解详细信息)和许多子系统,包括 CFNetwork,正在迁移到那。在这一举措完全完成之前,你会遇到一些像这样的奇怪的边缘情况。
回答by ilovecomputer
I didn't use Corona SDK's Facebook API but I encountered this problem when using Alamofire, the secondRequest
always cancel in execution with the error -999, according to the posts I found on internet, the reason is that session
property is deinit
before completion of async work since it is out of the scope, I finally solved this problem by deinit
the session property manually so the compiler won't deinit it at wrong position:
我没有使用 Corona SDK 的 Facebook API,但是我在使用 Alamofire 时遇到了这个问题secondRequest
,根据我在互联网上找到的帖子,总是取消执行并出现错误 -999,原因是该session
属性deinit
在异步工作完成之前它超出了范围,我终于通过deinit
session 属性手动解决了这个问题,这样编译器就不会在错误的位置取消初始化它:
class SessionManager {
var session:SessionManager?
init() {
self.session = SessionManager(configuration:URLSessionConfiguration.ephemeral)
}
private func firstRequest() {
guard let session = self.session else {return}
session.request(request_url).responseData {response in
if let data=response.data {
self.secondRequest()
}
}
private func secondRequest() {
guard let session = self.session else {return}
session.request(request_url).responseData {response in
if let data=response.data {
self.secondRequest()
}
//session will no longer be needed, deinit it
self.session = nil
}
}
回答by Victor Thomas Wilcox Jr.
I was getting this error in iOS specific version of Xamarin app. Not sure the underlying cause, but in my case was able to work around it by using post method instead of get for anything passing the server context in the request body -- which makes more sense anyway. Android / Windows / the service all handle the GET with content, but in iOS app will become partially unresponsive then spit out the 999 NSUrlErrorDomain stuff in the log. Hopefully, that helps someone else running into this. I assume the net code is getting stuck in a loop, but could not see the code in question.
我在 Xamarin 应用程序的 iOS 特定版本中收到此错误。不确定根本原因,但在我的情况下,可以通过使用 post 方法而不是 get 来解决它,因为它可以在请求正文中传递服务器上下文——无论如何这更有意义。Android / Windows / 该服务都处理带有内容的 GET,但在 iOS 应用程序中将变得部分无响应,然后在日志中吐出 999 NSUrlErrorDomain 内容。希望这有助于其他人遇到这个问题。我假设网络代码陷入循环,但看不到有问题的代码。
回答by Vyrnach
For my Cordova project (or similar), turns out it was a plugin issue. Make sure you're not missing any plugins and make sure they're installed properly without issue.
对于我的Cordova 项目(或类似项目),结果是插件问题。确保您没有丢失任何插件,并确保它们安装正确而没有问题。
Easiest way to verify this is simply to start fresh by recreating the Cordova project(cordova create <path>
) along with the required platforms (cordova platform add <platform name>
) and add each plugin with the verbose flag (--verbose) so that you can see if anything went wrong in the console log while the plugin is being downloaded, added to project and installed for each platform (cordova plugin add cordova-plugin-device --verbose
)
验证这一点的最简单方法是通过重新创建 Cordova 项目( cordova create <path>
) 以及所需的平台 ( cordova platform add <platform name>
) 重新开始,并使用详细标志 (--verbose) 添加每个插件,以便您可以查看控制台日志中是否出现任何问题在下载插件、添加到项目并为每个平台安装时 ( cordova plugin add cordova-plugin-device --verbose
)
Recap:
cordova create <path>
cordova platform add <platform name>
cordova plugin add cordova-plugin-device --verbose
回顾:
cordova create <path>
cordova platform add <platform name>
cordova plugin add cordova-plugin-device --verbose