Can Javascript call anything in iOS when a certain event fires?
I'm implementing an iPhone App that uses WebSockets (inside a hidden UIWebView), so I ne开发者_运维技巧ed some communication between Javascript and Obj-C.
I'm aware of the [webview stringByEvaluatingJavaScriptFromString] method and that's great for sending websocket responses, but I was thinking more along the lines of "when an event fires in JS, let iOS know". I would be using this concept to trigger iOS to do stuff when it receives a certain websocket response.
Any ideas on how to solve such a problem? Thanks in advance
I think the best and only way to do this is to do a window.location.href in JS when the event is fired, and use the WebView method:
(BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType
You can then capture the URL and decide whether or not to reload the page. Then use the URL to take the appropriate action on the Objective-C side of things. More details here:
精彩评论