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阅读 9,166评论 2 7
  • iOS 的 Cookie 存取 https://juejin.im/entry/58d4c4cc44d904006...
    Farmers阅读 11,269评论 0 16
  • 一、简介 近两年随着HTML5的迅速发展与日趋成熟,越来越多的移动开发者选择使用HTML5来进行混合开发,不...
    宝宝teacher阅读 6,906评论 3 15
  • 导语 WKWebView 是苹果在 WWDC 2014 上推出的新一代 webView 组件,用以替代 UIKit...
    hope7th阅读 15,036评论 4 65
  • 青衫,是一种心境,是一种状态,更是一种理想。 注册简书时,叫什么名字,一时把我难住了。霸气的,太幼稚。幼稚的,太可...
    文墨青衫阅读 5,266评论 0 51