Programmatically scroll in a UIWebView - ios

I have a UIWebView which I want to perform a scroll programmatically. So say for instance if you press a UIButton it will scroll a few pixels down or so.
Would this be possible?

I think you should get UIScrollView of Webview first.
After that, you can control your UIScrollView.

https://github.com/marcuswestin/WebViewJavascriptBridge
https://github.com/newyankeecodeshop/GAJavaScript
use Native javascript using Objective-C function like windows.scrollTo(0,1000)

Scrolling inside a webview is controllable with JavaScript. Example using scrollTo:
With UIWebView (deprecated)
DispatchQueue.global().async {
_ = webView.stringByEvaluatingJavaScript(from: "window.scrollTo(0,100);")
}
With WKWebView (iOS 8+)
webView.evaluateJavaScript("window.scrollTo(0,100);")

Related

How to pass events from UIView to WKWebview underneath?

I have a WKWebview that I basically want to override the zooming behavior for. I want to scale up the font, so what I have right now is a UIView on top of the WKWebview that I've added a UIPinchGestureRecognizer to that does some evalutateJavascript call on the webview. However, now the Webview doesn't receive tapping or scrolling events as I believe the overlay view is capturing them. Is there a way to send it down to the webview? These are all in a single VC with outlets to both the webview and the overlay view. I wanted to override the behvaior directly for the WKWebview but I can't seem to get it to work.
Basically I know if you need to deliver gesture event to other responders, just set the pinchGestureRecognizer.cancelsTouchesInView = false

When WKWebView embed in a UITableViewCell, secondHalf of WebView will not display

This is very strange. I embed a WKWebView into the first cell(IndexPath.row = 0)of a UITableView . and if the webView's content is short ,it works fine.But when I put more text and image in the webView .the bottom part of the webView is not display. I have check the UI layer:
And I found the most upper layer(WKCompositingView) 's height is not right. the second layer is WKCompositingView too.
I don't know why,Buy if I put the WebView into a controller's view it worked fine.
Another point is , if I called a UITextFiled to first responder(call the keyboard out), than then webView will display normally。(but became 2 part ,both WKCompositingView).
can anyone tell me why?
Final , I solved this question by adding [webViewCell setNeedsLayout] in the scrolldViewdidscroll method.
I think, first cell not show out of content. You can add webview into view. After that, add this view into first cell. First cell will show out of content webview.

How can you handle touching a link within a UIWebView when you have a UIView overlay directly on top of it?

I've been researching how to use a UIWebView embedded in a UIScrollView for a while now and cant seem to find an easy fix to my problem. I'm using a UIWebView because a UITextView, although allowing hyperlinks, does not allow you to change the link color from the standard blue apple sets.
But using a UIWebView introduces another problem: It blocks the UIScrollView it is embedded in from handling scroll events because a UIWebView handles these events within its own private implementation.
Although this allows me to use its embedded links, the UIWebView absorbs the touch events which doesn't allow me to scroll up/down when dragging within the UIWebView itself. I worked around this problem by placing a transparent UIView overlay exactly over the UIWebView, intercepting the touch events that it receives, and resigning it as first responder so that the UIScrollView that they are both subviews of can handle the scrolling appropriately.
Now, however, I can't seem to think of solution to the new problem this introduces: I can't click on the links within my UIWebView that is right under the UIView Overlay.
What is the best way to forward touch events to a UIWebView that is right underneath my transparent UIView in order to trigger my UIWebView's delegate method 'webView:shouldStartLoadWithRequest:navigationType:' for tapping on links?
FYI from UIWebView API doc:
Important: You should not embed UIWebView or UITableView objects in UIScrollView objects. If you do so, unexpected behavior can result because touch events for the two objects can be mixed up and wrongly handled.
Back to the question,
UIWebView has property called scrollView, try doing:
webView.scrollView.scrollEnable = NO;

Stop UIView being dragged around screen

We are integrating a third party offer wall into our iOS phonegap app.
When we show the offerwall it gets added to the standard phonegap viewcontroller and shows over the top of our webview.
The problem with it is that people can drag the view all over the place so instead of just scrolling up and down in place. Which gives the effect show in the screenshot below:
What we want to achieve is being able to anchor this view so it can't be dragged around the app and can only be scrolled vertically.
In the integration code we have access to a UIView for the offerwall and the ViewController of the main app.
The offerwall is provided as a library so I don't have access to any of its code and can only deal with the UIView returned and the UIViewController I add it to. Other apps have managed to implement the view without horizontal scrolling
We are looking for code to apply to either the UIView or ViewController to prevent this.
Thanks
The offerwall view probably contains a UIScrollView that holds the actual content. You could try looping through all subviews and turn off scrolling on the first scroll view it finds:
for (UIView *view in offerwall.subviews) {
if ([view isKindOfClass:[UIScrollView class]]) {
UIScrollView *scrollView = (UIScrollView*)view;
scrollView.scrollEnabled = NO;
return;
}
}
This is definitely hacky, but if you don't have access to anything else from their code it might be your only option.
In the end the solution was quite simple. Turns out the offerwall was a UIWebView and in testing mode the offerwall page was rendering some elements that were overflowing the page hence the horizontal scrolling. In production mode the problem goes away

UIWebView inside UITableViewCell, link detected but not clickable

I have UITableView with one UITableViewCell subclass.
In that class I'm creating UIWebView and set the delegate to the class.
I'm also implementing shouldStartLoadWithRequest.
On loading the tableview, I call the cell.webView loadHTMLString with HTML string (not from the web).
After I call loadHtmlString the shouldStartLoadWithRuquest is called with navigationtype 5.
This also mean the delegate works OK.
Now, I can see the UIWebView with text and blue links, but when I click on the link the shouldStartLoadWithRequest is not called and nothing happen.
I read something about not implementing UIWebView in UIScrollView, but I also disable the scrolling of the UIWebView, I'm just using it as UILabel to display HTML text.
Any ideas why clicking on the link does not work and shouldStartLoadWithRequest is not fire?
grrr.
answering my own question.
the UITableViewCell object was set with userInteractionEnabled = NO in other place..
after changing to YES, it works. not my lucky day.

Resources