iOS使用
構(gòu)建聲波框架 將Sonic.framework添加到主項目中框架里面,sdk。
進(jìn)行下面的代碼進(jìn)行初始化.
[NSURLProtocol registerClass:[SonicURLProtocol class]];
@interface SonicWebViewController : UIViewController
?SonicSessionDelegate代理
#pragma mark - Sonic Session Delegate
* 調(diào)用回來時發(fā)送請求。
- (void)sessionWillRequest:(SonicSession *)session;
*當(dāng)Sonic要求WebView重新加載時回調(diào),例如模板更改或發(fā)生錯誤。
- (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初始化后立即發(fā)送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回調(diào)與網(wǎng)站交互。
- (void)getDiffData:(NSDictionary *)option withCallBack:(JSValue *)jscallback
{
* ViewController發(fā)送Sonic請求并通過回調(diào)返回結(jié)果。
[[SonicClient sharedClient] sonicUpdateDiffDataByWebDelegate:self.owner completion:^(NSDictionary *result) {
* 返回結(jié)果。
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....