【发布时间】:2014-10-28 09:31:25
【问题描述】:
我使用 Xcode beta6。 我创建了一个具有 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)
}
}
当服务器通过 LAN 电缆连接到网络时,此类运行良好并获得正确的 JSON 结果,但是当此服务器通过 WiFi 连接到同一网络时,我从 iOS 设备收到此错误:
Error Domain=NSURLErrorDomain Code=-1005 "The network connection was lost."
但这真的很奇怪,因为如果我将 json 路径粘贴到浏览器中,我会看到 json .. 所以只有在 iOS 设备上无法处理,但我不知道我应该修复什么.. 谁能帮帮我?
所以,如果我用来开发的 Mac mini 在 LAN 上,而服务器在 LAN 上,那么一切正常。 但是当我的 Mac mini 在 WiFi 上并且我的服务器在 Wifi 上时,我得到了这个错误......
【问题讨论】:
-
退出模拟器并重新运行您的项目。
标签: ios swift nsurlconnection nsurl nsurlconnectiondelegate