Access Flutter SharedPreferences in Swift
1,321
Solution 1
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)
Solution 2
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.
Comments
-
sandy over 1 year
Is it possible to access
SharedPreferences
saved from Flutter accessed in Swift code of plugin? In Android we haveFILE
mode forSharedPreferences
. Any similar feature in Swift 4? -
CopsOnRoad over 4 yearsLooks very promising +1 :)