2

I looked at a lot of the documentation from apple but I still can't seem to grasp the general idea of how to implement my javascript function into the webview.

I have an url:https://mobilemy.umd.edu/portal/server.pt/gateway/PTARGS_0_340574_368_211_0_43/https%3B/www.sis.umd.edu/testudo/studentSched?term=201401

I want to use javascript to fill in the form (username and password) and submit the forms.

In the console, I've been using the script:

javascript:(function() {
    document.lform.in_tx_username.value='username';
    document.lform.in_pw_userpass.value='password';
    doLogin();
    })()

Obj-c code look something like this:

loginScript = @"function() {document.lform.in_tx_username.value='username';
    document.lform.in_pw_userpass.value='password'; 
    doLogin();}";
[_visibleWebView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:scheduleURL]]];
[_visibleWebView stringByEvaluatingJavaScriptFromString:loginScript];

where scheduleUrl is the url i linked above. I am pretty new to both Obj-C and JS so any help/tips would be appreciated.

2 Answers 2

2

When you run stringByEvaluatingJavaScriptFromString you are running the javascript on the page / HTML. You're passing in a function, which when evaluated will do nothing, because the function will not be called anywhere. Just run the javascript itself like so:

loginScript = @"document.lform.in_tx_username.value='username';
    document.lform.in_pw_userpass.value='password'; 
    doLogin();";

This javascript will get executed on the page instead of just being validated

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

2 Comments

do i still call the stringbyevaluating: function?
@CoderNinja yes thats how you pass it into the webview, the issue was you were passing a function which needs to be called rather than executable code
0

You can try to execute the code when the webview's loading is finished:

load the request:

_visibleWebView = self;
[_visibleWebView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:scheduleURL]]];

then the inject the javascript call:

- (void)webViewDidFinishLoad:(UIWebView *)webView {

    loginScript = @"function() {document.lform.in_tx_username.value='username';
    document.lform.in_pw_userpass.value='password'; 
    doLogin();}";

    [_visibleWebView stringByEvaluatingJavaScriptFromString:loginScript];

}

EDIT

as Simon McLoughlin says, you also have to call the javascript method not only "declare" it

Comments

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.