iOS Question Find within webview

Erel

B4X founder
Staff member
Licensed User
Longtime User
Moved to the questions forum.

1644735303292.png


B4X:
Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    Root.LoadLayout("MainPage")
    WebView1.LoadUrl("https://www.b4x.com/android/forum/threads/find-within-webview.138392/")
    Button1.Enabled = False
    Wait For WebView1_PageFinished (Success As Boolean, Url As String)
    If Success Then
        WebView1.EvaluateJavaScript(File.ReadString(File.DirAssets, "search.js"))
        Button1.Enabled = True
    End If
End Sub


Private Sub Button1_Click
    HighlightOccurences("webview")
End Sub

Private Sub HighlightOccurences(s As String)
    WebView1.EvaluateJavaScript($"uiWebview_HighlightAllOccurencesOfString('${s}')"$)
End Sub
Based on: https://stackoverflow.com/questions/38317747/find-text-in-webview-page-using-swift/38317775#38317775
 

Attachments

  • Project.zip
    9.8 KB · Views: 178
Upvote 0

Kadandale Ganapathy Bhat

Member
Licensed User
Thank you

In this example the searched word highlighted.
If I want to scroll to highlighted spot programmatically how can get

in android we can scroll using webviewxtener library
example - wvext.scrollTo(WebView1,100,500)
like this how can scroll webview to desired position in b4i (ios)?
 
Upvote 0

Semen Matusovskiy

Well-Known Member
Licensed User
It's not a problem to scroll to absolute position. WebView has a scrollView
B4X:
WebView1.As(NativeObject).GetField ("scrollView")
and you can set contentOffset. But this is incorrect way. Correct way - to use JavaScript scrollIntoView (see uiWebview_ScrollTo function in Erel's sample)
 
Upvote 0

Kadandale Ganapathy Bhat

Member
Licensed User
It's not a problem to scroll to absolute position. WebView has a scrollView
B4X:
WebView1.As(NativeObject).GetField ("scrollView")
and you can set contentOffset. But this is incorrect way. Correct way - to use JavaScript scrollIntoView (see uiWebview_ScrollTo function in Erel's sample)
Thank you

please tell me a bit more with example.


and also which is Erel's sample

Thank you
 
Upvote 0

Kadandale Ganapathy Bhat

Member
Licensed User
Thank you, it works perfect.
another thing required, how can the bookmark is made in webpage? and how to scroll to that point where bookmark has been made.?

( in Android WebViewXtender identify the top position of page, but in ios how?)
*** B4A code
Dim lx As Int = wvext.getScrollX(WebView1)
Dim ly As Int = wvext.getScrolly(WebView1)


thank you
 
Upvote 0

Semen Matusovskiy

Well-Known Member
Licensed User
As I wrote before, retrieve a scrollview
B4X:
Dim s As ScrollView = WebView1.As(NativeObject).GetField ("scrollView")
and then you can get/set s.ScrollOffsetX and s.ScrollOffsetY values.

Meanwhile you should do this when a scrollview is still. For example, from our sample:
B4X:
Wait For (WebView1.EvaluateJavaScript($"uiWebview_ScrollTo('${idx}')"$)) WebView1_JSComplete (Success As Boolean, Result As String)
If you will retrieve scroll offsets immediatelly after this statement, you will receive the coordinates before a scroll.

It's possible to use a delegate and to detect when a scroll is finished, but you need to understand obj-c.
 
Upvote 0
Top