android - 为 Kivy 应用程序保存登录屏幕用户名和密码

标签 android python ios parse-platform kivy

我正在开发适用于 iOS 和 Android 的 Kivy 应用程序,需要帮助以保持用户持续登录,即使在应用程序关闭或终止后也是如此。我正在使用 Parse 来存储用户凭据。

我已经在 App 类中添加了一个 on_pause 方法,但这只会让用户在应用程序关闭但未被终止时保持登录状态。是否有最佳实践可以安全地允许用户使用 Kivy 进行持久登录,即使在应用程序被终止后也是如此?

编辑:我更喜欢同时适用于 Android 应用和 iOS 应用的单一 Kivy 解决方案,无需编辑/添加 iOS 或 Android 特定代码。

最佳答案

下面是我们最终用来存储登录信息的代码,它使用了 Kivy 的 JsonStore。然后还可以使用 Python 加密库对凭据进行加密。

from kivy.storage.jsonstore import JsonStore

from os.path import join


class AppScreen(ScreenManager):
    data_dir = App().user_data_dir
    store = JsonStore(join(data_dir, 'storage.json'))
    ...
    def login(self):
        username = self.login_username.text
        password = self.login_password.text
        AppScreen.store.put('credentials', username=username, password=password)

这是检索凭据的代码:

try:
    store.get('credentials')['username']
except KeyError:
    username = ""
else:
    username = store.get('credentials')['username']

try:
    store.get('credentials')['password']
except KeyError:
    password = ""
else:
    password = store.get('credentials')['password']

在 .kv 文件中,用户名和密码 TextInput 小部件如下所示:

TextInput:
    id: login_username
    text: root.username
    on_enter_key: root.login()

TextInput:
    id: login_password
    text: root.password
    on_enter_key: root.login()

关于android - 为 Kivy 应用程序保存登录屏幕用户名和密码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25147323/

相关文章:

android - 有人知道有多少 Android 设备支持 NEON 吗?

android - 在 AsyncTask 中获取 IndexOutOfBoundException

python - pytest KeyError:docker容器内的 'USER'

python - SWIG:使用自定义代码包装对 C++ 方法的调用?

ios - 使用 SDWebImage 加载大 gif 导致 iOS 应用程序因内存错误而崩溃

java - 当应用程序在后台长时间运行后返回时,静态对象将变为空

android - 在 Android 上使用 rawQuery SELECT .. WHERE ... IN

python - 将 24 位整数转换为 32 位整数的更多 Pythonic 方式

ios - Xcode UIView rotation : How to set the new position coordinates to 0, 0 旋转后不移动图像?

ios - 在搜索期间隐藏导航栏项目