如何在Xcode的Objective C中通过URL请求创建登录页面?

时间:2022-12-20 20:00:32

I am making an attempt to create a login after doing some research, but my following code doesn't seem to legitimately have a user signed and keep track of his information once he logs in and keep the user logged in until logged out.

我正在尝试在做一些研究之后创建一个登录,但我的下面的代码似乎没有合法地让用户签名并在他登录后跟踪他的信息并保持用户登录直到注销。

If anyone can verify if it's properly implemented as a login page should and provide some guidance as to how to properly create a login page with my code, I would greatly appreciate it.

如果任何人都可以验证它是否正确实现为登录页面,并提供一些指导如何使用我的代码正确创建登录页面,我将不胜感激。

Once a user enters in a username (emailTF) and a password (passwordTF) in the textfields and click the sign in button, it triggers the following action:

用户在文本字段中输入用户名(emailTF)和密码(passwordTF)并单击登录按钮后,将触发以下操作:

- (IBAction)signIn:(id)sender {

[self sendSignIn:nil success:^(NSArray  * response) {
    NSLog(@"-----------------------------SIGN UP SUCCESS--------------------------");

} failure:^(NSError * error) {
           NSLog(@"************************************Error*********************************");
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Please confirm your username/or password" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles: nil];
    [alert show];
}];
}

Once it is successful, it calls the method 'sendSignIn':

一旦成功,它会调用方法'sendSignIn':

-(void)sendSignIn:(NSDictionary *)params success:(Success)success failure:(Error)failure

//If email or password textfield is empty, throw up an error sign
{if([_emailTF.text isEqualToString:@""]|| [_passwordTF.text isEqualToString:@""])
{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Please input your email or password first" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles: nil];
    [alert show];
}
else
{
    NSString *email = self.emailTF.text;
    NSString *password = self.passwordTF.text;
    NSString *deviceType = @"ios";
    NSString *channelID = [UAirship push].channelID;

    NSDictionary *dict = @{@"email": email, @"password": password, @"deviceType": deviceType, @"channelID": channelID};

//Request to url for example: #define SERVER_URL                          @"http://exampleurl.com"
#define COURSES                             @"/login"

    NSString * url = [SERVER_URL stringByAppendingString:COURSES];
    NSLog(@"REQUEST : %@",url);

    [self.manager POST:url parameters:dict progress:^(NSProgress * _Nonnull downloadProgress) {

        //Upon success, login the user and segue to home page via 'login' segue
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        [self performSegueWithIdentifier:@"login" sender:self];
        success(responseObject);

//Upon failure, throw up a failure sign
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            failure(error);

        }];
    }
}

EDIT (How new user is stored - Sign up page):

编辑(如何存储新用户 - 注册页面):

-(AFHTTPSessionManager *)manager{

    if(!_manager){

        _manager = [AFHTTPSessionManager manager];
    }
    return _manager;
}

- (void)sendSignUp:(NSDictionary *)params success:(Success)success failure:(Error)failure
{
    NSString *firstName = self.firstName.text;
    NSString *lastName = self.lastName.text;
    NSString *email = self.emailTF.text;
    NSString *password = self.passwordTF.text;
    BOOL isProfessor = NO;

    NSString * url = [SERVER_URL stringByAppendingString:COURSES];
    NSLog(@"REQUEST : %@",url);

    NSDictionary *dict = @{@"firstName":firstName,@"lastName":lastName, @"email": email, @"password": password, @"isProfessor": [NSNumber numberWithBool:NO]};
    //NSDictionary *dict = @{@"student": @1,@"course":@1, @"section": @1, @"quiz": @1, @"question": @1, @"answer": @1};

    [self.manager POST:url parameters:dict progress:^(NSProgress * _Nonnull downloadProgress) {


    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {

        success(responseObject);

    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {

        failure(error);

    }];

}

1 个解决方案

#1


0  

Login General scenario :

登录一般情况:

You should send required parameters (username,password) in json format to server at given url.

您应该以json格式将所需参数(用户名,密码)发送到给定URL的服务器。

Server side they check that is it valid credential? if yes then it returns ye or true and if no then it returns no or false which you got in your responseObject.

服务器端他们检查是否是有效的凭证?如果是,那么它返回ye或true,如果没有则返回no或false,这是你在responseObject中得到的。

According to this response object you came to know that is it valid or not. if you return true then allow user for forward stuff. If not false then show alert message and don't allow to go forward.

根据这个响应对象,你知道它是否有效。如果你返回true,那么允许用户转发。如果不是假,则显示警告消息,不允许继续前进。

If you want to local storage for validation then store username and password in NSUserdefaults. It will remain permanent in your app until you update it or delete it. And you can check with local storage when user enter username and password. and remain sync with server when got internet connectivity.

如果您想要本地存储进行验证,请在NSUserdefaults中存储用户名和密码。在您更新或删除它之前,它将永久保留在您的应用中。当用户输入用户名和密码时,您可以使用本地存储进行检查。在获得互联网连接时保持与服务器同步。

Hope this will help :)

希望这会有所帮助:)

Update :

According to your comment you can use NSUserdefault like,

根据你的评论你可以使用NSUserdefault,如,

If you want to store string then,

如果你想存储字符串,

NSString *valueToSave = @"someValue";
[[NSUserDefaults standardUserDefaults] setObject:valueToSave forKey:@"preferenceName"];
[[NSUserDefaults standardUserDefaults] synchronize];

to get it back later

以后再拿回来

NSString *savedValue = [[NSUserDefaults standardUserDefaults]
stringForKey:@"preferenceName"];

like this way you can store NSArray, NSDictionary etc. :)

像这样你可以存储NSArray,NSDictionary等:)

#1


0  

Login General scenario :

登录一般情况:

You should send required parameters (username,password) in json format to server at given url.

您应该以json格式将所需参数(用户名,密码)发送到给定URL的服务器。

Server side they check that is it valid credential? if yes then it returns ye or true and if no then it returns no or false which you got in your responseObject.

服务器端他们检查是否是有效的凭证?如果是,那么它返回ye或true,如果没有则返回no或false,这是你在responseObject中得到的。

According to this response object you came to know that is it valid or not. if you return true then allow user for forward stuff. If not false then show alert message and don't allow to go forward.

根据这个响应对象,你知道它是否有效。如果你返回true,那么允许用户转发。如果不是假,则显示警告消息,不允许继续前进。

If you want to local storage for validation then store username and password in NSUserdefaults. It will remain permanent in your app until you update it or delete it. And you can check with local storage when user enter username and password. and remain sync with server when got internet connectivity.

如果您想要本地存储进行验证,请在NSUserdefaults中存储用户名和密码。在您更新或删除它之前,它将永久保留在您的应用中。当用户输入用户名和密码时,您可以使用本地存储进行检查。在获得互联网连接时保持与服务器同步。

Hope this will help :)

希望这会有所帮助:)

Update :

According to your comment you can use NSUserdefault like,

根据你的评论你可以使用NSUserdefault,如,

If you want to store string then,

如果你想存储字符串,

NSString *valueToSave = @"someValue";
[[NSUserDefaults standardUserDefaults] setObject:valueToSave forKey:@"preferenceName"];
[[NSUserDefaults standardUserDefaults] synchronize];

to get it back later

以后再拿回来

NSString *savedValue = [[NSUserDefaults standardUserDefaults]
stringForKey:@"preferenceName"];

like this way you can store NSArray, NSDictionary etc. :)

像这样你可以存储NSArray,NSDictionary等:)