In the last post, I wrote about how you can automatically fill in username and password in a UIWebView.

This post, I will teach you how you can automatically login to a UIWebView.

It turns out to be simple if the login uses a simple form POST.

- (void)login {
    // Setup the URL
    NSString *loginUrl = @"";
    NSURL *url = [NSURL URLWithString:loginUrl];    
    NSMutableURLRequest *requestObj = [NSMutableURLRequest requestWithURL:url];
    // POST the username password
    [requestObj setHTTPMethod:@"POST"];
    NSString *postString = [NSString stringWithFormat:@"username=%@&password=%@", @"samwize", @"secret"];
    NSData *data = [postString dataUsingEncoding: NSUTF8StringEncoding]; 
    [requestObj setHTTPBody:data];
    // Load the request
    self.webview.delegate = self;
    [self.webview loadRequest:requestObj];




Back to Home