我完全不熟悉iOS编程的整个Web方面,所以请耐心等待:P。我的目标是从网站获取JavaScript文件,然后“注入”该脚本,以便我可以使用它登录另一个网站。我不确定我的做法是否是“最好的”,所以如果你注意到他们有什么不好的情况,请告诉我! :D注入JavaScript以便在网站上使用?
因此,在我的应用程序中,我们从一个视图开始,用户有两个文本框和一个提交按钮。一个用于用户名的文本框,另一个用于密码。当用户点击提交时,我想要加载页面,将输入的用户名和密码放入HTML页面的正确位置!
- (IBAction)signInTouched:(id)sender {
if (userName.text.length > 0 && pass.text.length > 0)
{
NSString *theSite = @"www.thesite.com";
NSURL *theURL = [NSURL URLWithString:theSite];
NSURLRequest *loginRequest = [[NSURLRequest alloc] initWithURL:theURL];
[webView loadRequest:loginRequest]; // Webview is the web view :P
NSURL *jsSite = [NSURL URLWithString:@"www.ThePlaceWithTheJS"];
NSString *jsString = [NSString stringWithContentsOfURL:jsSite encoding:NSUTF8StringEncoding error:nil];
[webView stringByEvaluatingJavaScriptFromString:jsString];
NSString *userValue = userName.text;
NSString *passValue = pass.text;
NSString *javaFunction = [NSString stringWithFormat:@"login(%@,%@)", userValue, passValue]; // login is the function in the javascript
[webView stringByEvaluatingJavaScriptFromString:javaFunction];
}
}
然后,这里是JS文件:
<script type='text/javascript'>
function login(username,password){
var usernameBox = document.getElementById("ctl00_plnMain_txtLogin");
usernameBox.value = usernameBox.value + username;
var passwordBox = document.getElementById("ctl00_plnMain_txtPassword");
passwordBox.value = passwordBox.value + password;
}
</script>
那么,事情是我不知道如果我做这一切的权利,或如何得到这个正常工作。每当我测试它时,文本都不会被加载到网站上的字段中。所以它显然在某个地方出了问题......但是在哪里?
你的目标听起来很阴暗...... – lifetimes 2013-03-23 00:16:59
你为什么这么说:P – 2013-03-23 00:20:14
只是一个冷静的头脑,你不必乞求人帮你在这里,整个目的大家在这里浏览是为了解决你的问题,所以人们不可避免地会试图提供帮助。我对ObjC一无所知,所以我不能真正帮助你,但我肯定有人会为你解决这个问题。 – 2013-03-23 00:20:25