我正在使用以下代码将图像发布到我的服务器。
@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()
}
其中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.)"}
我了解到此错误是超时错误。重新启动应用程序或手机没有帮助。此外,我尝试从网络浏览器将图像发布到我的服务器,并且没有问题。
什么可能导致此超时错误?
编辑:当我监控我的网络活动时,我意识到应用程序发送了 10 MB 数据,即使我发送的图像是 0.1 MB。