DSBridge是比較好用的原生與JS交互跨平臺(tái)的jsbridge,支持iOS跛蛋、安卓。
git地址:
https://github.com/wendux/DSBridge-IOS
https://github.com/wendux/DSBridge-Android
iOS端用法簡(jiǎn)介
- pod安裝
pod "dsBridge"
- 使用
1痊硕、Implement APIs in a class
//JsApiTest.m
@implementation JsApiTest
//for synchronous invocation
- (NSString *)testSyn:(NSDictionary *)args
{
return [(NSString *)[args valueForKey:@"msg"] stringByAppendingString:@"[ syn call]"];
}
//for asynchronous invocation
- (NSString *)testAsyn:(NSDictionary *)args result:(JSCallback)completionHandler
{
completionHandler(@"asyn call", YES);
}
@end
創(chuàng)建JsApiTest類(lèi)赊级,只要在.m中實(shí)現(xiàn)與JS端協(xié)商好的方法即可,不需要在.h中聲明。如
- (NSString *)testSyn:(NSDictionary *)args
2岔绸、add API object to DWKWebView
DWKWebView *dwebview = [[DWKWebView alloc] initWithFrame:bounds];
// register api object without namespace
[dwebview addJavascriptObject:[[JsApiTest alloc] init] namespace:nil];
namespace要與JS協(xié)商決定理逊。
需要注意的地方 !盒揉!
JS端有彈框(alert/confirm/prompt)等晋被,DSBridge中已經(jīng)實(shí)現(xiàn)了WKUIDelegate代理方法中的代理,默認(rèn)對(duì)話(huà)框標(biāo)簽文本語(yǔ)言為中文刚盈,您可以通過(guò)調(diào)用來(lái)自定義文本customJavascriptDialogLabelTitles羡洛。如果您仍然想自己實(shí)現(xiàn)它們,請(qǐng)?jiān)O(shè)置作為DSUIDelegate 的代理的屬性WKUIDelegate藕漱。