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 ".