Skip to content

Flutter secure storage not storing data after I closed app IOS #532

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Stephnn0 opened this issue Mar 5, 2023 · 14 comments
Closed

Flutter secure storage not storing data after I closed app IOS #532

Stephnn0 opened this issue Mar 5, 2023 · 14 comments

Comments

@Stephnn0
Copy link

Stephnn0 commented Mar 5, 2023

I am having problems storing a token with flutter secure storage!

When I close the app or hot-restart the data gets lost or returns null

Any Suggestions?

-------------------------------------------------------------------- code

class Storage {
final storage = const FlutterSecureStorage();

final String _token = 'token';

Future setToken(String token) async {
const options =
IOSOptions(accessibility: KeychainAccessibility.first_unlock);
await storage.write(key: _token, value: token, iOptions: options);
}

Future<String?> getToken() async {
const options =
IOSOptions(accessibility: KeychainAccessibility.first_unlock);
return await storage.read(key: _token, iOptions: options);
}
}

@BraveEvidence
Copy link

This will help https://www.youtube.com/watch?v=JogvnnaJ8TI

@jannisnikoy
Copy link

jannisnikoy commented Mar 21, 2023

I'm noticing this in a specific scenario where I log out and log in of my app. So basically, we wipe the stored data, and then store a new token when logging in. After restarting/hot restart; same behavior as @stefanoHTB described. If I login and store a token after a clean start; there is no issue upon restart.

In my case, this problem occurs on both Android and iOS

@StigUK
Copy link

StigUK commented Mar 23, 2023

Hello, if you do getToken a second time, will the value still be null? I encountered a strange behavior that sometimes when I start the application, the value is returned null first time. However, it returns value after a certain amount of time when I try to do getToken again. And only on iOS.

@florentmorin
Copy link

Duplicates #524 ?

@phillsch
Copy link

phillsch commented Jul 6, 2023

Having the same problem.

Did anyone found a solution?

@codelovercc
Copy link

Having this same problem on MacOS. flutter secure storage version 9.0

@rydmike
Copy link

rydmike commented Feb 21, 2024

@codelovercc Same issue on macOS, did not test iOS yet.

  • flutter_secure_storage 9.0.0
  • macOS 14.3
  • Flutter 3.19.1
  • Dart 3.3.0
  • XCode 15.2

How are you all fixing this? I see no solution, at least not with an understandable explanation.

I'm beginning to think this PKG is not working well enough, pity. I will circle back to the issue later. I still need to test it on all platforms.

Edit:

  • Tested WEB release build, that works OK.

@codelovercc
Copy link

@rydmike Enable keychain this will help you. I've tested on macOS 12.7.1 (21G920) and Android Simulator API 34, it works.

@rydmike
Copy link

rydmike commented Feb 23, 2024

Thanks @codelovercc for the suggestion. I have tried enabling keychain on both iOS (17.0.1 (21A342) and macOS Version 14.3 (23D56) in all modes debug/profile/release. No luck.

I also tried with synchronizable: set to both to true and false.

    mOptions: MacOsOptions(      
      synchronizable: true,
      accessibility: KeychainAccessibility.first_unlock,
    ),
    iOptions: IOSOptions(      
      synchronizable: true,
      accessibility: KeychainAccessibility.first_unlock,
    ),

Made no difference, if it was true or false, or if that accessibility: KeychainAccessibility.first_unlock was there or not.

The flutter_secure_storage just always reads blank data when it starts up. While running the app it sets and gets data fine in it. But close the app and start it again, and it is gone.

@codelovercc
Copy link

@rydmike Can you try it in a new flutter project?

@hkh777uk
Copy link

Same problem occurring on Windows.

@rydmike
Copy link

rydmike commented Mar 10, 2024

@codelovercc I have tried, and also tried recreating the runner on macos, by deleting it and making a new one.

I have also tried this part with the AppIdentifierPrefix

Screenshot 2024-03-10 at 17 54 45

mentioned here: https://github.com/mogol/flutter_secure_storage
Which is not mentioned in the published readme here btw https://pub.dev/packages/flutter_secure_storage

All in all it did not help either.

I am a bit unsure of it should be the same as "Bundle identifier" in xcode, that is seems to put into Keychain Sharing under Keychain Groups. Xcode did that edit. Then I don't know if that should then also be used in MacOsOptions as groupId. Docs are very unclear on this, but I have tried pretty much every combo I can think of and nothing worked.

This package is a bit frustrating to use, since it seem to be close to impossible to get it to work on all platforms. Any help is of course appreciated, but I may need to come up with some alternative soonish, not urgent yet. Still not sure if there is any other good and reputable option available.

@rydmike
Copy link

rydmike commented Mar 10, 2024

Got it working on macOS. Here is how.

When you are using key-chain-access-groups and Xcode makes one based on your app bundle name e.g. com.domain.app you do need to have this in all your entitlements debug/profile/release as well:

	<key>keychain-access-groups</key>
	<array>
		<string>$(AppIdentifierPrefix)com.domain.app</string>
	</array>

And I did, but it was still not working.

However, do not use any groupID in your FlutterSecureStorage.mOptions:

    mOptions: MacOsOptions(
      synchronizable: true,
      accessibility: KeychainAccessibility.first_unlock,
    ),

At least for me adding any kind of groupId to MacOsOptions prevented it from working correctly. Removing it solved my issue.

    mOptions: MacOsOptions(
      groupId: 'com.domain.app', // NO NO! Did not matter what I used, any string made it not work.
      synchronizable: true,
      accessibility: KeychainAccessibility.first_unlock,
    ),

At least now it seems to work in my macOS builds as well and that is nice.
Thanks all for your input and help. Got the answer in some example usage and discussion on X/Twitter in case anybody want to dig into that, here it is https://x.com/RydMike/status/1766876648001986614?s=20

@juliansteenbakker
Copy link
Owner

I am closing all older issues. If this issue still exists in the latest version, please let me know.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

10 participants