Latest 1.0.3
Homepage https://github.com/mtlj/MTLJWebKit
License MIT
Platforms ios 9.0, requires ARC
Dependencies Masonry, ZipArchive
Authors

A set of Objective-C and Javascript interaction solutions based on WKWebView, supporting iOS 9.0

Installation (iOS)

Installation with CocoaPods

Add this to your podfile and run pod install to install:

pod 'MTLJWebKit', '~> 1.0.2'

Usage

1) Import the header file

#import <MTLJWebKit/MTLJWebKit.h>

2) Instantiate MTLJWebViewController and set originURL property

MTLJWebViewController *webViewVC = [[MTLJWebViewController alloc] init];
webViewVC.originURL = xxxURL;

3) Extends MTLJJSCommandHandlerBase class,override executeCommand:responseHandler: method and set scheme property,Register a JSCommandHandler in ObjC

[webViewVC registerJSCommandHandler:[MTLJJSCommandHandlerBaseSubClass new]];

4) Extends MTLJJSCommandResponseBase class,override executeResponse:responseData: method and set scheme、argument property,Call a JSCommandHandler in ObjC

[webViewVC callJSCommandHandler:[MTLJJSCommandResponseBaseSubClass new]];

5) Copy and paste setupMTLJWebViewJavascriptBridge into your JS:

function setupMTLJWebViewJavascriptBridge(callback) {
    if (window.MTLJWebViewJavascriptBridge) { return callback(MTLJWebViewJavascriptBridge); }
    if (window.WVJBCallbacks) { return window.WVJBCallbacks.push(callback); }
    window.WVJBCallbacks = [callback];
    var WVJBIframe = document.createElement('iframe');
    WVJBIframe.style.display = 'none';
    WVJBIframe.src = 'mtljcommand://__bridge_loaded__';
    document.documentElement.appendChild(WVJBIframe);
    setTimeout(function() { document.documentElement.removeChild(WVJBIframe) }, 0)
}

6) Finally, call setupMTLJWebViewJavascriptBridge and then use the bridge to register handlers and call ObjC handlers:

setupMTLJWebViewJavascriptBridge(function(bridge) {

    /* Initialize your app here */

    bridge.registerHandler('mtljcommand://fetchFansInfo', function(data, responseCallback) {
        log('ObjC called parameter with', data)
    var responseData = { 'Javascript Says info':'lilei' }
    log('JS responding parameter with', responseData)
    responseCallback(responseData)
    })

    bridge.callHandler('mtljcommand://fetchPhoneInfo', {'platform': 'ios'}, function(response) {
    log('JS got response', response)
    })
})

Latest podspec

{
    "name": "MTLJWebKit",
    "version": "1.0.3",
    "summary": "MTLJWebKit.",
    "description": "A short description of WebKitFramework",
    "homepage": "https://github.com/mtlj/MTLJWebKit",
    "license": {
        "type": "MIT",
        "file": "LICENSE"
    },
    "authors": {
        "lj": ""
    },
    "platforms": {
        "ios": "9.0"
    },
    "source": {
        "git": "https://github.com/mtlj/MTLJWebKit.git",
        "tag": "1.0.3"
    },
    "vendored_frameworks": "MTLJWebKit.framework",
    "requires_arc": true,
    "dependencies": {
        "Masonry": [
            "~> 1.1.0"
        ],
        "ZipArchive": [
            "~> 1.4.0"
        ]
    }
}

Pin It on Pinterest

Share This