无法为本地推送连接配置 NEAppPushManager

Unable to configure the NEAppPushManager for Local Push Connectivity

我正在尝试配置我的应用程序,以便使用 WWDC 2020 中描述的“本地推送连接”session。

我使用“网络扩展”授权创建了我的应用和扩展。

在我的扩展的 Info.plist 中,我添加了所需的配置:

    <key>NSExtension</key>
    <dict>
        <key>NSExtensionPointIdentifier</key>
        <string>com.apple.networkextension.app-push</string>
        <key>NSExtensionPrincipalClass</key>
        <string>$(PRODUCT_MODULE_NAME).PushProvider</string>
    </dict>

我创建了如下扩展 class:

class PushProvider: NEAppPushProvider {
    ...
}

现在,在 AppDelegate 中,我尝试按如下方式配置扩展程序:

let pushManager = NEAppPushManager()
pushManager.matchSSIDs = ["SSID Name"]
pushManager.localizedDescription = "PushProvider"
pushManager.providerBundleIdentifier = "---my-product.name----.PushProvider"
pushManager.delegate = self
pushManager.isEnabled = true
pushManager.providerConfiguration = [
      "host": "192.168.1.94"
]
pushManager.saveToPreferences(completionHandler: { error in
    print("error? \(error)")
    print("is active: \(pushManager.isActive)")
})

saveToPreferences方法总是returns以下错误:

2021-04-15 12:39:59.416074+0200 PushTest[2452:411559] [] : Failed to get the configuration index while saving configuration PushProvider: Error Domain=NEConfigurationErrorDomain Code=10 "permission denied" UserInfo={NSLocalizedDescription=permission denied}
2021-04-15 12:39:59.416437+0200 PushTest[2452:411556] [] <NEAppPushManager: 0x283807e10>: Failed to save the configuration: Error Domain=NEConfigurationErrorDomain Code=10 "permission denied" UserInfo={NSLocalizedDescription=permission denied}
error? Optional(Error Domain=NEAppPushErrorDomain Code=3 "(null)")
is active: false

我不明白我的配置有什么问题,因为我似乎遵循了配置的所有必要步骤。

有人用过iOS的这个特殊功能吗?

根据 Apple 的 Local Push Connectivity page

To use the Local Push Connectivity API, your app must have the Network Extensions Entitlement with the app-push-provider value. Request this entitlement from the Entitlement Request Page. After you receive the entitlement, apply it to both your app target and your provider extension target.

根据我的经验,最后一步意味着在您的 Apple Developer account 中使用上述权利为应用程序和网络扩展手动创建配置文件。然后下载配置文件并将它们导入 Xcode 以用于应用程序目标和本地推送网络扩展目标。 (因此,关闭配置文件的自动 Xcode 管理。)