0

I am working with a webapp in iPhone and I have problem with calling native methods from JavaScript.

I need a value which is in my viewController class, so that I am calling native method by using window.location and in shouldStartLoadWithRequest I can find which value is needed then I will send that value by using [webView stringByEvaluatingJavaScriptFromString:userId];

Based on this value, I need to call web services in native code. This time shouldStartLoadWithRequest is not firing. i.e simply "JS -> Native -> JS -> Native" in this case webView delegate not firing.

Would you please give me a solution for this.

1
  • could you post some code? perhaps on webView delegate? Commented Mar 3, 2012 at 13:00

1 Answer 1

2

running web.html stored in Resource.

    NSString *path = [[NSBundle mainBundle] pathForResource:@"web" ofType:@"html"];
    NSString *jsCode = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];

    [webView stringByEvaluatingJavaScriptFromString:jsCode];
    [webView setDelegate:self];
    [webView loadHTMLString:jsCode baseURL:nil];

in web.html, in body, you should have onload(), that will fire after webView start.

<html> <body> <head>

<script>

function anyFunction(){
    window.location='testing123';
}

</script>

</head><body onload="anyFunction();">
</body></html>

from there, you should be able to grab "testing123".

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType 
{
    NSString *data = [[[request URL] absoluteString] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    if ([data isEqualToString:@"testing123"])
        NSLog(@"value received");
}
Sign up to request clarification or add additional context in comments.

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.