stringByEvaluatingJavaScriptFromString not working
This expression doesn't seem to work. It's with a Mac application, not an iPhone app.
[[mainWebView mainFrame] stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"document.getElementById('username').value='%@';document.getElementById('password').value='%@';", [ebuddyidField value开发者_运维问答], [passwordField value]]];
You need to send your stringByEvaluatingJavaScriptFromString:
message to the WebView, not to its main frame.
Also, assuming that ebuddyidField
and passwordField
are NSTextFields (the latter possibly an NSSecureTextField), they do not respond to a value
message. The closest message they do respond to is objectValue
, though you more probably want stringValue
.
The compiler should have given you warnings for all three problems. Go to your Build Results window and fix every warning. Then turn some more on and fix those, too. Your application will be much more stable, more robust, and less leaky for it.
That's because WebFrame
doesn't respond to -stringByEvaluatingJavaScriptFromString:
. That's a method of UIWebView
on iOS, and has nothing to do with WebKit on Mac OS X.
Try [[webView windowScriptObject] evaluateWebScript:...]
instead. The result of that method, however, is an id
, and its type may or may not be NSString
.
See the WebScriptObject
class documentation for more info on JavaScript interaction in WebKit on Mac OS X.
Edit: Actually, going back and looking at the WebView
docs, I see it does indeed have a -stringByEvaluatingJavaScriptFromString:
method. So try sending that to the web view rather than the web frame.)
精彩评论