私はiOSプログラミングのこのWeb全体の側面に完全に慣れていないので、我慢してください:P. 私の目標は、Web サイトから JavaScript ファイルを取得し、そのスクリプトを「注入」して、それを使用して別のサイトにログインできるようにすることです。私の練習が「最高」かどうかはわからないので、何か悪いことに気づいたら教えてください!:D
したがって、私のアプリでは、ユーザーが 2 つのテキスト ボックスと送信ボタンを持つビューから始めます。ユーザー名用の 1 つのテキスト ボックスと、パスワード用の 1 つのテキスト ボックス。ユーザーが送信ボタンを押すと、ページを読み込んで、入力したユーザー名とパスワードを 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>
ですから、問題は、これを正しく行っているかどうか、またはこれを適切に機能させる方法がわからないということです。テストするたびに、テキストが Web サイトのフィールドに読み込まれません。つまり、明らかにどこかでうまくいかないのです...しかし、どこで?