在Swift中访问Flutter SharedPreferences [英] Access Flutter SharedPreferences in Swift

查看:196
本文介绍了在Swift中访问Flutter SharedPreferences的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以访问在插件的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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆