My application has authorisation. In return if I logged in I receive a token which I store in NSUserDefaults
.
How current app works: When I press login button the app sends user name and login to the server, in return app gets a token if success.
I have a save user with token and other received info method which contains:
- (void)saveUserWithInfo...{
...
[[NSUserDefaults standardUserDefaults] setObject:token
forKey:kUserToken];
Everything works perfect on this stage with token in app.
For the iOS extension I want to use this token to check if user logged in or not. I've added next line to the save user method above:
NSUserDefaults *sharedDefaults = [[NSUserDefaults alloc] initWithSuiteName:@"group.com.mayapp"];
[sharedDefaults setObject:token
forKey:kUserToken];
As you can see I also save token to a shared user defaults, and I can simple request token from this new storage.
The problem:
When I test it first time it works as needed before I upload it to store. I had a version on App Store which did not contain iOS extensions and new share storage NSUserDefaults
which should contain token.
After I updated version to new one and download it, I opened at first extension, but it shown for me error, that user has not logged in, which is seems right because shared NSUserDefaults
storage does not have token. In my current workflow, token can be saved after I press login button. But after update from version without extension to version with extension from App Store, my user is still logged in of course for the app, because original NSuserDefaults
contains token, but new shared NSUserDefaults
still did not get token because we need logged out and then login to make it work.
My question is how to set token to new shared NSUserDefaults
without logout and then login back. Of course when I login it starts invoke all new lines from updates and there is my new shared NSUserDefaults
which I added for iOS extension. So after new login it will contain token and if open up extension it works, but first start immediately after updates version from App Store show error that user is not logged in - which is right because of new added shared NSUserDeafults
You should really be using the keychain.
Anyway, that said, you should have an 'upgrade' piece of logic in your app delegate (or triggered from there) which runs each time the app is launched and verifies the situation. In your current case the logic is to check the shared defaults for a login key and check the non-shared for the key. If the key exists in one and not the other, copy it and save.