Search code examples
iosobjective-cios5ios6afnetworking

Logging into Instapaper with AFNetworking


If I wanted to login to Instapaper (http://www.instapaper.com/user/login) within an app where the user has put the username and password within a UITextField and login using AFNetworking, how would I accomplish this?

Is it to do with this method? http://afnetworking.github.com/AFNetworking/Classes/AFHTTPClient.html#//api/name/postPath:parameters:success:failure:

Basically I want to take the credentials, log in to Instapaper and then interact with the website that loads.

If it is the above method, do I use an NSDictionary for parameters where it's 'username'->'', and 'password'->'' or something? And is the response the HTML of the page that presents after login (what I'm looking for)?

I'm new to networking, so I'm a little confused how I'd use AFNetworking to accomplish this.


Solution

  • Use AFHTTPRequestOperation variant of AFHTTPClient, If you are sending username and password as parameters then add them into a NSDictionary as follows and send them as params, otherwise you can send them as formData if you want to send them in the body.

    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
     [parameters setObject:username.text forKey:@"username"];
     [parameters setObject:password.text forKey:@"password"];   
    
    AFHTTPClient  *afHttpClient = [AFHTTPClient clientWithBaseURL:[NSURL URLWithString:@"http://yourbaseURL"]];
    
    NSMutableURLRequest *request = [afHttpClient multipartFormRequestWithMethod:@"POST" path:@"/pathifAny" parameters:parameters constructingBodyWithBlock:^(id <AFMultipartFormData>formData)
                                            {
                                            // here you can send as body if you not setting as parameters
                                            [formData appendPartWithFormData:[username.text dataUsingEncoding:NSUTF8StringEncoding] name:@"username"];
                                            [formData appendPartWithFormData:[password.text dataUsingEncoding:NSUTF8StringEncoding] name:@"password"];
    
                                            }];
    
    
    
    AFHTTPRequestOperation  *afHttpReqOperation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
    

    Here is simple tutorial about GET/POST