karosLi / KKJSBridge

一站式解决 WKWebView 支持离线包,Ajax/Fetch 请求,表单请求和 Cookie 同步的问题 (基于 Ajax Hook,Fetch Hook 和 Cookie Hook)
MIT License
693 stars 120 forks source link

派发事件时,发生 JavaScript 异常 #32

Closed wjiuxing closed 4 years ago

wjiuxing commented 4 years ago

我用的最新的 AjaxHook 代码(参照 #30 Ajax hook 与 NSURLProtocol)。

派发事件操作如下:

NSString *event = @"com.wjx.setting.xxx";
NSDictionary *userInfo = @{
    @"action": event,
    @"data": @YES
};
[_jsBridgeEngine dispatchEvent:event data:userInfo];

KKJSBridgeMessageDispatcher.m 202 行产生日志,为了看到更多信息,我修改了一下本地代码直接输出 error 替代 error.localizedDescription,

NSLog(@"KKJSBridge Error: evaluate JavaScript error %@, %@", error, messageJson);

日志如下:

KKJSBridge Error: evaluate JavaScript error Error Domain=WKErrorDomain Code=4 "发生 JavaScript 异常" UserInfo={WKJavaScriptExceptionLineNumber=0, WKJavaScriptExceptionMessage=TypeError: undefined is not an object, WKJavaScriptExceptionColumnNumber=0, NSLocalizedDescription=发生 JavaScript 异常}, { data = { action = "com.wjx.setting.xxx"; data = 0; }; eventName = "com.wjx.setting.xxx"; messageType = event; }

karosLi commented 4 years ago

你是在初始化的时候调用的?

wjiuxing commented 4 years ago

是的,发现 _jsBridgeEngine.isBridgeReady 是 NO,除了延迟一下,还有更好的办法吗?

karosLi commented 4 years ago

你可以监听 _jsBridgeEngine.isBridgeReady 这个值

wjiuxing commented 4 years ago

可以,感觉还不如延迟一下方便。。。 加个 ready 的 callback block 咋样?

karosLi commented 4 years ago

嗯,这个也可以

wjiuxing commented 4 years ago

我需要一个通知所有 webView 的事件,所以,我通过 [[KKWebViewPool sharedInstance] valueForKey:@"enqueueWebViews"] 来给每一个 webView 派发事件:

NSDictionary<NSString *, NSSet< __kindof KKWebView *> *> *dequeueWebViews = [[KKWebViewPool sharedInstance] valueForKey:@"dequeueWebViews"];
[dequeueWebViews enumerateKeysAndObjectsUsingBlock:^(NSString * _Nonnull key, NSSet<__kindof KKWebView *> * _Nonnull webViews, BOOL * _Nonnull stop) {
    [webViews enumerateObjectsUsingBlock:^(__kindof KKWebView * _Nonnull webView, BOOL * _Nonnull stop) {
        if (!webView.hidden && webView.engine.isBridgeReady) {
            [webView.engine dispatchEvent:notificationName data:data];
        }
    }];
}];

派发事件时,又遇见个异常问题

KKJSBridge dispatch event (null).com.wjx.sdk.setting.focusChanged: { "followStatus" : 1, "followPid" : "6463673832007184418", "channel" : "profile", "pid" : "6463673832007184418", "myFollowStatus" : 1 }

我在 KKJSBridgeJSExecutor 打印了一下 javaScriptString:

window.KKJSBridge._handleMessageFromNative('{\"data\":{\"followStatus\":1,\"followPid\":\"6463673832007184418\",\"channel\":\"profile\",\"pid\":\"6463673832007184418\",\"myFollowStatus\":1},\"eventName\":\"com.wjx.sdk.setting.focusChanged\",\"messageType\":\"event\"}')

在第27行 -evaluateJavaScript:completionHandler: 回调中返回 Error 如下:

Error Domain=WKErrorDomain Code=4 "发生 JavaScript 异常" UserInfo={WKJavaScriptExceptionLineNumber=2, WKJavaScriptExceptionMessage=SyntaxError: JSON Parse error: Unexpected identifier "undefined", WKJavaScriptExceptionColumnNumber=14262, WKJavaScriptExceptionSourceURL=https://127.0.0.1:55433/sdk.wjx.com/js/common.bundle.min.js?d=20200716150243, NSLocalizedDescription=发生 JavaScript 异常}

karosLi commented 4 years ago

你可以在 safari debug 看看,为什么会产生这个 json 解析错误。

window.KKJSBridge._handleMessageFromNative

karosLi commented 4 years ago

另外,你想要对所有 webview 都派发事件,可以在每个 webviewcontroller 里监听通知,然后针对当前 webview 派发事件

wjiuxing commented 4 years ago

查到了,H5 那边要字符串自己解析成 object,我也要转一次字符串,蛋疼。。。

karosLi commented 4 years ago

什么意思? 你目前的解析代码是什么样的?

wjiuxing commented 4 years ago

没事了,我们这边的问题

发自Jiuxing Wang's iPhone

在 2020年7月23日,下午6:44,Karosli notifications@github.com 写道:



什么意思? 你目前的解析代码是什么样的?

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHubhttps://github.com/karosLi/KKJSBridge/issues/32#issuecomment-662938198, or unsubscribehttps://github.com/notifications/unsubscribe-auth/AA73W2HWX736RVDX2JENLT3R5AICTANCNFSM4PEQCCKQ.

karosLi commented 4 years ago

1.1.5-beta10 已经支持 bridge ready 回调了