5

Is it possible to access SharedPreferences saved from Flutter accessed in Swift code of plugin? In Android we have FILE mode for SharedPreferences. Any similar feature in Swift 4?

CopsOnRoad
  • 237,138
  • 77
  • 654
  • 440
sandy
  • 3,311
  • 4
  • 36
  • 47

3 Answers3

14

The shared_preferences uses NSUserDefaults on iOS to store the data. You can easily access it with Swift like this:

let name = NSUserDefaults.standard.string(forKey: "flutter.test")
print(name)

It would also make sense to use the optional binding to get the value safely:

if let name = NSUserDefaults.standard.string(forKey: "flutter.test") {
    print(name)
}

Note, that if you use the key test in your flutter/dart code you would need to add the flutter. prefix to the key, as the shared_preferences plugin prefixes every key with it (see this line in the source code)

DAG
  • 6,710
  • 4
  • 39
  • 63
3

Use UserDefaults on Swift.

UserDefaults.standard.object(forKey:"flutter.key"))

key = key used em flutter to shared preferences. You need to use flutter prefix on key.

0

I am not sure there exist anything like that, but you don't even need that.

You can fetch the value in Flutter itself, and then send the value using MethodChannel.

CopsOnRoad
  • 237,138
  • 77
  • 654
  • 440