gpt4 book ai didi

ios - Objective C 将用户凭据保存到单例中,并且

转载 作者:行者123 更新时间:2023-11-28 22:08:32 25 4
gpt4 key购买 nike

我的想法是将用户凭据保存到单例中,并使用此单例在服务器上进行身份验证。

但是有些我输入的用户名和密码总是空的,我不知道为什么......

我在做什么

在我的例子中单例调用 session

@property (nonatomic,strong) NSString*UserName;
@property (nonatomic,strong) NSString*Password;

+(Session *)getSession;

+(Session *)getSession
{
static Session* theSingelton = nil;
@synchronized([ Session class])
{
if (theSingelton == nil) {
theSingelton = [[Session alloc]init];
}
return theSingelton;
}
}

没有什么特别的或多或少的标准。

这是我在 viewdidload 中调用的登录名

-(void)showLoginPormt{
UIAlertView *login = [[UIAlertView alloc]initWithTitle:@"Log-in" message:@"Please enter your credentials" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Log in", nil];

login.alertViewStyle = UIAlertViewStyleLoginAndPasswordInput;
UITextField *tf = [login textFieldAtIndex:0];
tf.keyboardType = UIKeyboardTypeEmailAddress;

[login show];

这就是我将“用户名”和“密码”保存到我的单例中的方式

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
if (buttonIndex == alertView.firstOtherButtonIndex) {
NSString *userName = [alertView textFieldAtIndex:0].text;
NSString *passWord = [alertView textFieldAtIndex:1].text;
Session *LoginSession = [[Session alloc]init];
[LoginSession setUserName:userName];
[LoginSession setPassword:passWord];
}

所以现在没有什么不应该工作的。现在我想用我的获取请求发送我的凭据

   Session *LoginSession = [[Session alloc]init];   
NSString *authStr = [NSString stringWithFormat:@"%@:%@",[LoginSession UserName], [LoginSession Password]];
NSData *authData = [authStr dataUsingEncoding:NSASCIIStringEncoding];

但是我的用户名和密码总是空的..我真的不知道为什么......

感谢您的帮助和快速回答!

最佳答案

你有一个单例:

+(Session *)getSession

但你不使用它:

Session *LoginSession = [[Session alloc]init];

应该是:

Session *LoginSession = [Session getSession];

关于ios - Objective C 将用户凭据保存到单例中,并且,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23424649/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com