JKWKWebViewHandler 0.4.6

JKWKWebViewHandler 0.4.6

测试已测试
Lang语言 Obj-CObjective C
许可证 MIT
Released最新发布2021年12月

jack维护。



  • 作者:
  • HHL110120

JKWKWebViewHandler

CI Status Version License Platform

示例

要运行示例项目,请先克隆存储库,然后首先从 Example 目录运行 pod install

要求

安装

JKWKWebViewHandler 可通过 CocoaPods 获取。要安装它,只需将以下行添加到您的 Podfile 中。

Object-C

pod "JKWKWebViewHandler"

Swift

pod "JKWKWebViewHandler_Swift"

article

Author

HHL110120, [email protected]

QQ联系群组

如果你使用QQ,你可以使用这个二维码与我们联系

开发者指南

步骤 1

你应该配置 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];

步骤 2

此内容仅供 JS 开发者

exec:function(plugin,funcName,params,successCallBack,failureCallBack){
 //when you want to call native function ,you should call this function with params.
 //plugin:the native className to handle the js func
 //funcName:the native funcName
 
 //params:this is the data you want to send to native.

 //successCallBack:this is the success block

 //failureCallBack:this is  the failure block

}

例如

function getInfoFromNative(){
var params = {'name':'我是jack!!!'};
JKEventHandler.exec('JKPluginA','getNativeInfo',params,function(data){
console.log('succedss block');
alert(data);
},
function(data){
console.log('fail block');
alert(data);
});
}

步骤 3

对于本地开发者,如果你想要与 H5 交互,你应该创建一个插件类,并创建 H5 规定的与你交互的功能。例如

#import <Foundation/Foundation.h>

@interface JKPluginA : NSObject
+ (void)getNativeInfo:(NSDictionary *)params :(void(^)(id response))successCallBack :(void(^)(id response))failureCallBack;
@end


#import "JKPluginA.h"

@implementation JKPluginA
+ (void)getNativeInfo:(NSDictionary *)params :(void(^)(id response))successCallBack :(void(^)(id response))failureCallBack{
NSLog(@"getNativeInfo %@",params);
if (successCallBack) {
successCallBack(@"success !!!");
}
if (failureCallBack) {
failureCallBack(@"failure !!!");
}
}
@end

Swift 指南

//step 1
func configureWKWebView() -> Void {
    self.eventHandler = JKEventHandlerSwift.init(webView, self)
    let config:WKWebViewConfiguration = WKWebViewConfiguration.init()
    config.preferences = WKPreferences.init()
    config.preferences.minimumFontSize = 10
    config.preferences.javaScriptEnabled = true
    config.preferences.javaScriptCanOpenWindowsAutomatically = true
    config.processPool = WKProcessPool.init()
    
    let usrScript:WKUserScript = WKUserScript.init(source: JKEventHandlerSwift.handleJS()!, injectionTime: .atDocumentEnd, forMainFrameOnly: true)
    config.userContentController = WKUserContentController.init()
    config.userContentController.addUserScript(usrScript)
    config.userContentController.add(self.eventHandler, name: JKEventHandlerNameSwift)
    
    self.webView = WKWebView.init(frame: self.view.bounds, configuration: config)
    self.webView?.load(URLRequest.init(url: URL.init(string: self.url!)!))
    self.view.addSubview(self.webView!)
    self.eventHandler.webView = self.webView
    self.webView?.uiDelegate = self;
}

//step2
//MARK:JKEventHandlerProtocol
func nativeHandle(plugin: String?, funcName: inout String!, params: Dictionary<String, Any>?, success: ((Any?) -> Void)?, failure: ((Any?) -> Void)?) {
    if plugin == "JKPluginA" {
        JKPluginA.getNativeInfo(params: params ?? [:], successCallBack: success, failureCallBack: failure)
    }
}

//step3
class JKPluginA: NSObject {
     class func getNativeInfo(params:Dictionary<String,Any>, successCallBack:((_ response:Any?) -> Void)?, failureCallBack:((_ response:Any?) -> Void)?) -> Void {
        print("params:%@",params)
        if successCallBack != nil {
            successCallBack!("success !")
        }
        
        if failureCallBack != nil {
            failureCallBack!("failure !")
        }
    }
    
    
}

实现历程博客

《WKWebView与JS交互之完美解决方案》

许可

JKWKWebViewHandler 在 MIT 许可证下可用。有关更多信息,请参阅 LICENSE 文件。