How to get shared_preferences in Share Extension

446 views Asked by At

I want to know how to get shared_preferences in iOS share extension (swift code).

I use 'shared_preferences' to store session date in this code.

SharedPreferences.getInstance().setString("session", "session_sata")

And, I tried to get 'SharedPreferences' iOS in share extension (swift) by using UserDefaults like this code .

UserDefaults.standard.persistentDomain(forName: "") 
UserDefaults.init(suiteName: "") 

However, I couldn't get session date in swift code, even more I couldn't find a key of "flutter.session".

Does anyone know how to get shared_preferences in iOS share extension?

Reference Access Flutter SharedPreferences in Swift

enter image description here

1

There are 1 answers

0
konnic On BEST ANSWER

To be able to share data between an iOS host app and its app extension you need to:

  1. Add the App Group capability/entitlement to the app and the extension
  2. Set up an instance of UserDefaults initialized from a suiteName: UserDefaults(suiteName: ${appGroupIdentifier})

And unfortunately this is not possible with the current state of the SharedPreferences pub. Here is the GitHub issue addressing this.

A possible solution to your problem is to try out this pub, which appears to be created to handle this use case: https://pub.dev/packages/shared_preference_app_group