VasSonic功能特性-提升H5界面加載速度,本地緩存.

iOS使用

構建聲波框架 將Sonic.framework添加到主項目中框架里面,sdk。

進行下面的代碼進行初始化.

[NSURLProtocol registerClass:[SonicURLProtocol class]];

@interface SonicWebViewController : UIViewController

?SonicSessionDelegate代理

#pragma mark - Sonic Session Delegate

* 調用回來時發送請求。

- (void)sessionWillRequest:(SonicSession *)session;

*當Sonic要求WebView重新加載時回調,例如模板更改或發生錯誤。

- (void)session:(SonicSession *)session requireWebViewReload:(NSURLRequest *)request

{

[self.webView loadRequest:request];

}

步驟3:在WebView ViewController中使用Sonic

- (instancetype)initWithUrl:(NSString *)aUrl

{

if (self = [super init]) {

self.url = aUrl;

[[SonicClient sharedClient] createSessionWithUrl:self.url withWebDelegate:self];

}

return self;

}

* 在WebView初始化后立即發送Sonic屬性的請求。

- (void)loadView

{

[super loadView];

self.webView = [[UIWebView alloc]initWithFrame:self.view.bounds];

self.webView.delegate = self;

self.view = self.webView;

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:self.url]];

* SonicSession不為空.

if ([[SonicClient sharedClient] sessionWithWebDelegate:self]) {

[self.webView loadRequest:sonicWebRequest(request)];

}else{

[self.webView loadRequest:request];

}

}

通過JavaScript回調與網站交互。

- (void)getDiffData:(NSDictionary *)option withCallBack:(JSValue *)jscallback

{

* ViewController發送Sonic請求并通過回調返回結果。

[[SonicClient sharedClient] sonicUpdateDiffDataByWebDelegate:self.owner completion:^(NSDictionary *result) {

* 返回結果。

NSData *json = [NSJSONSerialization dataWithJSONObject:result options:NSJSONWritingPrettyPrinted error:nil];

NSString *jsonStr = [[NSString alloc]initWithData:json encoding:NSUTF8StringEncoding];

JSValue *callback = self.owner.jscontext.globalObject;

[callback invokeMethod:@"getDiffDataCallback" withArguments:@[jsonStr]];

}];

}

- (void)dealloc

{

[[SonicClient sharedClient ] removeSessionWithWebDelegate:self ];

}

下載最新的Demo ----?https://github.com/Tencent/VasSonic ? 里面包含了安卓,iOS....

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • iOS開發系列--網絡開發 概覽 大部分應用程序都或多或少會牽扯到網絡開發,例如說新浪微博、微信等,這些應用本身可...
    lichengjin閱讀 3,721評論 2 7
  • iOS 的 Cookie 存取 https://juejin.im/entry/58d4c4cc44d904006...
    Farmers閱讀 6,019評論 0 16
  • 一、簡介 近兩年隨著HTML5的迅速發展與日趨成熟,越來越多的移動開發者選擇使用HTML5來進行混合開發,不...
    寶寶teacher閱讀 2,372評論 3 15
  • 導語 WKWebView 是蘋果在 WWDC 2014 上推出的新一代 webView 組件,用以替代 UIKit...
    hope7th閱讀 15,011評論 4 65
  • 青衫,是一種心境,是一種狀態,更是一種理想。 注冊簡書時,叫什么名字,一時把我難住了。霸氣的,太幼稚。幼稚的,太可...
    文墨青衫閱讀 1,428評論 0 51