iOS Question jsinterface

Discussion in 'iOS Questions' started by mauro vicamini, Feb 2, 2015.

  1. mauro vicamini

    mauro vicamini Active Member Licensed User

    Hi Erel,
    is there a library like jsinterface of B4A in B4I to comunicate from a webview javascript to B4I code?
    I've seen that is possible to interact from B4I to javascript, but I don't have seen the opposite.

    Thank a lot
     
  2. Erel

    Erel Administrator Staff Member Licensed User

    It is not possible in iOS to directly call a method from the JavaScript. You can however use the OverrideUrl event as an alternative method.
     
  3. Derek Johnson

    Derek Johnson Active Member Licensed User

    Another way that you could communicate from the Javascript to B4I would be to change some property of the document eg for simplicity the title

    Code:
    document.title= 'Element ' + i +' not found ' + id
    then when the Javascript has completed you can recover this as follows:

    Code:
    Dim no As NativeObject = WebView1
    Dim Result As String
    Result=no.RunMethod(
    "stringByEvaluatingJavaScriptFromString:"Array('document.title')).AsString
    If you get a bit more clever you could probably write something that was invisible on the end of the document and recover it in a similar way. I'm sure someone in this forum could suggest how to do this.
     
  4. Derek Johnson

    Derek Johnson Active Member Licensed User

    I've now written and tested some code that allows you to write some hidden data into a WebPage and then retrieve that data using B4I. This isn't quite synchronous, but you could poll for the data using a timer if you wished.

    Here is the code:

    Code:
    Sub Test
        
    Dim no As NativeObject = WebView1
        
    Dim html=$"
        //Create style that is not displayed
        var x = document.createElement("STYLE");
        var t = document.createTextNode("#obscurename {display:none; visibility:hidden;}");
        x.appendChild(t);  
        document.head.appendChild(x);  

        //Write something at the end of the document
        var newNode = document.createElement('div');
        newNode.id = 'obscurename';
        newNode.innerHTML = 'Text to be returned....';
        document.body.appendChild(newNode);        
    "$

        no.RunMethod(
    "stringByEvaluatingJavaScriptFromString:"Array(html))
       
    End Sub   
       
    Sub GetIT   
        
    'Now retrieve the data
        Dim no As NativeObject = WebView1
        
    Dim result As String 
        result=no.RunMethod(
    "stringByEvaluatingJavaScriptFromString:"Array("document.getElementById('obscurename').innerHTML")).AsString
        
    Log($"Result was '${result}'  "$)
       
    End Sub
    I verified that this works on an iPad with IOS8 and on an iPhone 4 with IOS7.

    The actual text is not visible and does not take up space on the current webpage.
     
    TWCoder and mauro vicamini like this.
  5. mauro vicamini

    mauro vicamini Active Member Licensed User

    Thanks, interesting solution.
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice