开发者

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

开发者 https://www.devze.com 2023-04-12 12:33 出处:网络
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

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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号