在Swift中访问Flutter SharedPreferences [英] Access Flutter SharedPreferences in Swift
问题描述
是否可以访问在插件的Swift代码中访问的Flutter中保存的SharedPreferences
?在Android中,SharedPreferences
具有FILE
模式.
Swift 4有任何类似的功能吗?
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?
推荐答案
shared_preferences
使用NSUserDefaults
在iOS上存储数据.您可以像这样通过Swift轻松访问它:
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)
}
请注意,如果在flutter/dart代码中使用键test
,则需要在键中添加flutter.
前缀,如此行在源代码中)
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)
这篇关于在Swift中访问Flutter SharedPreferences的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!