Framework文件
- FireflyWebPluginService.framework
配置鏈接
- 需要(yào / yāo)添加
FireflyWebPluginService.framework
的(de)鏈接,以(yǐ)及對應的(de)依賴鏈接
頭文件引用
簡單導入
#import <FireflyWebPluginService/FireflyWebPluginService.h>
原理
注冊NSURLProtocol的(de)子(zǐ)類,以(yǐ)攔截UIWebView的(de)網絡請求,對符合規則的(de)請求進行重定向,将網絡IO定向到(dào)本地(dì / de)IO。
受限于(yú)蘋果應用商店政策原因,離線包不(bù)支持WKWebView。
使用步驟
1、注冊FireflySessionURLProtocol,建議在(zài)加載WebView之(zhī)前前注冊
[NSURLProtocol registerClass:[FireflySessionURLProtocol class]];
2、注冊本地(dì / de)化過濾器
[FireflySessionURLProtocol filter_register:[FireflySessionURLCustomFilter class]];
3、覆寫FireflySessionURLCustomFilter的(de)方法
//FireflySessionURLCustomFilter子(zǐ)類
+(BOOL)ff_canDoWithRequest:(NSURLRequest *_Nonnull)inRequest canonicalRequest:(NSURLRequest *_Nonnull*_Nullable)outRequest outUserData:(NSObject *_Nullable*_Nonnull)outUserData
{
//下面代碼示意過濾判斷
if (!inRequest || !inRequest.URL)
return NO;
NSString *scheme = [inRequest.URL.scheme lowercaseString];
if (![scheme isEqualToString:@"http"] && ![scheme isEqualToString:@"https"])
return NO;
NSString *userAgent = [inRequest valueForHTTPHeaderField:@"User-Agent"];
if ([userAgent rangeOfString:@"AppleWebKit"].location == NSNotFound)
return NO;
//隻有結果爲(wéi / wèi)YES時(shí),才會有(但不(bù)一(yī / yì /yí)定有)後續的(de)處理過程
return YES;
}
-(NSString *_Nonnull)localFilePath
{
return @"本地(dì / de)文件";
}