watchOS 2 上的钥匙串访问不适用于实际手表

Keychain access on watchOS 2 not working on the actual watch

我在 Apple 开发者论坛上了解到,与 watchOS 1 不同,watchOS 2 不与 phone 应用程序共享其钥匙串,所以 bam!!默认情况下不启用钥匙串共享,我们必须为此做一个解决方法。

好的,回到我的问题,我正在尝试 运行 实际手表设备上的一个非常基本的钥匙串访问程序 运行 使用 git 连接最新的测试版 (beta4)图书馆 https://github.com/jrendel/SwiftKeychainWrapper

  let saveSuccessful: Bool = KeychainWrapper.setString("keychainData", forKey: "ImportantKeychainData")

  if saveSuccessful{
       let retrievedString: String? = KeychainWrapper.stringForKey("ImportantKeychainData")
       print(retrievedString)
     }
     else
     {
       print("unable to write keychain data")
     }

在模拟器上它就像一个魅力但是当我尝试在实际手表上运行相同时它给我一个状态代码-34018

没有关于此错误代码的 public 文档,但我做了一些挖掘发现它原来是

errSecMissingEntitlement     = -34018,  /* Internal error when a required entitlement isn't present. */

来源:http://opensource.apple.com/source/Security/Security-55471/sec/Security/SecBasePriv.h

我为此做了很多研究,实际上一整天,人们向我指出了各个方向,例如内存问题、权利、配置文件问题、钥匙串中的错误等。

这里要注意的是,大多数报告此问题的开发人员并没有像我在应用程序的每个 运行 上一样持续遇到它,他们只在某些地方遇到它,就像当应用程序处于背景等。总结一下,

1. I tried the same piece of code on iOS 9 beta 4 and it worked well on the phone.

2. The same code works well on the watch simulator.

3. The same code does not work on watchOS beta 4 returns -34018 continuously on the device but works well on the simulator.

4. All this testing is done using free provisioning introduced from Xcode 7, entitlements were added to the phone app and the watch extension, keychain sharing was enabled, app groups was enabled.

我的问题是

1. Am I missing something here that I have to do with the device keychain that I am supposedly doing it wrong?

2. Is there an issue with free provisioning?

3. Is there an issue with the keychain perhaps??

感谢任何帮助。

仅供参考,我还尝试了 Apple 的 KeychainItemWrapper,直接与 SecItem 方法对话的海关代码没有结果。

更新,我也试过了,还是失败了

let storableString:NSString = "keychain in watchos is working with simple code"  
  let query : [NSString : AnyObject] = [  
            kSecClass : kSecClassGenericPassword,  
            kSecAttrService : "WatchService",  
            kSecAttrLabel : "KeychainData",  
            kSecAttrAccount : "SecureData",  
            kSecValueData : storableString.dataUsingEncoding(NSUTF8StringEncoding)!  
        ]  
  let result = SecItemAdd(query, nil)  
  print(result)  

更新 2:问题已在 watchOS2 beta 5 中修复。

Apple 在最近的 watchOS 2 beta 5 中修复了这个问题。