shared_preferences_ios_sn
包装了 Flutter shared_preferences 插件,并提供了 iOS Suite Name 支持,有助于将数据从 App 共享到 iOS Widget。
为什么需要“Suite Name”功能?
当您需要将数据从您的 App 共享到 iOS Widget 时
用法
在 pubspec.yaml 中
shared_preferences_ios_sn: ^1.0.2
#shared_preferences: x.x.x //should comment 'shared_preferences'
并运行以下命令安装插件
flutter pub get
在 Flutter 代码中
//final prefs = SharedPreferences.getInstance();
//use this to instead of above, prefs will works like a normal SharedPreferences instance in many different platforms.
final prefs = await SPUtil.withSuitName("<Your iOS GROUP NAME>");
prefs.setString("test_key", "test_value");
在 iOS Widget 的 Swift 代码中
//now you can get the SharedPreferences data from App in your widget code, for example:
//dont forget to add prefix "flutter." to the key:
print("test_key:",UserDefaults(suiteName: "<Your iOS GROUP NAME>")?.string(forKey: "flutter.test_key"))