iOS webView 遠端h5優先載入本地資源

2021-07-29 18:12:39 字數 2013 閱讀 7445

前言:uiwebview呼叫遠端h5頁面,優先載入本地、js、css等資源,解決辦法就是對請求進行攔截。

服務端**放在本文後面

客戶端需要對nsurlprotocol 的自定義類進行註冊,那麼所有的webview 對http請求都會被他攔截到;

首先自定義nsurlprotocol類

#import #import #import @inte***ce nsurlprotocolcustom : nsurlprotocol

@end

#import "nsurlprotocolcustom.h"

static nsstring* const filteredkey = @"filteredkey";

@implementation nsurlprotocolcustom

+ (bool)caninitwithrequest:(nsurlrequest *)request

] != nsnotfound;

return [nsurlprotocol propertyforkey:filteredkey inrequest:request] == nil && issource;

}+ (nsurlrequest *)canonicalrequestforrequest:(nsurlrequest *)request

- (void)startloading

//根據路徑獲取mimetype

cfstringref pathextension = (__bridge_retained cfstringref)[path pathextension];

cfstringref type = uttypecreatepreferredidentifierfortag(kuttagclassfilenameextension, pathextension, null);

cfrelease(pathextension);

//the uti can be converted to a mime type:

nsstring *mimetype = (__bridge_transfer nsstring *)uttypecopypreferredtagwithclass(type, kuttagclassmimetype);

if (type != null)

cfrelease(type);

//載入本地資源

nsdata *data = [nsdata datawithcontentsoffile:path];

[self sendresponsewithdata:data mimetype:mimetype];

}- (void)stoploading

- (void)sendresponsewithdata:(nsdata *)data mimetype:(nullable nsstring *)mimetype

@end

其次實現對類的註冊

#import "cwwebview.h"

#import "nsurlprotocolcustom.h"

@inte***ce cwwebview ()@property (nonatomic,strong) uiwebview *mywebview;

@end

@implementation cwwebview

- (void)viewdidload

- (uiwebview *)mywebview

return _mywebview;

}@end

服務端**

本地js檔案

function callios(msg) ;

}function calljs() ;

}function calljsmsg(msg) ;

}function jsfunc()

iOS webview與H5的互動

元件採用私有 cocoapods 來實現,並採用了 local pods 的方式,可以在本地不提交 的情況下,元件與呼叫方實現除錯。元件間的頁面間跳轉支援 openurl 的方式,由 oneroute 模組進行管理,頁面在 void load 方法中完成註冊,oneroute 內部儲存乙份 url ...

chrome inspect 遠端除錯H5

chrome inspect devices 乙個內置於chrome的遠端除錯指令,滿足遠端除錯的幾個必須條件 2,手機開啟usb除錯模式 以上條件為必須的 附上連線 1.很多手機的預設開發者許可權是關閉的,此時請多次點選版本號,就會彈出連線開發者的彈窗 2.連線效果圖 3.連線成功開啟chrome...

h5應用資料加密 H5定製開發方案

正文共計 815字預計 產品特性 快速迭代 h5常用於輕量級應用,例如 活動營銷頁面 拉新等,而這些相對開發成本比較低,也可滿足市場需求進行快速迭代。專屬方案 所想即所得,為客戶量身定製策劃方案 多種定製 提供活動營銷 商業 遊戲有效等多種定製 全新互動 互動性強,視覺效果佳,使用者的感官體驗好,可...