1

I am currently trying to find a solution to this task. Essentially I have an initial UIView that asks for the users username and password for a specific site, this information would then get past onto the next view which has a UIWebView inside. I would like to know if its possible to automatically fill in the username and password details on the site and send the user directly to their account page on the site.

Thanks.

user989941
  • 15
  • 3

2 Answers2

1

on solution is to "fake" the login request using the username and password and feed this into the web view. I.e. you have a page index.php which has a username and password field.

if you fill them in, the page login.php is called with those parameters.

you could build an

NSString *urlString = @”http://www.yoursite.com/login.php?username=user&password=pass”;
NSURL *url = [NSURL URLWithString:urlString];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webView loadRequest:request];

}

that should do the trick

HeikoG
  • 1,793
  • 1
  • 20
  • 29
  • hi, thanks for the reply. When I started the app I was hoping I could use this solution, however it seems the website does not use this login design. I have a feeling I would have to use javascript to inject the two pieces of information onto the site when it is loaded into the UIWebView. I know of the stringByEvaluatingJavaScriptFromString method but quite where and how to implement it. – user989941 Oct 11 '11 at 18:49
1

assuming your website has div tags you can inject using stringByEvaluatingJavsScriptFromString.

This example injects a username into the twitter sign up page. You can test it by loading this in your viewDidLoad:

NSString *urlString = @"https://mobile.twitter.com/signup";
NSURL *url = [NSURL URLWithString:urlString];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webView loadRequest:request];

and then in webViewDidFinishLoad inject the username:

- (void)webViewDidFinishLoad:(UIWebView *)webView;
{
[webView stringByEvaluatingJavaScriptFromString:@"var field = document.getElementById('oauth_signup_client_fullname');"  
 "field.value='yourUserName';"];  
}

sorry, just realised you already had the solution but were missing the implementation. Hope the above helps.

btw to add a dynamic variable into the injected javascript use:

- (void)webViewDidFinishLoad:(UIWebView *)webView;
{

    NSString *injectedVariable = @"userNameHere";

    NSString *injectedString = [NSString stringWithFormat:@"var field = document.getElementById('oauth_signup_client_fullname'); field.value='%@';", injectedVariable];

[webView stringByEvaluatingJavaScriptFromString:injectedString];  
}
Nik Burns
  • 3,363
  • 2
  • 29
  • 37
  • Thanks! I did finally manage to implement the javaScript but was having trouble adding a dynamic variable. Came on here to find the answer. – user989941 Oct 11 '11 at 20:40