Search code examples
iosuiwebviewasihttprequestautologinuiwebviewdelegate

Autologin using ASIHTTPRequest in a web based form login inside a UIWebView


I'm trying to accomplish an autologin using ASIHTTPRequest library in a web based form login inside a UIWebView. I want to make it persistant, so the following requests made by the user in the UIWebView use the credentials supplied from the networking library.

I tried this:

- (void)viewDidLoad {

[super viewDidLoad];
NSString *fullURL = @"http://myweb/loginform";

NSURL *url = [NSURL URLWithString:fullURL];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request setUseKeychainPersistence:YES];
[request setUseSessionPersistence:YES];
[request setUseCookiePersistence:YES];
[request setPostValue:@"myuser" forKey:@"__ac_name"];
[request setPostValue:@"mypass" forKey:@"__ac_password"];
[request setDelegate:self];
[request startAsynchronous];

}

- (void)requestFinished:(ASIHTTPRequest *)request {
NSError *error = [request error];
if (!error) {
    [webView loadData:[request responseData] MIMEType:@"text/html" textEncodingName:@"UTF-8" baseURL:[request url]]; }
}

But when I try to navigate via UIWebView, the credentials are not persisted inside it and got unauthenticated requests.

There is a way to share the credentials between ASIHTTPRequest world and the UIWebView one?

Thanks in advance.


Solution

  • OK, The code is perfectly correct, I was missing one POST value while forming the request. Every web app might require its own POST parameters, not only the username and password ones. My missing parameter was:

    [request setPostValue:@"1" forKey:@"form.submitted"];
    

    Now it works like a charm.