-1

Possible Duplicate:
How to call an Objective-C method from Javascript in a Cocoa/WebKit app?

How to call a simple Objective- C function from Javascript. Say I have a function :

-(void) callFromJavascript {
// How to call this method of Objective -C from a Javascript function.

}

Community
  • 1
  • 1
Cathy
  • 797
  • 2
  • 14
  • 25
  • check this link u will get your answer http://blog.techno-barje.fr/post/2010/10/06/UIWebView-secrets-part3-How-to-properly-call-ObjectiveC-from-Javascript/ – SSP Dec 27 '12 at 11:50
  • The answer I used was here: http://stackoverflow.com/a/2293305/105539 In my comments below it, I showed how to do it even easier. – Volomike Dec 11 '15 at 06:11

1 Answers1

0

One mechanism is to intercept document.location changes (and cancel them) and call your method when the requested change to document.location matches a URL you predefined.

Then from JS you just change document.location to that predefined URL.

To intercept the changes override:

- (BOOL)webView:(UIWebView *)_webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType;

and return NO when request matches what you're setting in JS as document.location.

sqreept
  • 5,236
  • 3
  • 21
  • 26