Reputation: 55
I tried to make an login page to make user authenticate to the website. I tried something like:
NSURL *myURL = [NSURL URLWithString:@"https://www.freelancer.com/users/onlogin.php"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:myURL
cachePolicy:NSURLRequestReloadIgnoringLocalCacheData
timeoutInterval:60];
NSLog(@"ispost");
[request setHTTPMethod:@"POST"];
[request setValue:usernameField.text forKey:@"username"];
[request setValue:passwordField.text forKey:@"passwd"];
NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
if (!theConnection) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Network Error" message:@"Could not login!" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
[alert release];
return;
}
I got an erroras followed:
2012-07-07 10:09:37.354 Auth[6062:f803] ispost
2012-07-07 10:09:37.357 Auth[6062:f803] *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<NSMutableURLRequest 0x68d69d0> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key username.'
Can anyone suggest me what i did wrong?
Thanks for any help
Upvotes: 2
Views: 4158
Reputation: 4527
Why don't you use like
[request setValue:usernameField.text forHTTPHeaderField:@"username"];
[request setValue:passwordField.text forHTTPHeaderField:@"passwd"];
Hope this helps you.
Upvotes: 4
Reputation: 371
NSMutableURLRequest doesn't have a username property (hence the error). You need to do something like the following (though it depends on what format your server is expecting):
[request setHTTPMethod:@"POST"];
NSString* str = [NSString stringWithFormat:@"username=%@&passwd=%@", usernameField.text, passwordField.text];
[request setHTTPBody:[str dataUsingEncoding:NSUTF8StringEncoding]];
Hope that helps.
Upvotes: 1
Reputation: 18290
The hint is in your error string:
this class is not key value coding-compliant for the key username.
I'm pretty sure you can't do basic auth in the way you are attempting. A quick search turned up this, and I do recall having to do base64 when trying to pass it with every request (the alternative is implementing the connection delegate methods, and properly handling the request for auth.
Upvotes: 0