2

I'm trying to disable hyperlink and copy/paste menu on iOS UIwebview so I googled and there were a lot of answers. then I encountered with another problem 'webviewdidfinishload not called' so I add my code to ask your help. (Iam sure I properly connected IBOutlet)

In additional, I still not get the idea of putting mainWebView.delegate = self instead of self.mainWebView.delegate = self could anyone explain the difference? so iam not sure my other functions like [[[mainWebView subviews] lastObject] setScrollEnabled:YES] [[[mainWebView subviews] lastObject] setScrollsToTop:NO]
mainWebView.scalesPageToFit = YES these are working or not.

it should be working on ios5 to ios6.1 thank you

in .h file

@interface HRViewController : UIViewController <UIWebViewDelegate>
{
    UIWebView *mainWebView;
}
@property (strong, nonatomic) IBOutlet UIWebView *mainWebView;

@end

in .m file

@interface HRViewController ()

@end

@implementation HRViewController

@synthesize mainWebView;

- (void)viewDidLoad
{
    [self startWebView];
    [super viewDidLoad];
}

- (void)startWebView
{
    mainWebView.delegate = self;

    [[[mainWebView subviews] lastObject] setScrollEnabled:YES];
    [[[mainWebView subviews] lastObject] setScrollsToTop:NO]; 
    mainWebView.scalesPageToFit = YES;

    mainWebView = [[UIWebView alloc] initWithFrame:self.view.bounds];
    mainWebView.scalesPageToFit = YES;
    [self.view addSubview:self.mainWebView];

    NSURL *url = [NSURL URLWithString:/" @"http://www.mysite.com" "/];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];

    [mainWebView loadRequest:request];
}

- (void)webViewDidFinishLoad:(UIWebView *)WebView {
    NSLog(@"finish");

    [WebView stringByEvaluatingJavaScriptFromString: @"document.documentElement.style.webkitUserSelect='none';"];
    [WebView stringByEvaluatingJavaScriptFromString: @"document.documentElement.style.webkitTouchCallout='none';"];
}

- (void)webViewDidStartLoad:(UIWebView *)mainWebView {
    NSLog(@"start");
}

- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {
    NSLog(@"Error for WEBVIEW: %@", [error description]);
}

EDIED I used below codes but still doesnt work... none of these doesnt work!!!!!

- (BOOL)canPerformAction:(SEL)action withSender:(id)sender
{
    NSLog(@"canPerformAction: %@",NSStringFromSelector(action));
    if (action == @selector(copy:) ||
        action == @selector(paste:)||
        action == @selector(cut:)) {
        return NO;
    }
    return NO;
}

and this

- (BOOL)canPerformAction:(SEL)action withSender:(id)sender
{
    UIMenuController *menuController = [UIMenuController sharedMenuController];
    if (menuController) {
        [UIMenuController sharedMenuController].menuVisible = NO;
    }
    return NO;
}

even this - (void)viewDidLoad { mainWebView.delegate = self; }

- (void)webViewDidFinishLoad:(UIWebView *)webView {
    NSLog(@"finish");
    // Disable user selection
    [webView stringByEvaluatingJavaScriptFromString:@"document.documentElement.style.webkitUserSelect='none';"];
    // Disable callout
    [webView stringByEvaluatingJavaScriptFromString:@"document.documentElement.style.webkitTouchCallout='none';"];
}

also this - (void)viewDidLoad { [self longPress:self.mainWebView]; }

- (void)longPress:(UIView *)webView {
    UILongPressGestureRecognizer* longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleLongPress)];

    longPress.allowableMovement=100;
    longPress.minimumPressDuration=0.3;
    longPress.delaysTouchesBegan=YES;
    longPress.cancelsTouchesInView=YES; 
    [webView addGestureRecognizer:longPress]; 
}

// I just need this for the selector in the gesture recognizer.
- (void)handleLongPress {

}

Please help me.. it drives me crazy for a week now..

1 Answers1

-1

To hide the copy/paste menu:

Override the canPerformAction:withSender: method to return NO for actions that you don't want to allow:

- (BOOL)canPerformAction:(SEL)action withSender:(id)sender
{
    if (action == @selector(paste:))
        return NO;

    if (action == @selector(select:))   
        return NO;

    if (action == @selector(selectAll:))   
        return NO;

    return [super canPerformAction:action withSender:sender];
}

Another way:

- (BOOL)canPerformAction:(SEL)action withSender:(id)sender
{
    UIMenuController *menuController = [UIMenuController sharedMenuController];
    if (menuController) {
        [UIMenuController sharedMenuController].menuVisible = NO;
    }
    return NO;
}

Also check This link

To disable hyperlinks:

You can get the answer for this question from this site.

Regarding the UIWebView:

If you use ARC then write only,

@property (strong, nonatomic) IBOutlet UIWebView *mainWebView;

No need to @synthesize the property, Other wise,

@interface HRViewController : UIViewController <UIWebViewDelegate>
{
    UIWebView *mainWebView;
}

@property (nonatomic,retain) IBOutlet UIWebView *mainWebView;

@end

// .m file

- (void)viewDidLoad
{
    mainWebView = [[UIWebView alloc] initWithFrame:self.view.bounds];
    mainWebView.scalesPageToFit = YES;
    mainWebView.delegate = self;

    [[[mainWebView subviews] lastObject] setScrollEnabled:YES];
    [[[mainWebView subviews] lastObject] setScrollsToTop:NO];
    mainWebView.scalesPageToFit = YES;

    [self.view addSubview:self.mainWebView];

    NSURL *url = [NSURL URLWithString:@"http://www.mysite.com"];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    [mainWebView loadRequest:request];
}

This might be helpful for you.

iPatel
  • 46,010
  • 16
  • 115
  • 137
  • Thank you for your answer. Unfortunately it still doesnt work for me. I tried them all but nothing worked. I even disabled longpressgesture but doesnt work. Adding javascript to CSS didnt work either.. I dont know the problem but it drives me crazy.. but one progress is now webViewDidFinishLoad is called(nslog comfirmed.) addtional help needed – Seung Hyun Sean Lee Feb 13 '13 at 08:43