问题
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.
回答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];
}
回答2:
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
来源:https://stackoverflow.com/questions/7729473/can-i-use-username-and-password-information-from-uitextfields-to-log-onto-a-webs