0

I found WebView/UIWebView in OSX/iOS give us great ability to build fancy UI layout with html/css/javascript. I'd like to know how javascript in the UIWebView can call the object-c object outside the UIWebView?

I searched in the forum, and found this thread: Javascript in UIWebView callback to C/Objective-C and document here.

But it seems only way the web view delegate will be called only when the view load/load finish or something like that. Is there any way the javascript can call the object outside the view at any time?

Community
  • 1
  • 1
icespace
  • 931
  • 1
  • 9
  • 17

2 Answers2

1

See my answer here: NSString in UIWebview

Community
  • 1
  • 1
Michael Kessler
  • 14,245
  • 13
  • 50
  • 64
1

I have found that setting window.location inside UIWebView seems to have side-effects. In particular, it prevents any further JS manipulation of the DOM, even if your shouldStartLoadWithRequest: returns YES.

There is a solution for console logging involving a polling mechanism described here which could be extended to allow invocation of any arbitrary objective-c method - but it probably would have performance implications.

Another solution, if you are willing to do without the javascript alert() function is to override the UIWebView's runJavaScriptAlertPanelWithMessage: method in a Category. This is used here to change the look of the alert dialog, but could also be used to invoke any arbitrary objective-c method based on the contents of the message. However, be warned that this is an undocumented API and therefore shouldn't be used in production (it will likely get your app rejected).

Community
  • 1
  • 1
wodenx
  • 281
  • 2
  • 5