iOS WKWebview實(shí)現(xiàn)攔截js,css蛋辈,html以及圖片資源替換為本地資源的兩種方式(NSUrlProtocol)


NSURLProtocol簡(jiǎn)介

NSURLProtocol是URL Loading System的重要組成部分属拾。它是一個(gè)抽象類将谊。可以攔截網(wǎng)絡(luò)請(qǐng)求渐白∽鹋ǎ可以攔截的網(wǎng)絡(luò)請(qǐng)求包括NSURLSession,NSURLConnection以及UIWebvIew〈垦埽現(xiàn)也支持WKWebview框架栋齿,本文就是采用WKWebview。


方式

攔截做替換的方式有兩種

  • 重定向到本地資源襟诸,利用canonicalRequestForRequest實(shí)現(xiàn)
  • 根據(jù)url瓦堵,自定義response。

下面先說第一種實(shí)現(xiàn)方式:

1歌亲、無論哪種實(shí)現(xiàn)方式都必須注冊(cè)NSURLProtocol

我這里在AppDelegate的didFinishLaunchingWithOptions方法里面進(jìn)行注冊(cè)谷丸,代碼如下

- (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions {

    //注冊(cè)
    [NSURLProtocol registerClass:[BaseOnRedirectRequestURLProtocol class]];

    //實(shí)現(xiàn)攔截功能

    Class cls = NSClassFromString(@"WKBrowsingContextController");

    SEL sel = NSSelectorFromString(@"registerSchemeForCustomProtocol:");

    if([(id)clsrespondsToSelector:sel]) {
#pragma clang diagnostic push

#pragma clang diagnostic ignored"-Warc-performSelector-leaks"

        [(id)cls performSelector:sel withObject:@"myapp"];

#pragma clang diagnostic pop

    }

    return YES;

}
2、創(chuàng)建NSURLProtocol子類BaseOnRedirectRequestURLProtocol
  • 在.m文件中 遵循協(xié)議<NSURLSessionDelegate>
  • 創(chuàng)建 NSURLSessionDataTask 對(duì)象
    ? ? ? ?@property (nonnull,strong) NSURLSessionDataTask *task;
  • 下面是NSURLSession代理方法
- (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveResponse:(NSURLResponse *)response completionHandler:(void (^)(NSURLSessionResponseDisposition))completionHandler {
    [[self client] URLProtocol:self didReceiveResponse:response cacheStoragePolicy:NSURLCacheStorageAllowed];
    
    completionHandler(NSURLSessionResponseAllow);
}

- (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveData:(NSData *)data {
    [[self client] URLProtocol:self didLoadData:data];
}

- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(nullable NSError *)error {
    [self.client URLProtocolDidFinishLoading:self];
}
3应结、準(zhǔn)備就緒后刨疼,下面開始實(shí)現(xiàn)攔截功能
  • 注冊(cè)NSURLProtocol后,客戶端所有請(qǐng)求都將走+ (BOOL)canInitWithRequest:(NSURLRequest *)request鹅龄。
    下面是代碼片段:
+ (BOOL)canInitWithRequest:(NSURLRequest *)request
{
    NSLog(@"request.URL.absoluteString = %@",request.URL.absoluteString);
    NSString *scheme = [[request URL] scheme];
    if ( ([scheme caseInsensitiveCompare:@"http"]  == NSOrderedSame ||
          [scheme caseInsensitiveCompare:@"https"] == NSOrderedSame ))
    {
        //看看是否已經(jīng)處理過了揩慕,防止無限循環(huán)
        if ([NSURLProtocol propertyForKey:BaseOnRedirectRequestURLProtocolKey inRequest:request])
            return NO;
        return YES;
    }
    return NO;
}
  • request 重定向方法 下面代碼中的地址,改為你需要攔截的地址扮休,本地路徑也改為你需要替換的本地資源
+ (NSURLRequest *)canonicalRequestForRequest:(NSURLRequest *)request
{
    NSMutableURLRequest *mutableReqeust = [request mutableCopy];
    
    NSLog(@"url ---[%@]\n\n",request.URL.absoluteString);
    
    //request截取重定向
    if ([request.URL.absoluteString isEqualToString:@"http://xxx/xxx.js"])
    {
        NSString * filePath = [[NSBundle mainBundle] pathForResource:@"NewProject" ofType:@"js"];

        NSURL * url = [NSURL fileURLWithPath:filePath];
        mutableReqeust.URL = url;
        
        
    }
    return mutableReqeust;
}
  • 在startLoading中開始加載
- (void)startLoading{
    
    NSMutableURLRequest* request = self.request.mutableCopy;
    [NSURLProtocol setProperty:@YES forKey:BaseOnRedirectRequestURLProtocolKey inRequest:request];
    NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration] delegate:self delegateQueue:nil];
    self.task = [session dataTaskWithRequest:self.request];
    [self.task resume];
    
}
  • stopLoading中記得銷毀
- (void)stopLoading
{
    if (self.task != nil) {
        [self.task  cancel];
    }
}

至此第一種攔截替換本地資源的方式已實(shí)現(xiàn)迎卤,控制器加載WKWebView在本文末尾寫

下面先說第二種實(shí)現(xiàn)方式:

1、首先同第一種方式玷坠,都需要注冊(cè)
- (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions {

    //注冊(cè)
    [NSURLProtocol registerClass:[BaseOnResponseURLProtocol class]];

    //實(shí)現(xiàn)攔截功能

    Class cls = NSClassFromString(@"WKBrowsingContextController");

    SEL sel = NSSelectorFromString(@"registerSchemeForCustomProtocol:");

    if([(id)clsrespondsToSelector:sel]) {
#pragma clang diagnostic push

#pragma clang diagnostic ignored"-Warc-performSelector-leaks"

        [(id)cls performSelector:sel withObject:@"myapp"];

#pragma clang diagnostic pop

    }

    return YES;

}
2蜗搔、創(chuàng)建NSURLProtocol子類BaseOnResponseURLProtocol
  • 在.m文件中 遵循協(xié)議<NSURLConnectionDelegate>
  • 創(chuàng)建 NSURLConnection 對(duì)象
    ? ? ? ?@property (nonatomic, strong) NSURLConnection *connection;
  • 下面是NSURLConnection代理方法
#pragma mark - NSURLConnectionDelegate

- (void) connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
    [self.client URLProtocol:self didReceiveResponse:response cacheStoragePolicy:NSURLCacheStorageNotAllowed];
}

- (void) connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
    [self.client URLProtocol:self didLoadData:data];
}

- (void) connectionDidFinishLoading:(NSURLConnection *)connection {
    [self.client URLProtocolDidFinishLoading:self];
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
    [self.client URLProtocol:self didFailWithError:error];
}
3、準(zhǔn)備就緒后八堡,下面開始實(shí)現(xiàn)攔截功能
  • 注冊(cè)NSURLProtocol后樟凄,客戶端所有請(qǐng)求都將走+ (BOOL)canInitWithRequest:(NSURLRequest *)request。
+ (BOOL)canInitWithRequest:(NSURLRequest *)request
{
    NSLog(@"request.URL.absoluteString = %@",request.URL.absoluteString);

    if ( ([request.URL.absoluteString hasPrefix:@"http://game.scool.online/cutcake1/"]))
    {
        //看看是否已經(jīng)處理過了兄渺,防止無限循環(huán)
        if ([NSURLProtocol propertyForKey:BaseOnResponseURLProtocolKey inRequest:request]) {
            return NO;
        }
        
        return YES;
    }
    return NO;
}
  • 在startLoading中加載缝龄,實(shí)現(xiàn)思路:將需要替換的地址重新拼接response返回,再響應(yīng)本地資源挂谍,代碼如下:
- (void)startLoading {
 
    
    //1.獲取資源文件路徑 ajkyq/index.html
    NSURL *url = [self request].URL;
   
    //2.讀取資源文件內(nèi)容
    NSString *path = [[NSBundle mainBundle] pathForResource:@"NewProject" ofType:@"js"];

    NSFileHandle *file = [NSFileHandle fileHandleForReadingAtPath:path];
    NSData *data = [file readDataToEndOfFile];
    [file closeFile];

    //3.拼接響應(yīng)Response
    NSInteger dataLength = data.length;
    NSString *mimeType = [self getMIMETypeWithCAPIAtFilePath:path];
    NSString *httpVersion = @"HTTP/1.1";
    NSHTTPURLResponse *response = nil;
    
    if (dataLength > 0) {
        response = [self jointResponseWithData:data dataLength:dataLength mimeType: mimeType requestUrl:url statusCode:200 httpVersion:httpVersion];
    } else {
        response = [self jointResponseWithData:[@"404" dataUsingEncoding:NSUTF8StringEncoding] dataLength:3 mimeType:mimeType requestUrl:url statusCode:404 httpVersion:httpVersion];
    }
    
    //4.響應(yīng)
    [[self client] URLProtocol:self didReceiveResponse:response cacheStoragePolicy:NSURLCacheStorageNotAllowed];
    [[self client] URLProtocol:self didLoadData:data];
    [[self client] URLProtocolDidFinishLoading:self];
    
    
}
  • jointResponseWithData方法實(shí)現(xiàn)如下:
#pragma mark - 拼接響應(yīng)Response
-(NSHTTPURLResponse *)jointResponseWithData:(NSData *)data dataLength:(NSInteger)dataLength mimeType:(NSString *)mimeType requestUrl:(NSURL *)requestUrl statusCode:(NSInteger)statusCode httpVersion:(NSString *)httpVersion
{
    NSDictionary *dict = @{@"Content-type":mimeType,
                           @"Content-length":[NSString stringWithFormat:@"%ld",dataLength]};
    NSHTTPURLResponse *response = [[NSHTTPURLResponse alloc] initWithURL:requestUrl statusCode:statusCode HTTPVersion:httpVersion headerFields:dict];
    return response;
}
  • MIMEType類型獲取如下
#pragma mark - 獲取mimeType
-(NSString *)getMIMETypeWithCAPIAtFilePath:(NSString *)path
{
    if (![[[NSFileManager alloc] init] fileExistsAtPath:path]) {
        return @"text/html";
    }
    
    CFStringRef UTI = UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, (__bridge CFStringRef)[path pathExtension], NULL);
    CFStringRef MIMEType = UTTypeCopyPreferredTagWithClass (UTI, kUTTagClassMIMEType);
    CFRelease(UTI);
    if (!MIMEType) {
        return @"application/octet-stream";
    }
    return (__bridge NSString *)(MIMEType);
}

至此第二種實(shí)現(xiàn)方式也已完成叔壤,下面貼上控制器中的代碼

1、引用#import <WebKit/WebKit.h> 和 #import "NSURLProtocol+WKWebVIew.h" 并遵循<WKNavigationDelegate,WKUIDelegate>

NSURLProtocol+WKWebVIew 是對(duì)NSURLProtocol的一個(gè)擴(kuò)展口叙,主要是注冊(cè)http和https協(xié)議炼绘,本文不做介紹,demo里會(huì)有的妄田。

2俺亮、聲明WKWebView對(duì)象

? ? ? ?@property (nonatomic) WKWebView* webView;

3仗哨、viewDidLoad中代碼
- (void)viewDidLoad {
    [super viewDidLoad];
    [NSURLProtocol wk_registerScheme:@"http"];
    [NSURLProtocol wk_registerScheme:@"https"];
    [self.view addSubview:self.webView];
}
4、WKWebView懶加載
- (WKWebView *)webView {
    if (!_webView) {
        WKWebViewConfiguration *configuration = [[WKWebViewConfiguration alloc] init];
        configuration.userContentController = [WKUserContentController new];

        WKPreferences *preferences = [WKPreferences new];
        preferences.javaScriptCanOpenWindowsAutomatically = YES;
        preferences.minimumFontSize = 30.0;
        configuration.preferences = preferences;

        _webView = [[WKWebView alloc] initWithFrame:self.view.frame configuration:configuration];
        _webView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
        if ([_webView respondsToSelector:@selector(setNavigationDelegate:)]) {
            [_webView setNavigationDelegate:self];
        }

//        if ([_webView respondsToSelector:@selector(setDelegate:)]) {
            [_webView setUIDelegate:self];
//        }
        NSURL *url = [NSURL URLWithString:@"http://game.scool.online/cutcake1/"];
        NSURLRequest *request = [NSURLRequest requestWithURL:url];
        [_webView loadRequest:request];

    }
    return _webView;
}
5铅辞、別忘記回調(diào)哦
- (void)webView:(WKWebView *)webView runJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(void))completionHandler {

    completionHandler();

}



攔截的兩種方式都實(shí)現(xiàn)啦厌漂,有幫到你么?第一次寫簡(jiǎn)書斟珊,希望給個(gè)小心心支持一下苇倡,如果能關(guān)注一下本仙女,那就確定你是好人無疑啦~

附上demo地址:https://github.com/yananSun/WKLoadResource.git

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末囤踩,一起剝皮案震驚了整個(gè)濱河市旨椒,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌堵漱,老刑警劉巖综慎,帶你破解...
    沈念sama閱讀 217,277評(píng)論 6 503
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異勤庐,居然都是意外死亡示惊,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,689評(píng)論 3 393
  • 文/潘曉璐 我一進(jìn)店門愉镰,熙熙樓的掌柜王于貴愁眉苦臉地迎上來米罚,“玉大人,你說我怎么就攤上這事丈探÷荚瘢” “怎么了?”我有些...
    開封第一講書人閱讀 163,624評(píng)論 0 353
  • 文/不壞的土叔 我叫張陵碗降,是天一觀的道長(zhǎng)隘竭。 經(jīng)常有香客問我,道長(zhǎng)讼渊,這世上最難降的妖魔是什么动看? 我笑而不...
    開封第一講書人閱讀 58,356評(píng)論 1 293
  • 正文 為了忘掉前任,我火速辦了婚禮精偿,結(jié)果婚禮上弧圆,老公的妹妹穿的比我還像新娘赋兵。我一直安慰自己笔咽,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,402評(píng)論 6 392
  • 文/花漫 我一把揭開白布霹期。 她就那樣靜靜地躺著叶组,像睡著了一般。 火紅的嫁衣襯著肌膚如雪历造。 梳的紋絲不亂的頭發(fā)上甩十,一...
    開封第一講書人閱讀 51,292評(píng)論 1 301
  • 那天船庇,我揣著相機(jī)與錄音,去河邊找鬼侣监。 笑死鸭轮,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的橄霉。 我是一名探鬼主播窃爷,決...
    沈念sama閱讀 40,135評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼姓蜂!你這毒婦竟也來了按厘?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 38,992評(píng)論 0 275
  • 序言:老撾萬榮一對(duì)情侶失蹤钱慢,失蹤者是張志新(化名)和其女友劉穎逮京,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體束莫,經(jīng)...
    沈念sama閱讀 45,429評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡懒棉,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,636評(píng)論 3 334
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了览绿。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片漓藕。...
    茶點(diǎn)故事閱讀 39,785評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖挟裂,靈堂內(nèi)的尸體忽然破棺而出享钞,到底是詐尸還是另有隱情,我是刑警寧澤诀蓉,帶...
    沈念sama閱讀 35,492評(píng)論 5 345
  • 正文 年R本政府宣布栗竖,位于F島的核電站,受9級(jí)特大地震影響渠啤,放射性物質(zhì)發(fā)生泄漏狐肢。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,092評(píng)論 3 328
  • 文/蒙蒙 一沥曹、第九天 我趴在偏房一處隱蔽的房頂上張望份名。 院中可真熱鬧,春花似錦妓美、人聲如沸僵腺。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,723評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)辰如。三九已至,卻和暖如春贵试,著一層夾襖步出監(jiān)牢的瞬間琉兜,已是汗流浹背凯正。 一陣腳步聲響...
    開封第一講書人閱讀 32,858評(píng)論 1 269
  • 我被黑心中介騙來泰國(guó)打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留豌蟋,地道東北人廊散。 一個(gè)月前我還...
    沈念sama閱讀 47,891評(píng)論 2 370
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像梧疲,于是被迫代替她去往敵國(guó)和親奸汇。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,713評(píng)論 2 354

推薦閱讀更多精彩內(nèi)容