将userid存储在内存IOS中

Joe*_*oey 0 xcode asihttprequest ios

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

有人能以正确的方向推动我如何以最好和最安全的方式做到这一点吗?

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

ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:@"*SOME_URL*"]];
Run Code Online (Sandbox Code Playgroud)

在回调中,我可以检索用户ID,但我想将此ID存储在应用程序的内存中以供进一步使用.

谢谢.

小智 6

你可以存储userIdAppDelegate.


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

@property (nonatomic, strong) NSString *userId
Run Code Online (Sandbox Code Playgroud)

然后AppDelegate.m使用以下代码合成它:

@synthesize userId;
Run Code Online (Sandbox Code Playgroud)

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

#import "AppDelegate.h"
Run Code Online (Sandbox Code Playgroud)

要检索userId使用以下代码:

AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
NSString *userId = [appDelegate userId];
Run Code Online (Sandbox Code Playgroud)

并设置userId使用以下代码:

AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
[appDelegate setUserId:YouValueRetrievedByInternetOrWhateverYouWant];
Run Code Online (Sandbox Code Playgroud)