iOS Code Snippet WebView "extra"

B4i didn't have WebView extra library (yet) so I collect the code from around the forum. Hope it's useful for you.

1. Hide scrollbar of WebView:
B4X:
Dim no As NativeObject = WebView1
no.GetField("scrollView").SetField("showsHorizontalScrollIndicator", False)
no.GetField("scrollView").SetField("showsVerticalScrollIndicator", False)

2. Set background to transparent:
B4X:
Dim no As NativeObject = WebView1
no.SetField("opaque", False)

3. Disable Scroll:
B4X:
Dim no As NativeObject = WebView1
no.GetField("scrollView").SetField("scrollEnabled", False)

4. Go Back:
B4X:
Sub GoBack(wv As WebView)
Dim no As NativeObject = wv
  If no.RunMethod("canGoBack", Null).AsBoolean = True Then
   no.RunMethod("goBack", Null)
  End If
End Sub

5. Clear Cache:
B4X:
Dim no As NativeObject
no.Initialize("NSURLCache").RunMethod("sharedURLCache", null).RunMethod("removeAllCachedResponses", null)

I will update more.
 
Last edited:

moster67

Expert
Licensed User
Longtime User
Snippet to obtain title of loaded webpage (corresponding to the getTitle method in the WebViewXtender-library in B4a).
This snippet requires that JavaScript is always enabled in UIWebView but apparently it cannot be turned off in iOS so this snippet should always work.

(Based upon Erel's example for injecting JavaScript)

B4X:
Dim no As NativeObject = WebView1
Log(no.RunMethod("stringByEvaluatingJavaScriptFromString:", Array("document.title")))
 
Last edited:

youjunjer

Member
Licensed User
Longtime User
Is there any method to detect page on loading, I need to make a msgbox to show "page on loading".
And if network or website is error or unavailable, I need to redirect to a local page.
Thank for a lot....
 

joop

Active Member
Licensed User
Longtime User
Instead of full screen , inline media playing

A Boolean value that determines whether HTML5 videos play inline or use the native full-screen controller.


B4I code:

B4X:
Dim no As NativeObject = WebView1
no.SetField("allowsInlineMediaPlayback", True)


And the HTML code :

B4X:
<iframe webkit-playsinline width="200" height="200" src="https://www.youtube.com/embed/KKYTdhfwrAM?feature=player_detailpage&playsinline=1" frameborder="0"></iframe>
 
Last edited:

Baris Karadeniz

Active Member
Licensed User
B4i didn't have WebView extra library (yet) so I collect the code from around the forum. Hope it's useful for you.

1. Hide scrollbar of WebView:
B4X:
Dim no As NativeObject = WebView1
no.GetField("scrollView").SetField("showsHorizontalScrollIndicator", False)
no.GetField("scrollView").SetField("showsVerticalScrollIndicator", False)

2. Set background to transparent:
B4X:
Dim no As NativeObject = WebView1
no.SetField("opaque", False)

3. Disable Scroll:
B4X:
Dim no As NativeObject = WebView1
no.GetField("scrollView").SetField("scrollEnabled", False)

4. Go Back:
B4X:
Sub GoBack(wv As WebView)
Dim no As NativeObject = wv
  If no.RunMethod("canGoBack", Null).AsBoolean = True Then
   no.RunMethod("goBack", Null)
  End If
End Sub

5. Clear Cache:
B4X:
Dim no As NativeObject
no.Initialize("NSURLCache").RunMethod("sharedURLCache", null).RunMethod("removeAllCachedResponses", null)

I will update more.

Can please send Main?
 

Erick Kalugdan

Member
Licensed User
In B4A there is a method "addJavascriptInterface" so that if I want to call an app Sub from the javascript inside the webview, I just call it via APP.CallSub

Does this exist in B4i too? How? Sample code would be greatly appreciated.

Thanks in advance!
 

youjunjer

Member
Licensed User
Longtime User
what i use is to detect the webview's url.
for example, when webview load a url compete, it will do an event is load onPageFinished, so that you can do something when the url is what you need
 
Top