4

我需要从 UIWebView 中显示的 URL 中删除超链接,并且我已经查看了这个问题:Removing hyper links from a URL shown in UIWebView

我知道我需要使用这种方法:

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

但我似乎仍然有一些问题。

首先,我如何只避免某些链接(例如:www.google.com)。

接下来,如何避免 UIWebView 中的所有链接?

我的代码如下所示:

[webUI loadHTMLString:[strDescription stringByDecodingHTMLEntities] baseURL:nil];
webUI.dataDetectorTypes = UIDataDetectorTypeNone;

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

    [webUI stringByEvaluatingJavaScriptFromString:@"document.styleSheets[0].addRule(\".active\", \"pointer-events: none;\");document.styleSheets[0].addRule(\".active\", \"cursor: default;\")"];

}

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
       return YES;
}

需要一些指导。谢谢..

HTML 字符串如下所示:

> <div style="font-family: Helvetica"><div style="color: #FFFFFF;"><div
> style="font-family: Helvetica;"><p><span style="font-size:
> 24px;"><strong>Optimal Performance Always</strong></span><span
> style="font-size: 18px;"><br /></span></p><p><span style="font-size:
> 18px;">The standard servicing package<a
> href="http://www.google.com">www.google.com</a></div>
4

1 回答 1

3

如果您想在第一页加载后禁用所有链接,您可以添加一个属性来存储页面是否已加载,并在 webView:shouldStartLoadWithRequest 上使用其值:

@property(nonatomic) BOOL pageLoaded; // initially NO

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

    [webUI stringByEvaluatingJavaScriptFromString:@"document.styleSheets[0].addRule(\".active\", \"pointer-events: none;\");document.styleSheets[0].addRule(\".active\", \"cursor: default;\")"];

    // after all your stuff
    self.pageLoaded = YES;
}

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
   return ! self.pageLoaded;
}

请注意,这不会隐藏链接,它只会使 webview 不加载它们。

此外,您可以检查 webView:shouldStartLoadWithRequest:navigationType: 上的 request.URL 以仅加载某些页面。另一种方法是检查 navigationType 值:

enum {
   UIWebViewNavigationTypeLinkClicked,
   UIWebViewNavigationTypeFormSubmitted,
   UIWebViewNavigationTypeBackForward,
   UIWebViewNavigationTypeReload,
   UIWebViewNavigationTypeFormResubmitted,
   UIWebViewNavigationTypeOther
};
于 2013-03-04T03:51:01.057 回答