Question

I'm trying to make an app that saves pages from a particular website (https://www.airservicesaustralia.com/naips/Account/LogOn) and I'm trying to get the app to login for the user given details they save in the app. I want to try and get it to post the login data in the background. I've been trying to use a NSMutableURLRequest but having no luck... Any suggestions on how to login to this website in the background?

Thanks!

Was it helpful?

Solution

You should use a browser with some development mode (like Chrome or Safari with developer mode enabled) and read the variables that are part of the POST or GET request that happens when you login (in this case, the request that happens when you press Submit).

Use the same variables in your own request.

OTHER TIPS

Put this in Login button action

NSString *post = [[NSString alloc] initWithFormat:@"uname=%@&pwd=%@",usernameData,passwordData];

NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];

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

NSURL *url = [NSURL URLWithString:@"http://www.yourlink.com/chckLogin.php"];
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];
[theRequest setHTTPMethod:@"POST"];
[theRequest setValue:postLength forHTTPHeaderField:@"Content-Length"];
[theRequest setHTTPBody:postData];


NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];

if( theConnection )
{
    indicator.hidden = NO;
    [indicator startAnimating];
    webData = [[NSMutableData data] retain];
}
else
{
    NSLog(@"Internet problem maybe...");
}

And then have connection

-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    [webData setLength: 0];
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    [webData appendData:data];
}
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
    // show error
    indicator.hidden = YES;
    [indicator stopAnimating];
    [connection release];
    [webData release];
}

-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    NSString *loginStatus = [[NSString alloc] initWithBytes: [webData mutableBytes] length:[webData length] encoding:NSUTF8StringEncoding];
    greetingLabel.text = @"";
    NSLog(@"after compareing data is %@", loginStatus);
    if ([loginStatus isEqualToString:@"right"]) {

        // right login
    } else {
        // wrong login
        greetingLabel.hidden = NO;
        greetingLabel.text = @"Incorrect username and/ or password.";
    }

    [loginStatus release];
    [connection release];
    [webData release];
    indicator.hidden = YES;

}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top