Coder Social home page Coder Social logo

arcangelw / wzbridge Goto Github PK

View Code? Open in Web Editor NEW
3.0 3.0 0.0 33 KB

Enable adding Javascript Interface to WKWebView like Android WebView ,Inspired by EasyJSWebView an dsBridge

License: MIT License

Ruby 3.95% Objective-C 76.54% Swift 8.35% HTML 8.43% JavaScript 2.73%
bridge wkwebview dsbridge easyjswebview

wzbridge's Introduction

WZBridge

CI Status Version License Platform

Example

A simple javascript and native interaction bridge WKWebView

Inspired by EasyJSWebView an dsBridge

To run the example project, clone the repo, and run pod install from the Example directory first.

Usage

  1. Implement APIs in a class
@protocol WZObjCApiTestExport <WZJSExport>
@required
- (void)callSyn;
- (NSString *)callSyn:(NSString *)msg;
- (void)callAsyn:(NSString *)msg :(WJSCallFunction *)func;
- (void)callAsyn:(WJSCallFunction *)func;
@optional
- (void)callProgress:(WJSCallFunction *)func;
@end

@interface WZObjCApiTest : NSObject<WZObjCApiTestExport>
@end
@implementation WZObjCApiTest
{
    NSTimer *_timer;
    int _t;
    WJSCallFunction *_func;
}
#pragma mark - to implement protocol method
- (void)callAsyn:(nonnull WJSCallFunction *)func {
    NSLog(@"%s  Hello, I am js",__func__);
    [func executeWithParam:@"hellow js ,I am ObjC ,I received your message" completionHandler:^(NSString * _Nullable result, NSError * _Nullable error) {
        NSLog(@"%@",result);
    }];
}

- (void)callAsyn:(nonnull NSString *)msg :(nonnull WJSCallFunction *)func {
    NSLog(@"%s  Hello, I am js",__func__);
    [func executeWithParam:[NSString stringWithFormat:@"hellow js ,I am ObjC ,I received your message:%@",msg] completionHandler:^(NSString * _Nullable result, NSError * _Nullable error) {
        NSLog(@"%@",result);
    }];
}

- (void)callSyn {
    NSLog(@"%s  Hello, I am js",__func__);
}

- (nonnull NSString *)callSyn:(nonnull NSString *)msg {
    NSLog(@"%s  Hello, I am js",__func__);
    return [NSString stringWithFormat:@"hellow js ,I am ObjC ,I received your message:%@",msg];
}

- (void)callProgress:(WJSCallFunction *)func
{
    if (_func) {
        [_timer invalidate];
        _timer = nil;
        _func.removeAfterExecute = YES;
        [_func executeWithParam:@""];
    }
    _func = func;
    _func.removeAfterExecute = NO;
    _t = 10;
    [_func executeWithParam:@(_t).stringValue completionHandler:nil];
    _timer = [NSTimer scheduledTimerWithTimeInterval:1.0
                                              target:self
                                            selector:@selector(onTimer:)
                                            userInfo:nil
                                             repeats:YES];
}

- (void)onTimer:(NSTimer *)timer
{
    _t--;
    if (_t > 0) {
        [_func executeWithParam:@(_t).stringValue];
    }else{
        _func.removeAfterExecute = YES;
        [_func executeWithParam:@(0).stringValue];
        [_timer invalidate];
        _timer = nil;
    }
}
@end
  1. add API object to WWKWebView
WWKWebView *webView = [WWKWebView new];
[webView addJavascriptObject:[WZObjCApiTest new] namespace:@"toObjC"];
/// custom dialogText
[webView setDialogText:@"启禀陛下:" forKey:WWKWebViewDialogKeyAlertTitle];
/// monitor title or progress 
[webView addObserverForType:WWObserverTypeProgress block:^(id  _Nonnull value) {
        NSLog(@"block progress is :%@",value);
 }];
[webView addObserverForType:WWObserverTypeTitle target:self selector:@selector(setTitle:)]; 
  1. Call Native (ObjC/Swift) API in Javascript
toObjC.callSyn()

var fromObjC = toObjC.callSyn("Hellow! I'm js!")

toObjC.callAsyn("Hellow! I'm js!",function(v){
    return "OK ,I received your message:" + v;
})

toObjC.callAsyn(function(v){
    return "OK ,I received your message:" + v;
})

Installation

WZBridge is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod 'WZBridge'

Author

arcangelw, [email protected]

License

WZBridge is available under the MIT license. See the LICENSE file for more info.

wzbridge's People

Contributors

arcangelw avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.