【问题标题】:How to load a link in safari from a pdf being displayed in a UIWebView ios如何从 UIWebView ios 中显示的 pdf 加载 Safari 中的链接
【发布时间】:2015-02-13 10:13:29
【问题描述】:
I am using the Page View Application to show a series of PDF's. The UIWebView is displaying the PDF's fine, however whenever there is a link that is embedded into the PDF and the user clicks it, it opens the link inside of the UIWebView. I want it to open said link in safari.

NSString *path = [[NSBundle mainBundle] pathForResource:page ofType:@"pdf"];
NSURL *targetURL = [NSURL fileURLWithPath:path];
NSURLRequest *request = [NSURLRequest requestWithURL:targetURL];


[self.webView loadRequest:request];

-(BOOL) webView:(UIWebView *)inWeb shouldStartLoadWithRequest:(NSURLRequest *)inRequest navigationType:(UIWebViewNavigationType)inType {
    if ( inType == UIWebViewNavigationTypeLinkClicked ) {
        [[UIApplication sharedApplication] openURL:[inRequest URL]];
        return NO;
    }

    return YES;
}

如您所见,我尝试插入代码来启动它,但单步执行时 BOOL 永远不会运行。

【问题讨论】:

  • 你检查过其他导航类型吗?
  • 是的,我试过了。问题是我无法单击 pdf 中的链接。当我使用 html 文件而不是 pdf 时,此代码工作正常。

标签: ios objective-c iphone uiwebview


【解决方案1】:

使用这个:-

-(BOOL) webView:(UIWebView *)inWeb shouldStartLoadWithRequest:(NSURLRequest *)inRequest navigationType:(UIWebViewNavigationType)inType {
if ( inType == UIWebViewNavigationTypeLinkClicked || inType ==UIWebViewNavigationTypeOther) {
    [[UIApplication sharedApplication] openURL:[inRequest URL]];
    return NO;
}

return YES;
}

UIWebViewNavigationTypeLinkClicked 在用户点击样式链接时发生,如果更改是从 Javascript 中完成的(例如 onclick 事件),则使用 UIWebViewNavigationTypeOther。

此外,如果您首先加载页面,则使用 UIWebViewNavigationTypeOther,否则您将被元刷新或其他东西重定向

【讨论】:

  • 此代码不起作用。我也尝试设置检测器类型,但无法单击 pdf 中的超链接。 [self.webView setDataDetectorTypes:UIDataDetectorTypeLink];
猜你喜欢
  • 1970-01-01
  • 2014-03-18
  • 1970-01-01
  • 1970-01-01
  • 2014-09-09
  • 1970-01-01
  • 2016-01-20
  • 2013-10-14
  • 1970-01-01
相关资源
最近更新 更多