从 iOS 应用程序扩展发出网络请求返回错误:“找不到具有指定主机名的服务器”

Asp*_*pen 17 iphone xcode ios firebase swift

我正在尝试从 Google MLKit 下载语言翻译模型。我可以在主 iOS 应用程序上成功下载模型,但是当我尝试在键盘扩展应用程序上下载它时,我收到一条错误:“找不到具有指定主机名的服务器。”

testKeyboard[43687:3987699] [] networkd_settings_read_from_file Sandbox is preventing this process from reading networkd settings file at "/Library/Preferences/com.apple.networkd.plist", please add an exception.
testKeyboard[43542:3979636] [discovery] [d 6BF0C0BD-3B78-43CD-A23F-26023D59A4F3] <PKHost:0x283acc400> Failed discovery: Error Domain=PlugInKit Code=4 "Connection invalid" UserInfo={NSLocalizedDescription=Connection invalid}.
testKeyboard[43542:3979636] [xpc] XPC error sending request to pkd: Connection invalid
testKeyboard[43542:3979662] [] nw_resolver_can_use_dns_xpc_block_invoke Sandbox does not allow access to com.apple.dnssd.service
testKeyboard[43542:3979662] dnssd_clientstub ConnectToServer: connect() failed path:/var/run/mDNSResponder Socket:9 Err:-1 Errno:1 Operation not permitted
testKeyboard[43542:3979662] [connection] nw_resolver_create_dns_service_locked [C1] DNSServiceCreateDelegateConnection failed: ServiceNotRunning(-65563)
testKeyboard[43542:3979662] Task <38FA4936-22C4-4E80-BECC-1D592945BE52>.<1> HTTP load failed, 0/0 bytes (error code: -1003 [10:-72000])
testKeyboard[43542:3979662] Task <38FA4936-22C4-4E80-BECC-1D592945BE52>.<1> finished with error [-1003] Error Domain=NSURLErrorDomain Code=-1003 "A server with the specified hostname could not be found." UserInfo={_kCFStreamErrorCodeKey=-72000, NSUnderlyingError=0x2821c14a0 {Error Domain=kCFErrorDomainCFNetwork Code=-1003 "(null)" UserInfo={_kCFStreamErrorCodeKey=-72000, _kCFStreamErrorDomainKey=10}}, _NSURLErrorFailingURLSessionTaskErrorKey=LocalDownloadTask <38FA4936-22C4-4E80-BECC-1D592945BE52>.<1>, _NSURLErrorRelatedURLSessionTaskErrorKey=(
    "LocalDownloadTask <38FA4936-22C4-4E80-BECC-1D592945BE52>.<1>"
), NSLocalizedDescription=A server with the specified hostname could not be found., NSErrorFailingURLStringKey=https://redirector.gvt1.com/edgedl/translate/offline/v5/high/r24/en_es.zip, NSErrorFailingURLKey=https://redirector.gvt1.com/edgedl/translate/offline/v5/high/r24/en_es.zip, _kCFStreamErrorDomainKey=10}
error downloading Optional(Error Domain=com.google.mlkit Code=2 "Failed to download the model from the server." UserInfo={NSLocalizedDescription=Failed to download the model from the server.})

Run Code Online (Sandbox Code Playgroud)

我根据KeyboardViewController 的 viewDidLoad() 函数中Google 的“在 iOS 上使用 MLKit 翻译文本”文档,使用相同的代码下载模型:

        let options = TranslatorOptions(sourceLanguage: .english, targetLanguage: .spanish)
        self.translator = Translator.translator(options: options)
        let conditions = ModelDownloadConditions(allowsCellularAccess: true, allowsBackgroundDownloading: true )
        self.translator!.downloadModelIfNeeded(with: conditions) { error in
            guard error == nil else {
                print("error downloading", error)
                return
            }
            print("Model downloaded successfully")
        }
Run Code Online (Sandbox Code Playgroud)

我的 Info.plist 设置是这样的:

  1. “RequestsOpenAccess”为 1
  2. “应用程序传输安全设置”>“允许任意加载”为“是”

信息.plist 文件

我不明白这是怎么回事。为什么我可以在主应用程序上下载 ML Kit 语言模型,但不能在键盘扩展上下载?任何帮助表示赞赏。

and*_*ewz 24

问题在于您的项目Sandbox设置。转到您的project page / Signing & Capabilities / App Sandbox(Xcode 13.2),然后启用,Outgoing Connections (Client)这将允许您的应用程序进行 http 调用。

在此输入图像描述

  • 调试和发布都需要执行此操作 (6认同)