views:

2075

answers:

4

Hi!

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?

+3  A: 

"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).

Tyler
I am already doing what you suggested, but when I pass the touches to UIWebView the text selection stops working. I can no longer select text for copy.
Mithin
I'm not 100% sure why it's not working. Maybe copy-and-paste (including selecting) is special functionality where the iPhone SDK knows which UIView is foremost, and uses that information -- if that's close to what's happening, then it could be causing your problem. If you clarify your big picture goal, you might be able to get more useful suggestions from stackoverflow.
Tyler
When the div tags with some specific ids in the HTML is tapped on, I want to display a view in which some information can be edited and saved. Also, in the same HTML, user should also be able to select text and save it. While, I have successfully implemented first feature, I am struggling with the second one. When a UIView is added on top of the UIWebView to intercept the taps, text selection stops working. If I remove the overlapping UIView, the selection works fine. Any ideas how I can get both this working?Thanks.
Mithin
The combination of shouldStartLoadWithRequest and stringByEvaluatingJavascriptFromString is the way to go here.
Stephen Darlington
@Stephen: Can you please explain this a little more? I am not sure how shouldStartLoadWithRequest can be helpful in my case.
Mithin
A: 

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.

Corey Floyd
+1  A: 

Answering my own question. Please take a look at this tutorial for the answer

Mithin
A: 

Can anyone help me..the application rejected from App Store because Your app is disabling native WebKit javascript or native Webkit rendering behavior.

To utilize the iOS webkit or the webkit javascript, please refer to the UIWebView Class Reference.

Sam