将用户ID存储在内存IOS中 [英] Store userid in memory IOS

查看:63
本文介绍了将用户ID存储在内存IOS中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我制作了一个具有登录功能的IOS应用程序.我为此使用ASIHTTPRequest来检查用户是否存在于MySQL数据库中.一切正常,但我想通过其他viewcontrollers传递userid或在应用程序的其他部分上检索它.

I made an IOS Application with a login function. I use the ASIHTTPRequest for this to check if the user exists in a MySQL database. All works fine but i want to pass the userid trough other viewcontrollers or retrieve it on other parts of the app.

有人可以向我正确的方向如何以最佳和最安全的方式做到这一点吗?

Can someone push me in the right direction how to do this in the best and most secure way?

这是我对PHP脚本执行POST HTTP请求的方式:

This is my the way i do a POST HTTP request to a PHP script:

ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:@"*SOME_URL*"]];

在回调中,我可以检索用户ID,但是我想将此ID存储在应用程序的内存中以备将来使用.

In a callback i can retrieve the users ID, but i want to store this ID in the memory of the app for further use.

谢谢.

推荐答案

您可以将userId存储在AppDelegate中.

使用以下代码在AppDelegate.h中声明userId属性:

Declare a userId property in AppDelegate.h with the following code:

@property (nonatomic, strong) NSString *userId

,然后在AppDelegate.m中使用以下代码对其进行合成:

And then synthesize it in AppDelegate.m with the following code:

@synthesize userId;


然后您可以在应用程序中的任何位置使用userId.在要使用userId的类中为AppDelegate添加导入,如下所示:


You can then use userId from anywhere in your app. Add an import for the AppDelegate in the class you want to use userId, as follows:

#import "AppDelegate.h"

要检索userId,请使用以下代码:

To retrieve the userId use the following code:

AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
NSString *userId = [appDelegate userId];

要设置userId,请使用以下代码:

And to set the userId use the following code:

AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
[appDelegate setUserId:YouValueRetrievedByInternetOrWhateverYouWant];

这篇关于将用户ID存储在内存IOS中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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