2

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]]];

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the stringValue part, I always forget that. But it doesn't give me any warnings anymore and still it isn't working...
@icant: Then you need to edit your question both to show your revised code and to be more specific about how it “isn't working”.
0

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.)

4 Comments

It says 'No -evaluateWebScript: method found'. This is my code: [[mainWebView windowScriptObject] evaluateWebScript:[NSString stringWithFormat:@"document.getElementById('username').value='%@';document.getElementById('password').value='%@';", [ebuddyidField value], [passwordField value]]];
Did you #import <WebKit/WebKit.h>?
Yes, I did import it in the header file.
In any case, @Peter has the right answer-- send -stringByEvaluatingJavaScriptFromString: to the WebView rather than the WebFrame. That should cover it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.