1
votes

I am developing an iPhone application and worked with UIWebViews. I planned to customize the UIMenuItems thats popped up when i highlight strings in UiWebView (Copy, Select All, Cut, etc.). I tried removing those menuItems but i cannot remove the COPY menu item.

I need your help guys.

Thanks, ZaldzBugz

2
I believe that this could be done in canPerformAction. But i really dont know how to remove the COPY menu item.Zaldy Bughaw
hi, can u tell me how to remove the others menu item in UIMenuController? i want to remove the arrow itemR. Dewi
@Risma - You got any luck in trying to remove the arrow item and place all the available menus in the main frame itself?Raj Pawan Gumdal

2 Answers

2
votes

You're going to have to do some hijacking. I don't even know if it is possible. It looks like it probably responds to a gesture event called UILongPressGestureRecognizer.

You need to find out what view has the gesture added to it. something like this.

NSArray *gestures = myWebView.gestureRecognizers;
for(int i=0;i<[gestures count];i++){
    UIGestureRecognizer g = [gestures objectAtIndex:i];
    if([g isKindOfClass:UILongPressGestureRecognizer.Class]){
       [myWebView removeGestureRecognizer:g];
       break;
    }
}

but I don't think the gesture is on the UIWebview directly. It is on one of its subviews.

So it might be on this subview

UIScrollView *sview = [[mywebview subviews] objectAtIndex:0];

If not on that one then it is probably on the view that is on top the most, which is what I suspect. It is an undocumented View type, but if you treat it as a UIView for the methods we are calling, it is presumably safe. Also, because you are treating it as a UIView, it isn't against the rules as far as I understand.

UIScrollView *sview = [[mywebview subviews] objectAtIndex:0];
UIView *realWebView = [[sview subviews] objectAtIndex:10];

The reason I didn't write the snippet (which is untested) to try to remove the gesture from all of the views is because it is important you know which view you remove it from.

After you remove it, you need to write your own UILongPressGestureRecognizer and then add the new one to the respective view (the view it was removed from).

You have lost all your functionality of copy select etc... but now you can implement your own.

If you want some of the old functionality, I think I know how you can get it back, but I can't promise this is allowed or safe.

Before you remove the gesture you need to override a method to UIView. You should be able to do it in your current working class file (at the top of the file)

@implementation UIView (extended)
     -(BOOL) respondsToSelector:(SEL)aSelector {
          printf("SELECTOR: %s\n", 
            [NSStringFromSelector(aSelector) UTF8String]);

          return [super respondsToSelector:aSelector];
     }
@end

I can't promise anything, but this should log the name of EVERY method that is called in your view.

Run your app and execute the copy,paste,select,select all methods. Your log should show what methods get called when you press the buttons. It may show several methods when you push a button, you probably just want the first.

Now when you create a menuItem you know what selector to assign to it.

This is all just a guess. Hope it helps.

1
votes

I used the technique in this answer to a similar question and it works for ridding of the Copy menu item. That is, override UIWebView instead of UITextView in their example. Also wrote a canPerformAction:withSender: in the view controller that returns YES for my custom selectors and NO for the copy: selector, then returns the value of the super implementation if the selector is none of those. Remember to set the custom UIWebView subclass name in your xib.