Latest 0.1.0
Homepage https://github.com/arcangelw/WZBridge
License MIT
Platforms ios 8.0, requires ARC
Frameworks WebKit, UIKit, Foundation
Authors

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.

Latest podspec

{
    "name": "WZBridge",
    "version": "0.1.0",
    "summary": "A short description of WZBridge.",
    "description": "TODO: Add long description of the pod here.",
    "homepage": "https://github.com/arcangelw/WZBridge",
    "license": {
        "type": "MIT",
        "file": "LICENSE"
    },
    "authors": {
        "arcangelw": "[email protected]"
    },
    "source": {
        "git": "https://github.com/arcangelw/WZBridge.git",
        "tag": "0.1.0"
    },
    "platforms": {
        "ios": "8.0"
    },
    "requires_arc": true,
    "source_files": "WZBridge/Classes/**/*.{h,m}",
    "resources": "WZBridge/Classes/*.js",
    "public_header_files": "WZBridge/Classes/**/*.h",
    "module_map": "WZBridge/Classes/WZBridge.modulemap",
    "frameworks": [
        "WebKit",
        "UIKit",
        "Foundation"
    ],
    "static_framework": true,
    "user_target_xcconfig": {
        "OTHER_LDFLAGS": "-ObjC"
    }
}

Pin It on Pinterest

Share This