【问题标题】:How to use shouldStartLoadWithRequest method of UIWebView Delegate如何使用 UIWebView Delegate 的 shouldStartLoadWithRequest 方法
【发布时间】:2013-03-04 03:31:08
【问题描述】:

我需要从 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>

【问题讨论】:

    标签: ios objective-c uiwebview uiwebviewdelegate


    【解决方案1】:

    如果您想在第一个页面加载后禁用所有链接,您可以添加一个属性来存储页面是否已加载,并在 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
    };
    

    【讨论】:

    • 当我打印出 request.URL 时,我得到这个:about:blank?为什么会这样?
    • 我认为这是因为您正在加载 HTML 文件而不是实际页面。
    • 它不起作用吗?该 CSS 规则应删除链接。我提供的代码只是阻止 UIWebView 加载其他请求。
    • 你是什么意思?什么 CSS 规则?
    • stringByEvaluatingJavaScriptFromString 里面的那个: [webUI stringByEvaluatingJavaScriptFromString:@"document.styleSheets[0].addRule(\".active\", \"pointer-events: none;\");document.styleSheets [0].addRule(\".active\", \"cursor: default;\")"];
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-01-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-21
    • 1970-01-01
    相关资源
    最近更新 更多