1

I'm have created a Login view. Everytime I login it gives me login failed error even if I enter right credentials. This is the method I'm using right now:

NSString *postString = [[NSString alloc] initWithFormat:@"username=%@&password=%@",userName, password];
    // Package the string in an NSData object
    NSData *requestData = [postString dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];

    NSString *postLength = [NSString stringWithFormat:@"%d", [requestData length]];  

    // Create the URL request
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL: [NSURL URLWithString:@"https://172.168.1.9/dologin.php"]];  // create the URL request
    [request setHTTPMethod: @"POST"];   // you're sending POST data
    [request setHTTPBody: requestData];  // apply the post data to be sent
    [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
    [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];

    // Call the URL
    NSURLResponse *response;  // holds the response from the server
    NSError *error;   // holds any errors
    NSData *returnData = [NSURLConnection sendSynchronousRequest: request returningResponse:&response error:&error];
vikingosegundo
  • 52,040
  • 14
  • 137
  • 178
Aniruddh
  • 7,648
  • 1
  • 26
  • 43

1 Answers1

2

Can you verify what the response is? Also, you should probably be encoding the parameters you post: Objective-c iPhone percent encode a string?

ASIHttpRequest example

- (void)btnLoginTap {
    [txtUsername resignFirstResponder];
    [txtPassword resignFirstResponder];

    NSURL *url = [[NSURL alloc] initWithString:YOUR_URL_HERE];
    ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
    [request setTimeOutSeconds:120];
    [request setPostValue:txtUsername.text forKey:@"username"];
    [request setPostValue:txtPassword.text forKey:@"password"];
    [request setDelegate:self];
    [request startAsynchronous];
    [url release];

    //Show a loading view or something here while you wait for the response...
}

- (void) requestFinished:(ASIHTTPRequest *)request {
    //Hide your loading view here.
    NSString *responseString = [request responseString];
    DLog(@"%@", responseString);
    //Request succeeded
}

- (void) requestFailed:(ASIHTTPRequest *)request {
    //Hide your loading view here.
    NSError *error = [request error];
    DLog(@"%@", [error localizedDescription]);
    //Request failed
}
Community
  • 1
  • 1
jocull
  • 20,008
  • 22
  • 105
  • 149
  • The response comes in HTML format. When I saw the HTML codes, it was showing the codes of login failed page. – Aniruddh Dec 14 '10 at 16:03
  • If that is the case, I would guess you are having a server side problem. You should verify on the server side that it is receiving the username and password you sent as expected. Also, I should mention that there is a really great class library already made for making HTTP requests. It's been a lifesaver for me: http://allseeing-i.com/ASIHTTPRequest/ – jocull Dec 14 '10 at 16:05
  • I don't know how to use ASIHTTPRequest. :( – Aniruddh Dec 14 '10 at 16:19
  • Can you show me a sample code to send Login info using ASIHTTPRequest? I tried it before but couldn't get it work for my purpose. :( – Aniruddh Dec 14 '10 at 16:27
  • Okay, now I'm getting this error: objc-class-ref-to-ASIAuthenticationDialog in ASIHTTPRequest.o Symbol(s) not found – Aniruddh Dec 14 '10 at 17:17
  • 1
    I've updated my answer. Follow the setup instructions for ASIHttpRequest here: http://allseeing-i.com/ASIHTTPRequest/Setup-instructions it's super helpful – jocull Dec 14 '10 at 17:19
  • Hello, since the response is coming in HTML format, how would ASIHTTPRequest will know that the request is failed or it is successful. Also, how can I show the response data in UIAlertView. I'm sorry for being too annoying. :( – Aniruddh Dec 14 '10 at 17:35
  • Oh, I got the answer of first question but still confused with second one: "How can I show the response data in UIAlertView" – Aniruddh Dec 14 '10 at 17:37
  • If it failed, that means the request timed out, it couldn't reach the server, the network is offline, or some other issue. If the server responds with HTML you'll have to check it yourself somehow. You may want to write a separate login handler on the server for your iPhone login that returns a simpler response. – jocull Dec 14 '10 at 17:38
  • I mean I know how to show the UIAlertView but don't know how to get the response data using ASIHttpRequest. I'm currently using this code: NSData *responseData = [request responseData]; but the app is exiting with error "SIGABRT". – Aniruddh Dec 14 '10 at 17:49
  • Try using the responseString like in my example. – jocull Dec 14 '10 at 18:10
  • Great, ASIHTTPRequest and you are the life saver for me now. :D – Aniruddh Dec 14 '10 at 18:16
  • 1
    If you don't want to include separate libraries in your project, just use your original code and change username to [username stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding], and similar for password, when you add them to your data in the first line. – ughoavgfhw Dec 14 '10 at 21:57
  • I have use ASIHttpRequest to create an iPhone safari like app, now I stumble to do login to a wordpress site, and I just realise that I can do it with ASI again. Thanks @jocull! – swdev Sep 04 '11 at 07:24