开发者

how to do an adblock for UIWebview?

开发者 https://www.devze.com 2023-01-11 08:58 出处:网络
Although apple banned flash which disabled a lot of ad. I still like a adblock like functionality for browser.

Although apple banned flash which disabled a lot of ad. I still like a adblock like functionality for browser.

I noticed that adblock do so by checking all load request's url. Is that possible with UIWebview?

开发者_StackOverflow社区Any suggestions are well come Thanks


No.

However, you can swizzle -[NSURLRequest initWithURL:cachePolicy:timeoutInterval:] to prevent the request be issued from the start, e.g.:

static id (*oldMethod)(id self, SEL _cmd, NSURL* theURL, ....);

static id newMethod(id self, SEL _cmd, NSURL* theURL, ....) {
    if ([[theURL absoluteString] hasPrefix:@"http://example.com"]) {
        [self release];
        return nil;
    }
    return oldMethod(self, _cmd, theURL, cachePolicy, timeoutInterval);
}

....


Method m = class_getInstanceMethod([NSURLRequest class], 
                                  @selector(initWithURL:cachePolicy:timeoutInterval:));
oldMethod = method_setImplementation(m, newMethod);

Note that returning nil is not safe in general. It is possible that a request will be stored in some data structure and the program will crash.

0

精彩评论

暂无评论...
验证码 换一张
取 消