ios 如何在 iphone 应用程序中创建和管理会话?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14702836/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 22:09:26  来源:igfitidea点击:

How to create and manage session in iphone app?

iphoneiossession

提问by Shaunak

I have an application which uses wcf web service and after successful login, i want to activate a session of 10 minutes. If user is idle or app is in background for 10 minutes then session should expire and app should ask for login details again.

我有一个使用 wcf Web 服务的应用程序,成功登录后,我想激活 10 分钟的会话。如果用户空闲或应用程序在后台运行 10 分钟,则会话应过期,应用程序应再次询问登录详细信息。

I have used keychain for storing username and password and it works But i want to add the session so that user is redirected to main page if session is not expired and to the login page if session is expired...

我已经使用钥匙串来存储用户名和密码并且它可以工作但是我想添加会话,以便在会话未过期时将用户重定向到主页,如果会话已过期则重定向到登录页面...

I know this is repeated question but i had to write it since i am not getting proper answer from other questions. Please help..

我知道这是一个重复的问题,但我不得不写它,因为我没有从其他问题中得到正确的答案。请帮忙..

回答by Dixit Patel

use this Linkthis works me.

使用此链接这对我有用。

use NSUserDefaultto create Session for ios.

使用NSUserDefault为 ios 创建会话。

i also created Session in ios with this Tutorial.

我还使用本教程在 ios 中创建了 Session。

EDIT:

编辑:

i have a Login Screen in first Page of My application

我的应用程序的第一页中有一个登录屏幕

if Login is Successfull authenticate by server then i stored my Username & Password in NSUserDefaultLike this way:

如果登录成功通过服务器进行身份验证,那么我将我的用户名和密码存储在NSUserDefault 中,如下所示:

 NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
     [defaults setObject:userNameText forKey:@"username"];               
     [defaults setObject:passWordText forKey:@"password"];
     [defaults synchronize];

Onclickof Login Button. & redirected to Dashboard.

单击登录按钮。& 重定向到仪表板。

In dashboard Logout Button is there if user click LogoutDelete All Data Like this way.

如果用户以这种方式单击注销删除所有数据,则在仪表板注销按钮中。

 NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults removeObjectForKey:@"username"];
    [defaults removeObjectForKey:@"password"];
    [defaults synchronize];

& On other condition when user close app The NSUSerDefaultAre stored when u second time open app at that time Check wheather username& passwordstored in NSUserDefaultDuring ViewDidAppearof Loginpage.

与在其他条件当用户关闭应用程序的NSUSerDefault存储当u在那个时候入住wheather第二次打开应用程序的用户名密码存储在NSUserDefault期间ViewDidAppearLoginpage

  NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

    // check if user is alraidy Login
    if([defaults objectForKey:@"username"]!=nil  && ![[defaults objectForKey:@"username"] isEqualToString:@""]){
        // Redirected to Dashboard.
}

If sucessfull then Redirected to Dashboard.

如果成功,则重定向到仪表板。

回答by Tr??ng Minh Nh?t

You can use AFNetworking framework, it auto store your session, I had tested my APIs and it worked

您可以使用AFNetworking 框架,它会自动存储您的会话,我已经测试了我的 API 并且它有效