我有一个适用于Xcode6-Beta1和Xcode6-Beta2的iOS7和iOS8应用程序.但是对于Xcode6-Beta3,Beta4,Beta5,我正面临着iOS8的网络问题,但在iOS7上一切正常.我收到了错误"The network connection was lost.".错误如下:
错误:错误域= NSURLErrorDomain代码= -1005"网络连接丢失." UserInfo = 0x7ba8e5b0 {NSErrorFailingURLStringKey =,_ kCFStreamErrorCodeKey = 57,NSErrorFailingURLKey =,NSLocalizedDescription =网络连接丢失.,_ kCFStreamErrorDomainKey = 1,NSUnderlyingError = 0x7a6957e0"网络连接丢失."}
我使用AFNetworking 2.x和以下代码片段进行网络调用:
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager setSecurityPolicy:policy];
manager.requestSerializer = [AFHTTPRequestSerializer serializer];
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
[manager POST:<example-url>
parameters:<parameteres>
success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@“Success: %@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];
Run Code Online (Sandbox Code Playgroud)
我试过NSURLSession但仍然收到同样的错误.
我使用以下代码将图像发布到我的服务器.
@IBAction func postButtonPressed(sender: UIButton) {
let task = NSURLSession.sharedSession().dataTaskWithRequest(createRequest("http://xx.xx.xxx.xxx/xxxx/"), completionHandler: {
data, response, error in
println(NSString(data: data, encoding: NSUTF8StringEncoding))
})
task.resume()
}
Run Code Online (Sandbox Code Playgroud)
在哪里createRequest()创建所需的NSURLRequest对象.
当我使用模拟器时,这很好用.问题是,当我在iPhone上运行我的应用程序时,我收到以下错误.
Error Domain=NSURLErrorDomain Code=-1001 "The operation couldn’t be completed. (NSURLErrorDomain error -1001.)" UserInfo=0x155e71f0 {NSErrorFailingURLKey=http://xx.xxx.xxx.xxx/xxxx/, NSErrorFailingURLStringKey=http://54.148.156.117/query/, NSUnderlyingError=0x155674d0 "The operation couldn’t be completed. (kCFErrorDomainCFNetwork error -1001.)"}
Run Code Online (Sandbox Code Playgroud)
我了解到这个错误是超时错误.重新启动应用程序或手机没有帮助.此外,我已经尝试从Web浏览器将图像发布到我的服务器,它没有问题.
可能导致此超时错误的原因是什么?
编辑:当我监控我的网络活动时,我意识到应用程序发送10 MB数据,即使我发送的图像是0.1 MB.
服务器:带有 nodejs express 和 nginx 代理的 Ubunta。
使用我的 android 应用程序时一切正常。
使用我的 iOS 应用程序时,我得到:
“错误调用 GET on /district Error Domain=NSURLErrorDomain Code=-1001 “请求超时。” UserInfo={NSUnderlyingError=0x1c0458db0 {Error Domain=kCFErrorDomainCFNetwork Code=-1001 “(null)” UserInfo={_kCFStreamErrorCodeKey=-2102, _kCFStreamErrorDomainKey=4}}, NSErrorFailingURLStringKey= http://xxx.xxx.xxx.xxx/api/district , NSErrorFailingURLKey= http://xxx.xxx.xxx.xxx/api/district , _kCFStreamErrorDomainKey=4, _kCFStreamErrorCodeKey=-2102 , NSLocalizedDescription=请求超时。}"
此错误仅在我执行 GET 请求时发生。我可以登录(POST)并将标记添加到我的地图等。一切正常。但是通过 get 请求获取数据不是。
我认为问题与 iOS 有 30 秒超时/在 iOS 6 或 7 上的设备上保持活动状态有关。它会自动设置一个标头以保持活动状态 30 秒。
在我的 mac localhost 服务器上工作时,我遇到了类似的问题。但是我通过用 res.set("Connection", "close"); 发回一个标头来修复它。在我的 nodejs 路由回 iOS。像这样:
nodejs DistrictRoutes.js
router.get('/api/district', passport.authenticate('jwt', { session:false }), function(req, res, next) {
log.info("inside getting districts");
District.find({}, …Run Code Online (Sandbox Code Playgroud)