简体   繁体   English

为什么NSURLConnection失败,错误域= NSURLErrorDomain Code = -1005“网络连接丢失。”在Swift iOS8中?

[英]Why NSURLConnection failed with Error Domain=NSURLErrorDomain Code=-1005 “The network connection was lost.” in Swift iOS8?

I use Xcode beta6. 我使用Xcode beta6。 I created an app which have a Downloader class, and this is the Downloader class: 我创建了一个具有Downloader类的应用程序,这是Downloader类:

class Downloader : NSObject {

    private var _connection : NSURLConnection?
    private var _downloadedData: NSMutableData?

    func getDataFromURLString(urlToRequest: String!, aType: DownloadedDataType) {

        _downloadedData = NSMutableData()

        var request : NSMutableURLRequest = NSMutableURLRequest(URL: NSURL(string: urlToRequest), cachePolicy: .ReloadIgnoringLocalCacheData, timeoutInterval: 20.0)
        request.setValue("", forHTTPHeaderField: "Accept-Encoding")

        self._connection = NSURLConnection(request: request, delegate:self)
    }

    func connection(connection: NSURLConnection!, didReceiveResponse response: NSURLResponse!) {
        println("Data expected size: \(response.expectedContentLength)")
    }

    func connectionDidFinishLoading(connection: NSURLConnection!) {
        println("finished")
    }

    func connection(connection: NSURLConnection!, didFailWithError error: NSError!) {
        println("error: \(error)")
    }

    func connection(connection: NSURLConnection!, didReceiveData data: NSData!)  {
        _downloadedData?.appendData(data)
    }

}

This class works well and get the right JSON result when the server is on the network with LAN cable but when this server connected to the same network via WiFi I get this error from the iOS device: 当服务器使用LAN电缆连接到网络时,此类运行良好并获得正确的JSON结果,但当此服务器通过WiFi连接到同一网络时,我从iOS设备收到此错误:

Error Domain=NSURLErrorDomain Code=-1005 "The network connection was lost."

But Its really weird because if I paste the json path to the browser I see the json.. So only on iOS devices cant handle, but I dont know what I should fix.. Can anyone help me? 但它真的很奇怪,因为如果我将json路径粘贴到浏览器上,我会看到json ..所以只有在iOS设备上无法处理,但我不知道应该修复什么..任何人都可以帮助我吗?

So If my Mac mini what I use to develop is on Lan, and the Server is on Lan, everything works fine. 所以,如果我使用的Mac mini是在Lan上开发的,而服务器在Lan上,那么一切正常。 But when my Mac mini is on WiFi and my server is on Wifi I get this error... 但是,当我的Mac mini在WiFi上而我的服务器在Wifi上时,我收到此错误...

Well, my first question is have you tried accomplishing the same task using the same logic/code in Objective-C / iOS 7? 那么,我的第一个问题是你是否尝试过在Objective-C / iOS 7中使用相同的逻辑/代码完成相同的任务? This would give us an idea if it's a problem in Swift, iOS 8, or a problem in your code. 如果它是Swift,iOS 8中的问题或代码中的问题,这将让我们知道。 If you have, please post this code in an edit/update. 如果有,请在编辑/更新中发布此代码。

Second question: Why are you overriding the accept-encoding? 第二个问题:为什么要重写接受编码? Many servers require something there if you specify the header value. 如果指定标头值,许多服务器需要一些东西。 Best to remove that. 最好删除它。

Third question: What version of iOS 8 beta are you running? 第三个问题:您运行的是哪个版本的iOS 8测试版? FWIW, a simple search of SO showed this question: Error Domain=NSURLErrorDomain Code=-1005 "The network connection was lost." FWIW,SO的简单搜索显示了这个问题: Error Domain = NSURLErrorDomain Code = -1005“网络连接丢失了。”

so it could be a bug in iOS 8 betas. 所以它可能是iOS 8测试版中的一个错误。 I'd suggest trying objective-c first, and if that also breaks, goto the developer forums and post your issue to get Apple's attention on it. 我建议首先尝试使用objective-c,如果这也打破了,请转到开发人员论坛并发布您的问题以引起Apple的注意。 You might also want to open a Radar for it. 您可能还想为它打开雷达。

I completely solved this problem by deleting my Wi-Fi network connection and re-instantiating it by choosing it from the network connection list and providing the password again. 我通过删除我的Wi-Fi网络连接并通过从网络连接列表中选择并再次提供密码来重新实例化,从而彻底解决了这个问题。 See my stack overflow response here at the bottom of the page: 请参阅页面底部的堆栈溢出响应:

NSURLConnection GET request returns -1005, "the network connection was lost" NSURLConnection GET请求返回-1005,“网络连接丢失”

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在移动网络上解析iOS SDK错误域= NSURLErrorDomain代码= -1005“网络连接丢失。” - Parse iOS SDK Error Domain=NSURLErrorDomain Code=-1005 “The network connection was lost.” on mobile network Error Domain = NSURLErrorDomain代码= -1005“网络连接丢失。”-在iOS 9中使用https - Error Domain=NSURLErrorDomain Code=-1005 “The network connection was lost.” - Occurs in iOS 9 with https 无法确定当前的国家/地区代码:错误域= NSURLErrorDomain代码= -1005“网络连接丢失。” - Could not determine current country code: Error Domain=NSURLErrorDomain Code=-1005 “The network connection was lost.” 随机获取Error Domain = NSURLErrorDomain代码= -1005“网络连接丢失。” - Randomly getting Error Domain=NSURLErrorDomain Code=-1005 “The network connection was lost.” NSURLErrorDomain代码= -1005“网络连接丢失。”,_ kCFStreamErrorCodeKey = 54 - NSURLErrorDomain Code=-1005 “The network connection was lost.”, _kCFStreamErrorCodeKey=54 名为“ NSURLErrorDomain代码= -1005”的网络连接丢失。 - Network error named “NSURLErrorDomain Code=-1005 ”The network connection was lost." comes too frequently Error Domain = NSURLErrorDomain代码= -1005“网络连接丢失 - Error Domain=NSURLErrorDomain Code=-1005 "The network connection was lost 错误代码=-1005“网络连接丢失。” 在 Swift 中同时使用 Web 服务 - Error Code=-1005 "The network connection was lost." in Swift while consuming Web Service Alamofire请求错误:NSURLErrorDomain代码-1005网络连接丢失 - Alamofire Request error: NSURLErrorDomain Code -1005 The network connection was lost NSURLErrorDomain代码= -1005“网络连接丢失 - NSURLErrorDomain Code=-1005 "The network connection was lost
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM