【问题标题】:ios simulator access localhost serverios模拟器访问localhost服务器
【发布时间】:2014-09-06 16:31:21
【问题描述】:

我正在尝试将休息数据获取到 ios 应用程序,并且我使用此代码:

    var rest_url = "http://192.168.0.1:8000/rest/users/"

    let url: NSURL = NSURL(string: rest_url)
    let session = NSURLSession.sharedSession()
    let task = session.dataTaskWithURL(url, completionHandler: {data, response, error -> Void in
    if(error != nil) {
        println(error.localizedDescription)
    }
    println(data)
    var err: NSError?

    var jsonResult = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: &err) as NSDictionary!

但是我认为我无法像这样访问我的服务器,有人知道我如何从 ios 模拟器访问我的服务器吗?

【问题讨论】:

  • 本地主机是127.0.0.1。您的网络服务器可能只监听该 IP。
  • The operation couldn’t be completed. (NSURLErrorDomain error -1004.) <> JSON Error The operation couldn’t be completed. (Cocoa error 3840.) fatal error: unexpectedly found nil while unwrapping an Optional value

标签: ios swift localhost nsurlsession


【解决方案1】:

您的 Info.plist 文件中是否有应用传输安全设置? 如果没有,那么出于调试目的,您可以将它们设置为

<key>NSAppTransportSecurity</key>
    <dict>
        <key>NSAllowsArbitraryLoads</key>
        <true/>
    </dict>

此类设置允许向任何服务器发出请求。 但不要为发布版本这样做。这是不安全的。

【讨论】:

    【解决方案2】:

    如果您在运行 iOS 模拟器的机器上运行服务器,那么您需要选择“http://127.0.0.1”作为 URL。

    你的情况是:

    var rest_url = "http://127.0.0.1:8000/rest/users/"
    

    【讨论】:

      【解决方案3】:

      对于因证书无效而无法连接到本地主机而发现此线程的人:在您的URLSessionDelegate 中,您应该使用以下委托方法回复URLAuthenticationChallenge

      func urlSession(_ session: URLSession, didReceive challenge: URLAuthenticationChallenge, completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) {
      
          func defaultAction() { completionHandler(.performDefaultHandling, nil) }
      
          // Due to localhost using an invalid certificate, we need to manually accept it and move on
          guard challenge.protectionSpace.host.hasPrefix("localhost") else { return defaultAction() }
          guard challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodServerTrust else { return defaultAction() }
          guard let trust = challenge.protectionSpace.serverTrust else { return defaultAction() }
      
          completionHandler(.useCredential, URLCredential(trust: trust))
      }
      

      【讨论】:

        【解决方案4】:

        用 ios 模拟器调试时,可能你可以将 192.168.0.1 替换为 localhost(即真实设备应该使用你服务器的 IP)。

        我也无法使用模拟器上的 IP 地址访问我的测试服务器。但是当我使用 localhost 或 120.0.0.1 时,模拟器可以很好地与我的测试服务器配合使用。

        【讨论】:

          【解决方案5】:

          确保您的 Mac 的 IP 为 192.168.0.1。所以你的网址可能是

          var rest_url = "http://YOUR MAC IP:8000/rest/users/"
          

          【讨论】:

          • 我在 192.168.0.13 上使用端口 8000 运行 python 服务器,我尝试使用该 url 和 127.0.0.1:8000,但没有任何效果:/ The operation couldn’t be completed. (NSURLErrorDomain error -1004.) &lt;&gt; JSON Error The operation couldn’t be completed. (Cocoa error 3840.) fatal error: unexpectedly found nil while unwrapping an Optional value
          猜你喜欢
          • 2016-07-09
          • 1970-01-01
          • 2018-10-27
          • 1970-01-01
          • 1970-01-01
          • 2013-12-13
          • 2018-11-10
          • 2017-03-20
          • 2014-10-29
          相关资源
          最近更新 更多