Topic
  • 2 replies
  • Latest Post - ‏2012-10-09T13:04:19Z by SystemAdmin
SystemAdmin
SystemAdmin
2327 Posts

Pinned topic [Solved] Interaction between Native and Javascript

‏2012-10-08T13:32:10Z |
I have checked out the API to be able to launch a Native page from Javascript, passing along a JSON object with data.
And conversely I have seen how the NativePage can return to the web view.

All of this interaction appears to be triggered from the web view itself - the web calls out to a native page, some work is performed on the native page, and then control is returned back to the web view.

My question is if I build some logic into the Application Shell, and the Application Shell wants to proactively displaying something in the web view - can the application shell call out to the html / javascript web view tier and trigger a function so that some action can occur on the web view side?

Thanks
Jeff
  • SystemAdmin
    SystemAdmin
    2327 Posts

    Re: Interaction between Native and Javascript

    ‏2012-10-08T15:05:37Z  
    You can try:

    Native to JS:

    
    [webView stringByEvaluatingJavaScriptFromString:@
    "myJSFunction()"];
    


    
    [webView stringByEvaluatingJavaScriptFromString: [NSString stringWithFormat:@
    "myJSFunction(%@)", stringData]];
    


    Source: http://stackoverflow.com/questions/7719881/can-you-call-a-javascript-function-from-native-code-not-in-a-callback-using-ph

    I haven't tried it, let me know if it works.

    JS to Native:

    Module 07.4 - Apache Cordova (PDF, 641KB)
    Module 08.3 - iOS Development Using the Apache Cordova Plugin (PDF, 527KB)
    Module 09.3 - Android Development Using the Apache Cordova Plugin (PDF, 345KB)

    Source: https://www.ibm.com/developerworks/mobile/worklight/getting-started/
  • SystemAdmin
    SystemAdmin
    2327 Posts

    Re: Interaction between Native and Javascript

    ‏2012-10-09T13:04:19Z  
    You can try:

    Native to JS:

    <pre class="jive-pre"> [webView stringByEvaluatingJavaScriptFromString:@ "myJSFunction()"]; </pre>

    <pre class="jive-pre"> [webView stringByEvaluatingJavaScriptFromString: [NSString stringWithFormat:@ "myJSFunction(%@)", stringData]]; </pre>

    Source: http://stackoverflow.com/questions/7719881/can-you-call-a-javascript-function-from-native-code-not-in-a-callback-using-ph

    I haven't tried it, let me know if it works.

    JS to Native:

    Module 07.4 - Apache Cordova (PDF, 641KB)
    Module 08.3 - iOS Development Using the Apache Cordova Plugin (PDF, 527KB)
    Module 09.3 - Android Development Using the Apache Cordova Plugin (PDF, 345KB)

    Source: https://www.ibm.com/developerworks/mobile/worklight/getting-started/
    Yeah - thanks for the tip. It definitely works.

    Even the autogenerated test client invokes javascript WL functions via this mechanism.

    Thanks for the pointer

    Jeff