iOS

POST Request to UIWebView

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
- (void)login {
    // Setup the URL
    NSString *loginUrl = @"https://just2us.com/login";
    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];
}

Comments