UIWebView events and hold to select/copy

2019-05-20 04:32发布

I have created a subclass of UIWebView and have added a UIView on top of it in order to catch the touch events and use them.

Now, due to the extra view added on top of the UIWebView the text selection is not working at all. When I remove the extra UIView the text gets selected but then I cannot identify the events.

Is there a way by which both the functionalities can co-exist?

[EDIT]

May be my post was not clear enough. When I subclass UIWebView to handle events, selection stops working. I cannot select text for copy anymore. Any ideas why?

2条回答
疯言疯语
2楼-- · 2019-05-20 04:51

"The UIWebView class should not be subclassed." - from Apple's UIWebView docs.

It sounds like you're trying to mess with how a person interacts with a web page, which is likely to get you rejected from the app store. (For a lot of possible rejection reasons, check out app rejected.)

If you're not worried about that, here is some advice that might help you achieve your goals:

  • If you want to control where the user can and can't go via hyperlinks, or just perform some code whenever they click on some links, you can add a hook via the webView:shouldStartLoadWithRequest:navigationType: method of the UIWebViewDelegate protocol. Very handy.
  • If you want to perform some simple modifications to how the page acts or looks, you can essentially execute your own javascript in the page with a call to stringByEvaluatingJavaScriptFromString:, a method of UIWebView itself. Just pass in your javascript as a string.

And, in case that doesn't give you want you want (in which case you're really going to tick off them app store review guys), then you can probably do what you're already doing, and just propagate all those UITouchs right on through to the UIWebView itself. Something like this, as an example (in the overlapping UIView):

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
  [self doWhatever];
  [underlappingWebView touchesMoved:touches withEvent:event];
}

That way you can have your cake (get the user touch info) and eat it too (have the web page act as it normally does).

查看更多
【Aperson】
3楼-- · 2019-05-20 04:57

Not what you want to hear, but you definitely need to rethink your strategy to get the functionality you want.

UIWebview has a lot of complex behavior, it encapsulates an entire web rendering engine.

You can probably achieve your goals a different way (perhaps a toolbar item) or the functionality you desire may be hidden in a delegate method.

查看更多
登录 后发表回答