2

作为我正在开发的应用程序的一部分,用户需要能够从各种地方导入图像,包括从网络下载它们。我添加了一个应用内浏览器,并包含一个上下文相关菜单,以便在用户长按特定项目时为他们提供不同的选项。

我的代码使用 Javascript 来检测 HTML 元素的标签,并且基于以下教程和 SO 帖子:

http://www.icab.de/blog/2010/07/11/customize-the-contextual-menu-of-uiwebview/

UIWebView - 在 <img> 标签上启用操作表

以下方法由 UILongPressGestureRecognizer 调用:

(void)openContextMenuAt:(CGPoint)pt
{
    // Load the JavaScript code from the Resources and inject it into the web page
    NSString *path = [[NSBundle mainBundle] pathForResource:@"JSTools" ofType:@"js"];
    NSString *jsCode = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
    [self.webView stringByEvaluatingJavaScriptFromString: jsCode];

    // get the Tags and URL at the touch location
    NSString *tags = [self.webView stringByEvaluatingJavaScriptFromString:
            [NSString stringWithFormat:@"MyAppGetHTMLElementsAtPoint(%i,%i);",(NSInteger)pt.x,(NSInteger)pt.y]];
    NSString *elementURL = [self.webView stringByEvaluatingJavaScriptFromString:
            [NSString stringWithFormat:@"MyAppGetLinkHREFAtPoint(%i,%i);", (NSInteger)pt.x, (NSInteger)pt.y]];

    // create the UIActionSheet and populate it with buttons related to the tags
    UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:elementURL
                                                       delegate:self
                                              cancelButtonTitle:nil
                                         destructiveButtonTitle:nil
                                              otherButtonTitles:nil];

    BOOL shouldShowMenu = NO;

    // If a link was touched, add link-related buttons
    if ([tags rangeOfString:@",A,"].location != NSNotFound) {
        [sheet addButtonWithTitle:NSLocalizedString(@"Open", nil)];
        shouldShowMenu = YES;
    }

    // If an image was touched, add image-related buttons
    if ([tags rangeOfString:@",IMG,"].location != NSNotFound) {
        [sheet addButtonWithTitle:NSLocalizedString(@"Save Image", nil)];
        shouldShowMenu = YES;
    }

    // check the item to see if it has an audio link attached
    NSSet *validAudioExtensions = [NSSet setWithObjects:@"aiff", @"aac", @"alac", @"m4a", @"mp3", @"raw", @"wav", nil];
    NSString *urlSuffix = [[elementURL pathExtension] lowercaseString];
    if ([validAudioExtensions containsObject:urlSuffix]) {
        [sheet addButtonWithTitle:@"Save Audio"];
        shouldShowMenu = YES;
    }

    // Add buttons which should be always available
    [sheet addButtonWithTitle:NSLocalizedString(@"Open in Safari", nil)];

    if (shouldShowMenu == YES)
    {
        CGRect menuTarget = CGRectMake(pt.x, pt.y, 1, 1);
        [sheet setActionSheetStyle:UIActionSheetStyleBlackOpaque];
        [sheet showFromRect:menuTarget inView:self.webView animated:YES];
    }
}

直到最近,这几乎在我测试的每个网站上都能完美运行。然而,它已经停止了谷歌的图像搜索(不幸的是,这是大多数用户可能会首先查看的地方)。

在主要搜索结果(带有图像缩略图)上,我能够检测到 IMG 标签并下载缩略图图像。但是,如果我点击缩略图以调出更大的全尺寸图像,我将无法再检测到我在按什么。此外,我无法从任一视图中检索 URL(打印 url 仅显示一个空字符串)。

是否有其他更好的方法来检测按下哪个元素?我需要为 Google 使用特定的解决方法吗?

在此先感谢您的帮助。

4

0 回答 0