iOS Question jsinterface

mauro vicamini

Active Member
Licensed User
Longtime 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
 
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: Copy
document.title= 'Element ' + i +' not found ' + id

then when the Javascript has completed you can recover this as follows:

Code: Copy
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.
 
Upvote 0
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: Copy
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.
 
Upvote 0
Back
Top