Q:

Logging in from settings username and password preferences

I have an app that uses webView. In the home screen of a website I want to login using username and password entered by the user in the app's settings option. Currently I have this code which I saw in solution to a similar question asked on this forum. But it isn't working (neither it did for the other guy). 
-(IBAction)cachepressed:(id)sender{  

    NSString *baseurl=[[NSUserDefaults standardUserDefaults] stringForKey:@"url_preference"];
    NSString *username= [[NSUserDefaults standardUserDefaults] stringForKey:@"name_preference"];
    NSString *password= [[NSUserDefaults standardUserDefaults] stringForKey:@"pswrd_preference"];    
    NSString* content = [NSString stringWithFormat:@"username=%@&password=%@", username, password];
    NSData *data=[content dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
    NSString *postlength=[NSString stringWithFormat:@"%d",[data length]];
    NSString *loginpage= [baseurl stringByAppendingString:@"/index.php?title=Special:UserLogin"];
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:loginpage]];
    [request setHTTPMethod:@"POST"];
    [request setValue:postlength forHTTPHeaderField:@"Content-Length"];
    [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Length"];
    [request setHTTPBody:data]; 
    NSError *error=nil;
    NSURLResponse *response=nil;
    NSData *result=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
    NSString *HTMLString = [[NSString alloc] initWithData:result encoding:NSUTF8StringEncoding];
    [webView loadHTMLString:HTMLString baseURL:[NSURL URLWithString:loginpage]];    

}

I am not too sure about the two lines of code starting with [request setValue...]. Can somebody suggest a solution to this? It would be a great help. Thanks in anticipation.

A:

This depends a lot on how the authentication is done on the website.
Based on what I can figure from your question, the code you posted sends values for the fields named "username" and "password" to the index.php?title=Special:UserLogin page using POST.
You need to make sure that everything is set accordingly.
1. Are your fields name the same on the website?
Maybe the login form uses different field names, not username and password. Check the source code and see if the input names are indeed "username" and "password"
2. Does your host allow posting data from a different source?
I used to set mine so no POST request would work if they didn't originate from my server, so POSTing data from an iPad would not have functioned.
Is this the case for you too?
3. The login process is really done by posting data to that page?
Maybe on the website the authentication is done using AJAX on a different php page and then you get redirected to index.php?title=Special:UserLogin, I've seen such cases in the past.
Bottom line is that you need to know exactly how the login is done on the website before doing it inside a webview. Correct me if I'm wrong, but based on your post it doesn't seem to be the case here

