我完全不熟悉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