xcode - OS X 小部件模拟器中没有网络连接 (HTTPS)

标签 xcode swift

我正在使用 Swift 构建一个 OS X Today Widget。这应该通过 HTTPS 调用 JSON API 并显示结果。到目前为止一切顺利。

但是当我在 Widget Simulator 中运行 widget 时,我总是收到错误“找不到具有指定主机名的服务器”。 URL 是正确的,当我在命令行上执行 curl 时它可以工作。 Widget Simulator 似乎没有正确的 DNS 查找或完全阻止 https 流量。

我正在使用 Alamofire,但我也尝试过使用 NSURLSession。相同的结果。

let url = NSURL(string: "https://httpbin.org/get")

let task = NSURLSession.sharedSession().dataTaskWithURL(url!) {(data, response, error) in
  debugPrint(error)
}

task.resume()

导致:

Optional(
    Error 
      Domain=NSURLErrorDomain 
      Code=-1003 "A server with the specified hostname could not be found." 
      UserInfo={
        NSUnderlyingError=0x7fe1f3e341a0
        ErrorDomain=kCFErrorDomainCFNetwork 
        Code=-1003 "(null)" 
        UserInfo={
          _kCFStreamErrorCodeKey=-72000,
          _kCFStreamErrorDomainKey=10}
        }, 
        NSErrorFailingURLStringKey=https://httpbin.org/get, 
        NSErrorFailingURLKey=https://httpbin.org/get,
        _kCFStreamErrorDomainKey=10, 
        _kCFStreamErrorCodeKey=-72000,
        NSLocalizedDescription=A server with the specified hostname could not be found.
     }
)

Widget Simulator 应该可以进行外部连接,是吗?

最佳答案

这是一个非常古老的问题,所以很确定你已经有了答案,但我之前遇到了我认为是同样的问题,我花了很长时间才弄清楚。希望这会帮助其他人更快地解决它。

您可以使用小部件模拟器连接到 https。我的版本是版本 1.0 (552.6)。我得到的错误是:2017-01-07 14:35:26.062370 MYAPP [18387:5719297] dnssd_clientstub ConnectToServer: connect() failed path:/var/run/mDNSResponder Socket:5 Err:-1 Errno:1 Operation not permitted

事实证明,您必须打开ON App Sandbox 并明确允许网络进出。我想我应该阅读文档:https://developer.apple.com/library/content/documentation/IDEs/Conceptual/AppDistributionGuide/AddingCapabilities/AddingCapabilities.html#//apple_ref/doc/uid/TP40012582-CH26-SW3

扩展程序有自己的权利,因此请确保配置每个需要网络的目标。

Turning ON Sandbox allows network connection

关于xcode - OS X 小部件模拟器中没有网络连接 (HTTPS),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34926060/

相关文章:

objective-c - 将变量传递给 AppleScript

iphone - 带有标签栏的半透明覆盖

ios - 如何在我的标签中显示多个文本字段?

swift - Mixpanel初始化错误: argument passed to call that takes no arguments

xcode - 在 Swift 中禁用应用程序小睡

Swift SpriteKit 获取可见帧大小

ios - Xcode 7/Swift : Save value of slider and send to child class when view changes

xcode - IOS 6 Xcode 4.5 MFMailComposer 崩溃

javascript - native IOS 应用程序从网络浏览器 javascript 接收数据

c - 从 C 到 Swift 的函数回调