转自:http://evgenii.com/blog/sharing-keychain-in-ios/
如有侵犯,请来信[email protected]
使用了KeyAccess这个第三方库保存UUID,原来设计是用了access group,可以多个app共享的,
可惜真机运行时出错了,log提示:
"Internal error when a required entitlement isn't present, client has neither application-identifier nor keychain-access-groups entitlements."
找了资料:https://github.com/kishikawakatsumi/KeychainAccess/issues/52
说了半天,都没搞好,里面的这篇链接(看下文),貌似是能解决的,可是我这里好像是设置不了苹果网页管理那里的group 权限,当前是disable,
想着暂时也不需要多个app共享uuid,就去掉了keyaccess里的参数,只管保证本app能拿回来相同的UUID就好了。
Keychain is a secure storage suitable for short bits of sensitive information like passwords or credit card numbers. By default the data saved in one app can not be read in other apps. In most cases this is exactly what we need.
However, sometimes we need to share Keychain items between multiple apps on the same device. For instance, suppose we created two apps where users can log into the same account. It would be nice to have ability to share the login information between these apps. This way the user will only need to log in once in one of the apps.
Fortunately, there is a way to share Keychain items between apps and it can be done by using Keychain Groups. Here is how to do it. The following steps need to be done in all apps where you want to share the Keychain items.
Expand the Keychain Sharing capability settings and you will see your app's bundle ID used as the keychain group. You may want to change it to something meaningful to you, for example myKeychainGroup1. Remember the group you entered because you will use it in the code later.
Knowing the group name you entered is not enough for sharing the Keychain. You also need to know your App ID Prefix which is added to the beginning of the group name. In order to see how it works click on the.entitlements file and look at the value of the Keychain Access Groups array.
You will notice that the access group value has the form of$(AppIdentifierPrefix)myKeychainGroup1
. The$(AppIdentifierPrefix)
part will be replaced by your App ID Prefix. The problem is that at this stage we probably have no clue what it is, but we will soon find out.
The App ID Prefix (also called Team ID) is a unique text identifier associated with your Apple developer account that allows to share keychain and pasteboard items between your apps. Here is how to find what it is:
Now you know your App ID prefix and the full Keychain access group name will look like this:
AB123CDE45.myKeychainGroup1
We have enabled the Keychain sharing and found out the keychain group name. Now we can delete, add and retrieve shared Keychain items. To work with Keychain you can use one of many Keychain libraries that support access groups (Keychain Swift is one). But here I will show how to do it directly using the built-in Keychain API. The main rule is to pass the access group name with the kSecAttrAccessGroup
query key intoSecItemAdd
, SecItemDelete
and SecItemCopyMatching
functions.
let itemKey = "My key"
let itemValue = "My secretive bee