User Login With AFNetworking

后端 未结 3 1110
耶瑟儿~
耶瑟儿~ 2021-01-22 02:57

I am building my first iOS app.

I have got the backend code done, but I am struggling with the Objective-C part of it.

I have a signup / login page.

But

相关标签:
3条回答
  • 2021-01-22 03:35

    Since you're trying to login to your own API, you don't want setAuthorization stuff. That's for basic HTTP auth. Instead you want to use getPath:parameters:success:failure or the postPath version, depending on if your backend is expecting HTTP GET or HTTP POST.

    Pass your userid / password in the parameters argument. You should set parameterEncoding to be the correct format. You're probably using HTTP Forms url encoding, or JSON. Whatever your backend expects.

    0 讨论(0)
  • 2021-01-22 03:42

    You don't want to set the authorization headers in this case, since this is for "basic access HTTP authentication", which is a method for a HTTP user agent to provide a user name and password when making a request to a server.

    You want to use your own API and interact with a restful server and therefore, I would recommend, that you subclass AFHTTPClient -> interact with an API, Web Service, or Application. - Take a look at the examples in the AFNetworking zip archive, if you have difficulties in subclassing AFHTTPClient.

    Since you want to create an app with user login, the app needs to send these information to your server, and the server should return if the login was succesful or not. This can be done like so - HTTP POST.

     - (void)login {
    
        // Login information from UITextFields
        id params = @{
            @"username": self.usernameField.text,
            @"password": self.passwordField.text
        };
    
        //Call the AFHTTP subclass client, with post block. postPath = the path of the url,   where the parameters should be posted. 
        [[theAuthAPIClient sharedClient] postPath:@"/login"
                                    parameters:params
                                       success:^(AFHTTPRequestOperation *operation, id responseObject) {
    
                                           //handle succesful response from server. 
    
                                       } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
                                           // handle error - login failed
                                           }
                                       }];
    
    }
    

    You need to pass the parameters in the right format, depending on what format your server expects. This can be done by setting the right encoding in your AFHTTPClient subclass -> ParameterEncoding

    0 讨论(0)
  • 2021-01-22 03:42

    Since I came here while searching for a working solution for AFNetworking 2.0, not knowing that AFHTTPClient was removed from the Framework, I will post the new way to establish this connection here:

    AFHTTPRequestOperationManager *manager = [[AFHTTPRequestOperationManager alloc] initWithBaseURL:[NSURL URLWithString:@"http://examplewebsite.com"]];
    [manager setRequestSerializer:[AFHTTPRequestSerializer serializer]];
    [manager.requestSerializer setAuthorizationHeaderFieldWithUsername:@"userName" password:@"password"];
    
    0 讨论(0)
提交回复
热议问题