Skip to content
This repository has been archived by the owner on Feb 15, 2019. It is now read-only.
/ MXBridge Public archive

a easy way for javaScript to call Objective-C in UIWebview

License

Notifications You must be signed in to change notification settings

CodingForMoney/MXBridge

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

18 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

废弃 DEPRECATED

通过JavaScriptCore搞的js桥接,但是WKWebView中拿不到JSC, 只能废弃了。

MXBridge

Bridge betweeen iOS and JavaScript.

English

Bridge JavaScript and Objective-C using the JavaScriptCore.

Classes description:

  • MXWebviewContext : A context for global setting.
  • MXWebviewBridge : The bridge connected Objective-C and JavaScript. UIWebview will hold a MXWebviewBridge instance within the lifecycle.
  • MXWebviewPlugin : Objective-C plugin which provide functions for JavaScript. A plugin has some functions. js will specify the plugin and function to call Native.
  • MXNativeInvocation : Storing Information for one call from JavaScript to Objective-C.
  • MXNativeMethod : Storing Native method info.
  • MXWebviewPluginConfig : Store Native plugin info.

Usage

Add MXBridge to your project by Cocoapods:

pod 'MXBridge'

To use MXBridge, you can use MXWebView. Or you can use MXWebViewBridgeWrapper :

_bridge = [MXWebViewBridgeWrapper wrapperWithWebView:webview];
_bridge.webviewDelegate = self;

Create a plugin for JavaScript:

@interface TestPlugin : MXWebviewPlugin

@end
@implementation TestPlugin
MX_EXTERN_METHOD(hello, helloworld)
- (NSDictionary *)helloworld {
    return @{@"data":@"Hello world , hello MXBridge!"};
}

MX_EXTERN_METHOD(loadPicture, loadPicture:)
- (void)loadPicture:(MXCallNativeInvocation *)invocation {
    NSString *url = invocation.arguments[@"url"];
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:url]];
    [[[NSURLSession sharedSession] dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        if (error) {
            [self callBackSuccess:NO withString:nil toInvocation:invocation];
        }else {
            NSString *str = [data base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
            [self callBackSuccess:YES withString:str toInvocation:invocation];
        }
    }] resume];
}

@end

Use MX_EXTERN_METHOD to export functions of plugins. First argument is the function name in js . The second argument is the selector of the exported function.

Register plugin before using :

[[MXWebviewContext shareContext] registerPlugin:[TestPlugin class] name:@"test"];

Then,you can call the plugin in your JavaScript Code :

function clickSync() {
	var retString = mxbridge.execSync("test","hello");
	if (retString.data) {
        mxbridge.log(retString.data);
		alert(retString.data);
	}
}
function clickAysn() {
    mxbridge.execSafely("test","loadPicture",{"url":"http://resource.luoxianming.cn/steam.gif"},function successDownload(data){
    	document.getElementById("showImg").src = "data:image/png;base64," + data ;
    });
}

Notice

The JSContext inits every time after the finish of website loading in the UIWebview.So the MXBridge is unavailable before the loading finished. MXBridge post a notification bridgeReady after the initializtion.You should call the Objective-C plugins after the bridgeReady notification recieved.

More documents in the plan.

License

These specifications and CocoaPods are available under the MIT license.

About

a easy way for javaScript to call Objective-C in UIWebview

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published