简体   繁体   English

如何使用UIWebView Delegate的shouldStartLoadWithRequest方法

[英]How to use shouldStartLoadWithRequest method of UIWebView Delegate

I need to remove the hyperlinks from the URL shown in UIWebView and I have looked at this question: Removing hyper links from a URL shown in UIWebView . 我需要从UIWebView中显示的URL中删除超链接 ,我已经看过这个问题: 从UIWebView中显示的URL中删除超链接

I know I need to use this method: 我知道我需要使用这个方法:

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

But I still seem to have some problems. 但我似乎仍有一些问题。

Firstly, how do i avoid only certain links (for example: www.google.com). 首先,我如何避免某些链接(例如:www.google.com)。

Next, how do i avoid all links in my UIWebView? 接下来,我如何避免UIWebView中的所有链接?

My code looks like this: 我的代码看起来像这样:

[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;
}

Need some guidance. 需要一些指导。 Thanks.. 谢谢..

The HTML string looks like this: 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>

If you want to disable all links after the first page was loaded you can add a property to store if the page was loaded and use its value on webView:shouldStartLoadWithRequest: 如果要在加载第一个页面后禁用所有链接,则可以在页面加载时添加属性以存储并在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;
}

Note that this doesn't hide links, it only makes the webview not load them. 请注意,这不会隐藏链接,它只会使webview无法加载它们。

Also, you can check request.URL on webView:shouldStartLoadWithRequest:navigationType: to only load certain pages. 此外,您可以在webView上检查request.URL:shouldStartLoadWithRequest:navigationType:仅加载某些页面。 Another way would be check navigationType value: 另一种方法是检查navigationType值:

enum {
   UIWebViewNavigationTypeLinkClicked,
   UIWebViewNavigationTypeFormSubmitted,
   UIWebViewNavigationTypeBackForward,
   UIWebViewNavigationTypeReload,
   UIWebViewNavigationTypeFormResubmitted,
   UIWebViewNavigationTypeOther
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM