如何在多个iOSapp里共享数据

转自: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就好了。

How to share Keychain between iOS apps


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.

1) Turn on Keychain sharing in Xcode

  1. Select your app target and click Capabilities tab.
  2. Turn on the Keychain Sharing capability.

如何在多个iOSapp里共享数据_第1张图片

2) Select developer team

如何在多个iOSapp里共享数据_第2张图片

3) Specify Keychain group name

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.

如何在多个iOSapp里共享数据_第3张图片

4) App ID Prefix and Keychain group name

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.

如何在多个iOSapp里共享数据_第4张图片

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.

5) What is my App ID Prefix?

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:

  • Login to you Apple developer account (also known as Member Center)developer.apple.com/account.
  • Open the Certificates, Identifiers & Profiles page.
  • Tap the App IDs link under Identifiers.
  • Tap on an existing app ID or create a new one and you will find an App ID prefix there.

如何在多个iOSapp里共享数据_第5张图片

Now you know your App ID prefix and the full Keychain access group name will look like this:

AB123CDE45.myKeychainGroup1

6) Accessing shared Keychain items

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 intoSecItemAddSecItemDelete and SecItemCopyMatching functions.

Delete a shared Keychain item

let itemKey = "My key"
let itemValue = "My secretive bee 

你可能感兴趣的:(iOS零碎)