English 中文(简体)
议题与网络观点和压倒一切的联系
原标题:issue with web view and overriding links

I have a UIWebView in which some parts of it have an <a href="" > <img src="" </a> in it. I have added a UITapGesture to this UIWebView and so what I want is basically when a user taps on an image, it doesn t bring you to the link pointed to it but rather do something. I am having issues in preventing the image to go to the hyperlink it s pointed to. Any idea? I ve tried injecting javascript so that it changes the href to some fake link when the user taps the image and then on the:

- (BOOL)webView:(UIWebView *)webview shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {

如果与我所看到的怀疑相匹配,我就检查了请求书。 然而,由于以下原因,这并不奏效:

NSString *stripLink = [NSString stringWithFormat:@"document.elementFromPoint(%f, %f).parentNode.href=%@", pt.x, pt.y, @"http://www.fakeurl.com"];
    if ([self stringByEvaluatingJavaScriptFromString:stripLink] != nil){
        NSString *testHref = [NSString stringWithFormat:@"document.elementFromPoint(%f, %f).parentNode.href", pt.x, pt.y];
        NSLog(@"FINAL HREF IS %@", [self stringByEvaluatingJavaScriptFromString:testHref]);
    }

是否有任何其他想法?

问题回答

如果你控制了超文本源(并且与你一样正确),那么你可以改变手法,使用URI的习俗,例如光电灯,然后你可以做以下工作:

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
    NSURL *url = [request URL];
    if ([[url scheme] isEqualToString:@"photo"])
    {
         // Custom Handling Goes Here
         return NO;  // Stops the link being executed by UIWebView
    } else
         return YES;
}

然而,如果你无法控制超文本来源,那么你可能需要稍加谨慎。 例如,你可以检查道路的结束,看看它是否具有形象(jpg、png等)。





相关问题
List Contents of Directory in a UITableView

I am trying to list the contents of Ringtones directory in a TableView, however, I am only getting the last file in the directory in ALL cells, instead of file per cell. This is my code: - (...

iPhone NSUserDefaults persistance difficulty

In my app i have a bunch of data i store in the NSUserdefaults. This information consists of an NSObject (Object1) with NSStrings and NSNumbers and also 2 instances of yet another object (Object2). ...

Writing a masked image to disk as a PNG file

Basically I m downloading images off of a webserver and then caching them to the disk, but before I do so I want to mask them. I m using the masking code everyone seems to point at which can be found ...

Resize UIImage with aspect ratio?

I m using this code to resize an image on the iPhone: CGRect screenRect = CGRectMake(0, 0, 320.0, 480.0); UIGraphicsBeginImageContext(screenRect.size); [value drawInRect:screenRect blendMode:...

Allowing interaction with a UIView under another UIView

Is there a simple way of allowing interaction with a button in a UIView that lies under another UIView - where there are no actual objects from the top UIView on top of the button? For instance, ...

热门标签