Question

I am building an iPhone App using PhoneGap. I am using ChildBrowser plugin.

If I have a form in the App with username/pass, is there anyway I can post those information to an URL like www.mydomain.com/login.php and open it in ChildBrowser?

Please let me know.

Thanks.

Was it helpful?

Solution

You can certainly pass GET parameters to childBrowser.showWebPage().

You could intercept the submit of your form in your app and gather up the field names and values passed (assuming this is a login form, so say username and password) and pass them as parameters to the URL opened in ChildBrowser.

childBrowser.showWebPage('https://www.mydomain.com/login.php?username='+username+'&password='+password)

This is a rather unsophisticated version for demonstration purposes. It wouldn't take much to make it better.

The catch is of course that it is being sent via GET. If your login form is expecting POST and you can't change that, you could have a problem.

Edit:

If POST is your only option, perhaps you would be better off using AJAX to post to the form instead of using child browser.

OTHER TIPS

I was looking for a solution, and ended developing it. This doesn't come out of the box in childbrowser for iPhone. So, here the code that I added to the plugin. FYI, data should be a string with the format: arg1=value1&arg2=value2&foo=bar

ChildbrowserViewController I've added the following method there:

- (void)postURL:(NSString*)url data:(NSString*)data{
NSMutableURLRequest *request = [[NSMutableURLRequest alloc]initWithURL: [NSURL URLWithString:url]];
[request setHTTPMethod: @"POST"];
[request setHTTPBody: [data dataUsingEncoding: NSUTF8StringEncoding]];
[webView loadRequest: request];
webView.hidden = NO;}

ChildbrowserCommand I've added the following code in the showWebPage method:

NSString* method = [ options objectForKey:@"method"];
if ([method isEqualToString:@"POST"]){
    [childBrowser postURL:url data:[options objectForKey:@"data"]];
}else{
    [childBrowser loadURL:url];
}

HTH! Milton.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top