Share keychain data with different apps

Source: Internet
Author: User

1. Create two projects, testwritekeychain and testreadkeychain. (add the Security. Framework library and use it to read the keychain). Then add the apple official keychainitemwrapper. h and keychainitemwrapper. M files to access the keychain.

2. Change the bundle identifier of the. plist file of the two projects to com. companyName. App. testwritekeychain and COM. companyName. App. testreadkeychain;

3. Change the entitlements of the two projects. Add the preceding two bundle identifier in the keychain access groups of the entitlements of the two projects. For example:

5. In the testwritekeychain project

-(Bool) Application :( uiapplication *) Application didfinishlaunchingwitexceptions :( nsdictionary
*) Add the following code to the launchoptions function:

Keychainitemwrapper * keychain = [keychainitemwrapperalloc]
Initwithidentifier: @ "_ test_write_keychain _" accessgroup: Nil]; // note that the accessgroup is nil,

// Read for the first time. The keychaindata should be nil because it is not set before.


Id keychaindata = [keychain objectforkey ID) ksecattraccount];

Nslog (@ "first keychaindata: % @,
% @, % D ", keychaindata, [keychaindataclass], [(nsstring
*) Keychaindatalength]);

// Write the corresponding data to the keychain. Here we write a string @ "testwritekeychain"

[Keychain setobject: @ "testwritekeychain" forkey :( ID) ksecattraccount];

// The second read, because it is not set before, keychaindata should be @ "testwritekeychain"

Keychaindata = [keychain objectforkey :( ID) ksecattraccount];

Nslog (@ "second keychaindata
: % @, % @, % D ", keychaindata, [keychaindata class], [(nsstring *) keychaindata length]);

After testwritekeychain is run, the-(bool) Application :( uiapplication *) Application in testreadkeychain
Didfinishlaunchingwitexceptions :( nsdictionary *) Add the following code to the launchoptions function:


Keychainitemwrapper * keychain = [[keychainitemwrapper alloc] initwithidentifier: @ "_ test_write_keychain _" accessgroup: Nil];

Id keychaindata = [keychain objectforkey ID) ksecattraccount];

Nslog (@ "first keychaindata: % @, % @, % d", keychaindata,
[Keychaindata class], [(nsstring *) keychaindata length]);

You can also read @ "testwritekeychain ".

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.