GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — GitHub - DouKing/STMScriptMessageHandler: A script message handler for wkwebview is used to communicate with js each other.

BranchesTags
Open more actions menu

Latest commit

 

History

52 Commits
52 Commits

Folders and files

NameName
Last commit message
Last commit date
STMScriptMessageHandler.xcodeproj
STMScriptMessageHandler.xcodeproj
 
 
STMScriptMessageHandler
STMScriptMessageHandler
 
 
.gitignore
.gitignore
 
 
Capture.gif
Capture.gif
 
 
LICENSE
LICENSE
 
 
LICENSE.MIT
LICENSE.MIT
 
 
LICENSE.NPL
LICENSE.NPL
 
 
README.md
README.md
 
 
STMScriptMessageHandler.podspec
STMScriptMessageHandler.podspec
 
 

Repository files navigation

996.icu LICENSE

STMScriptMessageHandler

The STMScriptMessageHandler is used to comunicate with js for WKWebView. It implements WKScriptMessageHandler and WKScriptMessageHandlerWithReply protocol. A STMScriptMessageHandler corresponding a js object. When your WKWebView add a STMScriptMessageHandler, the js side add a object on the window automatically. The handlerName of STMScriptMessageHandler is the js object's name.

Features

  • Support multi message handlers
  • Support Promise
  • Compatible with WebViewJavascriptBridge

Requirements

iOS 8.0+

Usage

Native side

//When the native register a STMScriptMessageHandler called `Bridge`, the js register a object called `window.Bridge`.
STMScriptMessageHandler *messageHandler = [[STMScriptMessageHandler alloc] initWithScriptMessageHandlerName:@"Bridge" forWebView:self.webView];
[self.webView stm_addScriptMessageHandler:messageHandler];

// register a message handler named `Page`, so the js should call your method (that the message handler registered) use `window.Page.callMethod...`
STMScriptMessageHandler *page = [[STMScriptMessageHandler alloc] initWithScriptMessageHandlerName:@"Page" forWebView:self.webView];
[self.webView registerScriptMessageHandler:page];

[page registerMethod:@"setButtons" handler:^(id data, STMResponseCallback responseCallback) {
    [self setupRightBarButtonItems:data callback:responseCallback];
}];

JS side

// Use js object `window.Bridge` call native method or register method for native.
window.Bridge.callHandler('nslog', data, function (data) {
    log('JS got native `nslog` response', data);
});

window.Bridge.registerMethod('log', function(data, callback){
    var message = JSON.parse(data);
    log('Native calling js method `log`', message);
    callback({key: 'from js', value: 'something'});
});

// Support Promise
async function promise(data) {
    // window.Bridge.callHandler('nslog', data).then(
    //     result => log('JS got native `nslog` response', result),
    //     error => log('JS got native `nslog` error', error)
    // )

    var p = window.Bridge.callHandler('nslog', data);
    var result = await p;
    log('JS got native `nslog` response', result);
}

Migrate from WebViewJavascriptBridge

If you register a message handler named WebViewJavascriptBridge at native side, the js side dones not need modify any code.

// The bridge's name is `WebViewJavascriptBridge`
self.bridge = [self.webView stm_addScriptMessageHandlerUseName:@"WebViewJavascriptBridge"];

Installation

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

pod 'STMScriptMessageHandler'

License

STMScriptMessageHandler is available under the MIT license and 996ICU license. See file LICENSE.MIT and LICENSE.NPL for more info.

About

A script message handler for wkwebview is used to communicate with js each other.

Topics

javascriptjsscriptmessagewebviewwkscriptmessagehandlerwkwebviewwkwebview-javascript-bridgewkwebview-js

Resources

Readme
Activity

Stars

3 stars

Watchers

1 watching

Forks

1 fork
Report repository

Releases

Packages

Contributors

Languages

You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.