2

i would like to know how to call an objective c method from javascript. for example i have a javascript function defined in my myFile.js like this :

function() {
alert('test');
....          // How i can an objective c function here ???
}

i have also a UIWebView :

-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
....
// what to do here ?????? 
}

thanks for your answer

1
  • There are many question in SO like you ask for. please search for them. Commented May 6, 2012 at 17:21

1 Answer 1

3

Just set the location to something you recognize and check if the request.URL matches that.

function nav(f){
 location.href = "methodCall";
}

-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
    if([request.URL.relativeString isEqualToString:@"methodCall"]){
        //call method here
        return false; //so it doesn't navigate anywhere
    }
    return true; //if you want every other link to work normally.
}

Note: I haven't tested this but I imagine it will work, though the URL returned by request.URL.relativeString may be a bit different, so just set a breakpoint there and see what it is.

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.