0

I have the following in a controller and don't understand why it is not calling the function in the page (it doesn't do anytihng). Any help is appreciated - first time using this:

- (void)viewDidLoad
{
    [super viewDidLoad];
    NSString *fullURL = @"http://local.com/js-more.html";  // this does laod
    NSURL *url = [NSURL URLWithString:fullURL];
    NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
    [arcWebView loadRequest:requestObj];

    NSString * param  = @"foo";   
    NSString * jsCallBack = [NSString stringWithFormat:@"myFunc('%@')",param];
    [arcWebView stringByEvaluatingJavaScriptFromString:jsCallBack];

and in the web page I have:

<script>

function myFunc(this_str){![enter image description here][1]
  alert('here is val: ' + this_str);
}
myFunc('here is in string');
</script>

and this gets alerted correctly in a browser or in an embedded UIWebView.

thx in advance

edit 1

enter image description here

1 Answer 1

1

I'd put the javascript inside the webviewdidfinishload delegate method. As the problem may be that your webpage is not loaded yet, even so you have called load request.

set the webviews delegate, put the code there, and it should work.

EG

- (void)viewDidLoad {
    [super viewDidLoad];
    NSString *fullURL = @"http://local.com/js-more.html";  // this does laod
    NSURL *url = [NSURL URLWithString:fullURL];
    NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
    [arcWebView setDelegate:self];
    [arcWebView loadRequest:requestObj];
}

- (void)webViewDidFinishLoad:(UIWebView *)webView {
    NSString * param  = @"foo";   
    NSString * jsCallBack = [NSString stringWithFormat:@"myFunc('%@')",param];
    [webView stringByEvaluatingJavaScriptFromString:jsCallBack];
}
Sign up to request clarification or add additional context in comments.

1 Comment

Drag from the delegate tab under outlets, to files owner icon. That should do it. Do a quick google of attaching delegate to find out more detailed instrcutions.

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.