如何通过Native App维护与AngularJS网站的会话? [英] How to Mantain Session with AngularJS Website from Native App?

查看:88
本文介绍了如何通过Native App维护与AngularJS网站的会话?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发几乎完全本地化的iOS应用.

I am working on an iOS app that is almost completely native.

但是,应用程序的一部分只是具有即可成为网络视图.

However, there is one part of the app that simply has to be a web view.

我需要做的是让用户选择添加到购物车中的项目,这些项目存储在AngularJS网站上的会话中.选择项目后,他们只是打开一个带有结帐页面的Web视图,该页面已经知道从应用程序中选择了哪些项目.

What I need to do is have a user select items that are added to a cart stored in a session on an AngularJS website. After they select the items, they just open a web view with the checkout page that already knows what items have been selected from within the app.

这是怎么做到的?

谢谢

推荐答案

我的建议是在sessioncookies上使用local storage,因为浏览器中每个选项卡的会话都不相同,并且cookie具有会话(时间删除).本地存储值将保留在那里,直到有人删除它们为止.为简单起见,我们使用简单的javascript代码访问angularjs中的本地存储.

My suggestion is to use local storage over session and cookies as session is different for every tab in the browser and cookies are have sessions(time to get deleted). Local storage values stay there until someone delete them. For simplicity, we use plain javascript code to access local storage in angularjs.

关于您的问题,关于用户登录存储的用户ID和浏览器本地存储中的密码.

Coming to your question, on user login store user id, and password in local storage of the browser.

例如,我们可以直接在angularjs控制器中调用

For example, we can call like this directly in a angularjs controller

localStorage.setItem(key, value)//存储值

localStorage.setItem(key, JSON.stringify(value))//用于存储对象

localStorage.getItem(key)//检索值

JSON.parse(localStorage.getItem(key))//检索对象

根据您的要求的示例

localStorage.setItem('userId', userId)

var user = localStorage.getItem('userId')

if(user){ //if user already logged in
  // retrieve user relevant data from DB, show home page directly or whatever you want. 
}

我认为,如果将选定的项目存储在本地存储中会存在安全风险.但是,如果您也想存储它们,只需在选择确认时将它们存储即可.

I think there will be security risk if you store selected items in local storage. But if you want to store them as well, just store them on selecting confirmation.

localStorage.setItem('selectedItems', JSON.stringify(selectedItems))我以selectedItems为对象.根据您的要求检索它们.

localStorage.setItem('selectedItems', JSON.stringify(selectedItems)) I assumed selectedItems as an object. Retrieve them based on your requirement.

这篇关于如何通过Native App维护与AngularJS网站的会话?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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