拦截webView 请求URL,修改webView内容icon

2018-12-09  本文已影响26人  Eddiegooo

修改webView里的某个图片

当我们使用一个webView 加载出一个界面,但是webView里面某个图片icon我们不想要,想要去掉它或是修改它。

方法

新建一个类,继承NSURLProtocol
@interface QLWebURLProtocol : NSURLProtocol

实现协议方法:

@implementation QLWebURLProtocol
//方法一: 
+ (BOOL)canInitWithRequest:(NSURLRequest *)request {
    /*防止无限循环,因为一个请求在被拦截处理过程中,也会发起一个请求,这样又会走到这里,如果不进行处理,就会造成无限循环*/
//    if ([NSURLProtocol propertyForKey:protocolKey inRequest:request]) {
//        return NO;
//    }
    NSString * url = request.URL.absoluteString;
    NSLog(@"%@",url); //这里可以获取到加载webView所有的加载链接
    //如果你想拦截哪个加载的链接,就返回YES  默认NO
    if ([request.URL.absoluteString isEqualToString:@"https://www.baidu.com"]) {
        return YES;
    }else {
        return NO;
    }


    //判断要加载的资源本地是否存在
//    if ([QLWebURLProtocol localResourceIsExistWith:request]) {
//        return YES;
//    } else {
//        return NO;
//    }
//    return NO;

}
//方法二
+ (NSURLRequest*)canonicalRequestForRequest:(NSURLRequest*)theRequest
{
    return theRequest;
}
//方法三:
- (void)startLoading
{
        //拿到你拦截的URL,做你想做的事。 可能禁止这个url,也可以替换成其他的URL。。 一般都是加载本地图片来更换它自有图片资源

//例如拦截图片加载,用本地图片替换,配合自己写的私有方法

//    NSArray *myUrlArr = [[self class] myUrls];
//    NSInteger index = [myUrlArr indexOfObject:self.request.URL.absoluteString];
//    NSString *imageName = [[[self class] myImageName] objectAtIndex:index];
//    
//    NSURLResponse *response = [[NSURLResponse alloc] initWithURL:[self.request URL]
//                                                        MIMEType:@"image/png"
//                                           expectedContentLength:-1
//                                                textEncodingName:nil];
//    NSArray *com = [imageName componentsSeparatedByString:@"."];
//    NSString *imagePath = [[NSBundle mainBundle] pathForResource:com[0] ofType:com[1]];
//    NSData *data = [NSData dataWithContentsOfFile:imagePath];
//    
//    [[self client] URLProtocol:self didReceiveResponse:response cacheStoragePolicy:NSURLCacheStorageNotAllowed];
//    [[self client] URLProtocol:self didLoadData:data];
//    [[self client] URLProtocolDidFinishLoading:self];

}
//方法四:
- (void)stopLoading
{
    NSLog(@"something went wrong!");
}

//私有方法,判断是否有需要替换的东西

+ (BOOL)localResourceIsExistWith:(NSURLRequest *)request
{
    NSArray *arr = [self myUrls];
    if ([arr containsObject:request.URL.absoluteString]) {
        return YES;
    }
    return NO;
}

//要替换的原webView 链接
+ (NSArray*)myUrls
{
    return @[@"https://uidesign.gbtcdn.com/check_out/paypal.png?impolicy==true",
             @"https://uidesign.gbtcdn.com/check_out/money.png?impolicy=hight",
             @"https://icss1.gearbest.com/imagecache/GB2/images/domeimg/pay_method/spp1.jpg",
             @"https://uidesign.gbtcdn.com/check_out/poli.png?impolicy=high",
             @"https://cashier.gearbest.com/static/img/mcredit.png"];
}
//我自己本地的图片
+ (NSArray*)myImageName
{
    return @[@"paypal.png",
             @"money.png",
             @"spp1.jpg",
             @"poli.png",
             @"mcredit.png"];
}

使用方法

在你使用webView 的方法类初始化中注册,dealloc方法里取消注册即可。

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    //注册
    [NSURLProtocol registerClass:[QLWebURLProtocol class]];

    [self.view addSubview:self.webView];
//用加载百度为例子
    [self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"https://www.baidu.com"]]];
}
百度.png

这样就能得到加载百度所有的请求:


加载的url.png

以去掉百度logo为例: 我们可以找到百度logo图片链接

    if ([request.URL.absoluteString isEqualToString:@"https://m.baidu.com/static/index/plus/plus_logo.png"]) {
        return YES;
    }else {
        return NO;
    }

startLoading 方法不做任何处理,运行看去掉了百度logo。


去掉logo后的百度.png
//dealloc 方法取消注册
- (void)dealloc {
    [NSURLProtocol unregisterClass:[QLWebURLProtocol class]];
}

以上的方法可以用来修改webView里某一个图片。当然你也可以拦截到指定的URL,做你想做的其他事。
WKWebView 貌似不适用。目前我还没有实现。还请大神指导。

iOS 12系统之后,UIWebView 已经被废弃了,现在写webVIew都会报警告了。所以这个没有啥太大意义了,毕竟不怎么使用WebVIew了。(微笑🙂)

上一篇 下一篇

猜你喜欢

热点阅读