WVJSB 1.0.1

WVJSB 1.0.1

retriable 维护。



WVJSB 1.0.1

  • retriable

WVJSB

License MIT Build Status Carthage compatible Pod Version Pod Platform

跨iframe WebView JavaScript Bridge。

安装

本地

Cocoapods

将以下内容添加到您项目的 Podfile 中

pod 'WVJSB'

Carthage

将以下内容添加到您项目的 Cartfile 中

github "retriable/WVJSB"

Web

将以下 JavaScript 添加到您的web项目中。

本地使用

创建服务器

服务器自动与网页视图关联。

    WVJSBServer *server=[WVJSBServer serverWithWebView:webView namespace:@"server namespace"];

处理无响应请求

[[server on:@"method"] onEvent:^id(WVJSBConnection * connection, id parameter, WVJSBAckBlock (^done)(void)) {
    done();
    return nil;
}];

处理响应式请求

[[server on:@"request"] onEvent:^id(WVJSBConnection * connection, id parameter, WVJSBAckBlock (^done)(void)) {
    done()(@"response object",nil);
    return nil;
}];

处理可取消请求

[[[server on:@"request"] onEvent:^id (WVJSBConnection * connection, id parameter, WVJSBAckBlock (^done)(void)) {
    //Simulate asynchronous request
    dispatch_source_t timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, dispatch_get_main_queue());
    dispatch_source_set_timer(timer, dispatch_time(DISPATCH_TIME_NOW, NSEC_PER_SEC*2), DBL_MAX, 0 * NSEC_PER_SEC);
    dispatch_source_set_event_handler(timer, ^{
        done()(@"response object",nil);
    });
    dispatch_resume(timer);
    //Return the timer as context 
    return timer;
}] onCancel:^(id  context) {
    dispatch_source_t timer = context;
    //Cancel timer 
    dispatch_source_cancel(timer);
}];

请求 JavaScript 客户端

WVJSBConnection *connection =  server.connections.allValues.lastObject;
WVJSBOperation *operation = [[[connection event:@"request" parameter:nil] onAck:^(WVJSBOperation *operation,id result, NSError *error) {
    //Do something with result
}] timeout:30];

取消请求

[operation cancel];

JavaScript 使用方法

创建客户端

const client = WVJSBClient('server namespace',{"key":"value"});

处理响应请求

client.on('request').onEvent(function(parameter,done){
    done()('response object',null);
})

处理可取消请求

client.on('request').onEvent(function(parameter,done){
    const context = window.setTimeout(function(){
        done()('response object',null);
    },2000);
    return context;
}).onCancel(function(context){
    window.clearTimeout(context);
});

请求本地服务器

const operation = client.event('request',null).onAck(function(operation,parameter,error){

}).timeout(30000);

取消请求

operation.cancel();

运行示例

打开终端,并执行以下命令

cd pathToProject/WVJSB/Resources/www
python -m SimpleHTTPServer

然后打开项目并运行