如何为UIWebview做广告拦截?

时间:2010-08-10 06:04:22

标签: iphone ipad uiwebview adblock

虽然苹果禁止使用闪存,因此禁用了很多广告。我仍然喜欢浏览器功能的广告块。

我注意到adblock是通过检查所有加载请求的网址来实现的。 UIWebview可以实现吗?

任何建议都很好 谢谢

1 个答案:

答案 0 :(得分:1)

没有

但是,您可以swizzle -[NSURLRequest initWithURL:cachePolicy:timeoutInterval:]阻止从一开始就发出请求,例如:

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);

请注意,返回nil一般不安全。请求可能存储在某些数据结构中,程序将崩溃。