how to change UIWebView Tap events

2019-07-17 17:43发布

问题:

How Can I change the UIWebView tap action Controls.like when I tap and hold on some link in UIWebView ..it opens a UIActionSheet with three options open copy add to reading list ...I need to change this UIActionsheet controls like ..I need to add one more button into this ...how to do that...how to disable this and add new UIActionSheet according to my choice...

code

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
NSURL *requestURL = [ request URL];

if (navigationType == UIWebViewNavigationTypeLinkClicked)
{
    // Call your custom actionsheet and use the requestURL to do what you want :)


    UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:@"Contextual Menu"
                                                       delegate:self cancelButtonTitle:@"Cancel"
                                         destructiveButtonTitle:nil otherButtonTitles:nil];

    [sheet addButtonWithTitle:@"Save Page as Bookmark"];
    [sheet addButtonWithTitle:@"Open Page in Safari"];

    [sheet showInView:webView];
    return NO;
}

return YES;
}


- (void)viewDidLoad
{
[super viewDidLoad];

[webview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://google.com"]]];

webview.delegate=self;

}



- (void)actionSheet:(UIActionSheet *)actionSheet
clickedButtonAtIndex:(NSInteger)buttonIndex {


if (buttonIndex == actionSheet.cancelButtonIndex) { return; }
switch (buttonIndex) {
    case 0:
    {
        NSLog(@"Item A Selected");
   NSLog(@"reg%@", request);

          NSURL *requestURL = [request URL];
        [webview loadRequest:[NSURLRequest requestWithURL:requestURL]];



        break;
    }
    case 1:
    {
        NSLog(@"Item B Selected");


        break;
    }

}

}

回答1:

You can do like this , catch a tap on link and use your actionsheet then

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType 
{
    NSURL *requestURL = [ request URL];

    if (navigationType == UIWebViewNavigationTypeLinkClicked)
    {
        // Call your custom actionsheet and use the requestURL to do what you want :)
        return NO;
    }

    return YES;
}


- (void)viewDidLoad
{
    [super viewDidLoad];


     webview.delegate=self;
     [webview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://google.com"]]];



}