简体   繁体   English

OSStatus 错误:[-34018] 设备上不存在所需权利时的内部错误

[英]OSStatus error:[-34018] Internal error when a required entitlement isn't present error on device

I am trying to implement keychain sharing using KeyChainAccess.我正在尝试使用 KeyChainAccess 实现钥匙串共享。 I have build two very basic applications: App One that writes a string to a shared keychain, and App Two that reads the data from the shared keychain and displays it.我已经构建了两个非常基本的应用程序:将字符串写入共享钥匙串的应用程序一,以及从共享钥匙串读取数据并显示它的应用程序二。

My code for App One:我的应用程序一代码:

    override func viewDidLoad() {
    super.viewDidLoad()

    //save item to keychain
    let keychain = Keychain(service: "app.test", accessGroup: "xxxxx.xxxxx.xxxxx.Keychain-Sharing")

    do {
        try keychain.set("Some Data Set in app one", key: "sharedData")
        print("Success")
        label.text = "Success"
    }
    catch let error {
        print("Keychain write failed: \(error)")
        label.text = "Keychain write failed: \(error)"
    }

}

My code for App Two that reads and displays from the shared keychain:我的应用程序二代码从共享钥匙串中读取和显示:

override func viewDidLoad() {
    super.viewDidLoad()

    //load item from keychain
    let keychain = Keychain(service: "app.test", accessGroup: "xxxxx.xxxxx.xxxxx.Keychain-Sharing")

    let data = try? keychain.get("sharedData")

    print("Data from Keychain: \(data ?? "nil")")

    label.text = "Data from Keychain: \(data ?? "nil")"
}

This is a very basic example just to try the concept, however what I am finding is that when I run it using Xcode's simulator on my Mac it behaves as expected, I run the first app - it succeeds, I then run the second app and the correct string is displayed.这是一个非常基本的示例,只是为了尝试这个概念,但是我发现当我在我的 Mac 上使用 Xcode 的模拟器运行它时,它的行为符合预期,我运行第一个应用程序 - 它成功,然后我运行第二个应用程序和显示正确的字符串。

When I then try to run it on a device (by plugging my device into my Mac and changing the run location I receive the following error when trying to write and read from the shared keychain:然后,当我尝试在设备上运行它时(通过将我的设备插入我的 Mac 并更改运行位置,我在尝试从共享钥匙串中写入和读取时收到以下错误:

   OSStatus error:[-34018] Internal error when a required entitlement isn't present, client has neither application-identifier nor keychain-access-groups entitlements.

I have checked my entitlements file and I can see that they are included in both applications:我检查了我的权利文件,我可以看到它们包含在两个应用程序中:

在此处输入图像描述

And the entitlements files is referenced correctly in the build settings:并且在构建设置中正确引用了权利文件: 在此处输入图像描述

Also when I hover over my provisioning profile it tells me that they are included:此外,当我在我的配置文件上 hover 时,它告诉我它们包括在内:

在此处输入图像描述

Is this what is allowing it to run successfully in the Xcode simulator?这是允许它在 Xcode 模拟器中成功运行的原因吗? and what am I missing to get it to run on the handset?我错过了什么让它在手机上运行? I think this is something to do with my certificates / profiles but I am fairly new to this so I am not certain as to exactly what I need / missing or what to check?我认为这与我的证书/个人资料有关,但我对此相当陌生,所以我不确定我需要/缺少什么或要检查什么?

Can anyone help or point me in the right direction?任何人都可以帮助或指出我正确的方向吗?

I had the same issue.我遇到过同样的问题。 And the solution was very easy.解决方案非常简单。 Check once again accessGroup parameter in Keychain initializer.再次检查 Keychain 初始化程序中的 accessGroup 参数。 For example, you add the Keychain Group in Capabilities with the name "com.myCompany.app".例如,您在 Capabilities 中添加名称为“com.myCompany.app”的钥匙串组。 But it is not the full name of accessGroup.但它并不是accessGroup的全称。 To solve the issue just prepend "App ID prefix" to the keychain group name.要解决此问题,只需在钥匙串组名称前添加“App ID 前缀”。 You can find this prefix in your apple developer account inside your App ID configuration.您可以在 App ID 配置中的 Apple 开发者帐户中找到此前缀。 The full accessGroup name will be "XXXXXXXXXX.com.myCompany.app", where XXXXXXXXXX is your App ID prefix.完整的访问组名称将为“XXXXXXXXXX.com.myCompany.app”,其中 XXXXXXXXXX 是您的 App ID 前缀。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM