简体   繁体   English

根据用户是否登录来管理iOS应用UI状态

[英]Managing iOS app UI state based on user being logged in or not

I have an app which presents a login screen on first launch. 我有一个应用程序在首次启动时显示登录屏幕。 When the user logs in, I give them an option to remain logged in. This establishes a session with an expiry on my server. 当用户登录时,我给他们一个保持登录状态的选项。这会在我的服务器上建立一个到期的会话。 What's the most appropriate way to do the following things: 做以下事情最合适的方法是什么:

  1. Store whether the user is logged in or not. 存储用户是否已登录。
  2. Present the user with a login or logout option on application launch based on the validity of their session. 根据会话的有效性,在应用程序启动时向用户显示登录或注销选项。
  3. End their current session if they choose to logout (or if their session is expired). 如果他们选择退出(或者他们的会话已过期),请结束当前会话。

I'm guessing this is a common design pattern and there should be tried and tested ways to do this but I seem to be using the wrong terms to search because I haven't found a satisfactory answer. 我猜这是一个常见的设计模式,应该有尝试和测试的方法来做到这一点,但我似乎使用错误的术语来搜索,因为我没有找到一个满意的答案。

Some ideas: 一些想法:

  1. I would suggest you store your sensitive session information in the application's KeyChain. 我建议您将敏感会话信息存储在应用程序的KeyChain中。 I wouldn't store here the state of wether the user is logged in or not, just store that in memory. 我不会在这里存储用户登录的状态,只是将其存储在内存中。 Your webservice should be able to return an error when the session ceases to exist, or if the user has logged out. 当会话不再存在或用户已注销时,您的Web服务应该能够返回错误。
  2. If the backend determines the session's validity, then you should have a RESTful call where you can pass the session information, returning whether the session is still valid. 如果后端确定会话的有效性,那么您应该有一个RESTful调用,您可以在其中传递会话信息,返回会话是否仍然有效。
  3. Again, if they choose to logout, then you could perform another call to your backend passing the session information. 同样,如果他们选择退出,那么您可以对后端执行另一次调用,传递会话信息。

For the Keychain, use the KeychainItemWrapper from Apple's examples. 对于Keychain,请使用Apple示例中的KeychainItemWrapper

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM