iOS
Comments

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];
}
iOS
Comments

If you use UIWebView and would like to automatically fill in the username and password (or any other text input fields), you could do so by executing Javascript on the webview.

I first learnt about this from stackoverflow, and has edited slightly.

The technique is to implement UIWebViewDelegate and execute some Javascript in webViewDidFinishLoad.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
- (void)webViewDidFinishLoad:(UIWebView *)webView {
    // Auto fill the username and password text fields, assuming the HTML has
    // <input type="text" name="username"> and
    // <input type="text" name="password">
    NSString *savedUsername = [[NSUserDefaults standardUserDefaults] objectForKey:@"USERNAME"];
    NSString *savedPassword = [[NSUserDefaults standardUserDefaults] objectForKey:@"PASSWORD"];
    if (savedUsername.length != 0 && savedPassword.length != 0) {
        // Create js strings
        NSString *loadUsernameJS = [NSString stringWithFormat:@"var inputFields = document.querySelectorAll(\"input[name='username']\"); \
                                    for (var i = inputFields.length >>> 0; i--;) { inputFields[i].value = '%@';}", savedUsername];
        NSString *loadPasswordJS = [NSString stringWithFormat:@"var inputFields = document.querySelectorAll(\"input[name='password']\"); \
                                    for (var i = inputFields.length >>> 0; i--;) { inputFields[i].value = '%@';}", savedPassword];
        // Runs the JS
        [self.webview stringByEvaluatingJavaScriptFromString: loadUsernameJS];
        [self.webview stringByEvaluatingJavaScriptFromString: loadPasswordJS];
    }
}
iOS
Comments

There is a very easy (but not documented) way to set the User Agent header for HTTP requests sent via UIWebView.

I find saw the solution from mphweb.

Basically, you just set register it with NSUserDefaults.

1
2
3
4
5
6
+ (void)initialize {
    // Set user agent (the only problem is that we can't modify the User-Agent later in the program)
    NSDictionary *dictionnary = [[NSDictionary alloc] initWithObjectsAndKeys:@"Your desired user agent", @"UserAgent", nil];
    [[NSUserDefaults standardUserDefaults] registerDefaults:dictionnary];
    [dictionnary release];
}
iOS
Comments

In a previous post, I wrote about a simple usage of AFNetworking, the de facto HTTP library for iOS.

This post, I will show how you can use AFNetworking to queue multiple HTTP operations. They could be running concurrently, or have dependencies.

Adding dependencies to HTTP operations is especially useful. For example, you can make sure that you fetch a list of resources, then fetch image1, then image2, …

Continue reading →
Mac
Comments

When my new iMac was delivered, the only unsatisfactory I had was with the faulty trackpad. It can click, but can’t move the cursor at all! I sent it for servicing and got a replacement after a few days.

Anyway, this post isn’t about a new iMac with a faulty trackpad.

What happened is that when I use the new and working trackpad, I realized the 4 finger swipe up/down gestures were not working (therefore I can’t do Expose and Mission Control).

Continue reading →
Mac
Comments

I did a migration using the Migration Assistant app when I bought a new iMac (late 2012 27” model).

I decided to migrate everything (instead of a fresh install) as I DON’T want to:

  • reinstall hundreds of apps
  • setup the system preferences
  • manually transfer files

But migration is not perfect (though I think it is much efficient than doing a fresh install).

Here are some tips that you will be interested to know.

Continue reading →