2010-08-10 29 views
0

儘管蘋果禁止了禁止大量廣告的Flash。我仍然喜歡瀏覽器的功能。如何爲UIWebview做一個adblock?

我注意到adblock是通過檢查所有加載請求的url來實現的。用UIWebview可以嗎?

任何建議都很好前來 感謝

回答

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一般來說不安全。有可能請求將被存儲在某些數據結構中,並且程序會崩潰。