iOS Question Websocket server in iOS

wl

Well-Known Member
Licensed User
Longtime User
Hello,

Is there a simple way to communicate in both directions between B4I code in an app and a webview (local HTML pages) that is embedded within the same app ?

If not I was thinking of writing a websocket server to do the communication:

Would the following be possible and allowed by Apple ?

A B4I app that
- contains a webview that communicates with ...
- ... a websocket server also embedded in the app and written in B4I using raw sockets

The HTML pages in the webview would then be able to communicate with the websocket server to obtain device specific data (eg: GPS coordinates). The websocket would need to service local requests even when external network (WIFI or 2/3/4G) is down.

Thanks
 
Last edited:

wl

Well-Known Member
Licensed User
Longtime User
I would build a websocket server using a TCP server based on the description of the protocol (see for example: https://developer.mozilla.org/en-US/docs/Web/API/WebSockets_API/Writing_WebSocket_server).

As my only intention is to have javascript inside a webview communicate in both directions with code in the B4I app, I meanwhile understoord there is a simpler way:

app -> Javascript
Log(no.RunMethod("stringByEvaluatingJavaScriptFromString:", Array("document.title")))

Javascript -> app
OverrideUrl

Correct ?
 
Upvote 0

wl

Well-Known Member
Licensed User
Longtime User
Thanks.

Any idea what the maximum length of a URL is and thus the amount of data that can be transmitted from Javascript to B4I code through OverrideUrl ?
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
More than 100,000 characters.

B4X:
Dim sb As StringBuilder
sb.Initialize
sb.Append($"<a href="http://test.test"$)
For i = 1 To 100000
   sb.Append("x")
Next
sb.Append($"">click here</a>"$)
WebView1.LoadHtml(sb.ToString)

Sub WebView1_OverrideUrl (Url As String) As Boolean
   Log(Url.Length)
   Log(Url.SubString(Url.Length - 10))
   Return True
End Sub
 
  • Like
Reactions: wl
Upvote 0
Top