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

标签 ios swift apple-push-notifications

我正在尝试配置我的应用,以便使用 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 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 方法总是返回以下错误:

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 管理。)

关于ios - 无法为本地推送连接配置 NEAppPushManager,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67107228/

相关文章:

ios - 隐藏通过 NotificationService Extension 收到的通知

ios - IBOutlets 和自定义 View

ios - 分享到 Facebook 墙

ios - 发送到释放实例的消息

Swift - 如何在同一函数中为两个不同的 SKNodes 非同时(以线性模式)运行 2 个 Action

swift - 选择生成不同的 SKSpriteNode

ios - Ionic - OneSignal 不适用于 iOS 13.x

ios - Xcode 会在调试时影响应用程序性能吗?

ios - 如何在解析中检索单列数据?

ios - 如果我撤销 iOS 分发将停止实时应用程序上的 APNS?