Latest 0.3.1
Homepage https://github.com/xindizhiyin2014/JKWKWebViewHandler
License MIT
Platforms ios 8.0
Authors

CI Status
Version
License
Platform

Example

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

Requirements

Installation

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

pod "JKWKWebViewHandler"

Author

HHL110120, [email protected]

QQ Contact group

if you use QQ you can use this Qrcode to contact with us

developer guide

step 1

you should config the JKEventHandler

WKUserScript *usrScript = [[WKUserScript alloc] initWithSource:[JKEventHandler shareInstance].handlerJS injectionTime:WKUserScriptInjectionTimeAtDocumentEnd forMainFrameOnly:YES];

    // 通过JS与webview内容交互
    config.userContentController = [[WKUserContentController alloc] init];

    [config.userContentController addUserScript:usrScript];
    // 注入JS对象名称AppModel,当JS通过AppModel来调用时,
    // 我们可以在WKScriptMessageHandler代理中接收到
    [config.userContentController addScriptMessageHandler:[JKEventHandler shareInstance] name:EventHandler];

    //通过默认的构造器来创建对象
    _webView = [[WKWebView alloc] initWithFrame:self.view.bounds
                                  configuration:config];
    [_webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:_url]]];
    [self.view addSubview:_webView];

step 2

this is for JS developer

callNativeFunction:function(nativeMethodName,params,callBackID,callBack){
 //when you want to call native function ,you should call this function with params.

 //nativeMethodName: this is the functionName of native.

 //params:this is the data you want to send to native.

 //callBackID:this is the callBackIdentifier you should to specified if you need a callBack. you shoud make sure it is unique

 //callBack:this is the content of the callback.

}

for Example:

JKEventHandler.callNativeFunction('getInfoFromNative',params,'getInfoFromNativeCallback',function(data){
            alert(data);
        });

step 3

this is for native developer
if you want interect with H5,you should create a category of class JKEventHandler. and create the function the H5 specified to interected with you. for example:

#import <JKWKWebViewHandler/JKWKWebViewHandler.h>

@interface JKEventHandler (Demo)

- (void)sendInfoToNative:(id)params;

- (void)getInfoFromNative:(id)params :(void(^)(id response))callBack;

@end

#import "JKEventHandler+Demo.h"

@implementation JKEventHandler (Demo)

- (void)sendInfoToNative:(id)params{
    NSLog(@"sendInfoToNative :%@",params);
}

- (void)getInfoFromNative:(id)params :(void(^)(id response))callBack{
    NSLog(@"params %@",params);
    NSString *str = @"'Hi Jack!'";
    callBack(str);

}
@end

License

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

Latest podspec

{
    "name": "JKWKWebViewHandler",
    "version": "0.3.1",
    "summary": "This is a tool of WKWebivew interact with H5.",
    "description": "TODO: This is a tool of WKWebivew interact with H5, it add a callback event of every JS funtion if needed. and when the ViewController will be destroyed all the callback events can be cleaned. it is different to the way create a new iframe and catch the url to interact with H5.",
    "homepage": "https://github.com/xindizhiyin2014/JKWKWebViewHandler",
    "license": {
        "type": "MIT",
        "file": "LICENSE"
    },
    "authors": {
        "HHL110120": "[email protected]"
    },
    "source": {
        "git": "https://github.com/xindizhiyin2014/JKWKWebViewHandler.git",
        "tag": "0.3.1"
    },
    "platforms": {
        "ios": "8.0"
    },
    "source_files": "JKWKWebViewHandler/Classes/**/*.{h,m}",
    "resources": "JKWKWebViewHandler/Resources/*.js"
}

Pin It on Pinterest

Share This