User Login With AFNetworking

2020-04-17 04:15发布

问题:

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 I don't know how to send that data to my server using Objective C.

I have read that AFNetworking is good, but I was wondering how I could use that for user login .

I have downloaded and added AFNetworking to my XCode Project and set up headers.

AFHTTPClient *client = [AFHTTPClient clientWithBaseURL:[NSURL URLWithString:@"http://examplewebsite.com]];

[client setDefaultHeader:@"key" value:@"value"];
[client setAuthorizationHeaderWithUsername:@"username" password:@"password"];
[client setAuthorizationHeaderWithToken:@"token"];

NSURLRequest *request = [client requestWithMethod:@"someMethod" path:@"somePath" parameters:nil];

AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];

but I am still lost.

回答1:

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.



回答2:

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



回答3:

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"];