Can I use username and password information from UITextFields to log onto a website in a UIWebView automatically?

寵の児 提交于 2019-12-06 21:36:43

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

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!