这个问题不难,但网上却搜不到,自己写写当做科普吧
mPaaS使用的就是系统提供的最原始的交互方式,包括 WKWebView调用原生 和 原生调用WKWebView
WKWebView调用原生
调用原生就找这个方法- (void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message;
hopper一下很容易看到使用的是PSDScriptMessageHandlerProxy这个类
然后就可以hook然后打印所有蚂蚁内部h5调用原生所传的所有原始参数了
.h
//
// PSDScriptMessageHandlerProxy+YYY.h
// MPTinyAppDemo_pod
//
// Created by YYY on 2020/7/21.
// Copyright © 2020 yangwei. All rights reserved.
//
@interface PSDScriptMessageHandlerProxy :NSObject
@end
NS_ASSUME_NONNULL_BEGIN
@interface PSDScriptMessageHandlerProxy (YYY)
@end
NS_ASSUME_NONNULL_END
.m
//
// PSDScriptMessageHandlerProxy+YYY.m
// MPTinyAppDemo_pod
//
// Created by YYY on 2020/7/21.
// Copyright © 2020 yangwei. All rights reserved.
//
#import "PSDScriptMessageHandlerProxy+YYY.h"
#import <objc/runtime.h>
@implementation PSDScriptMessageHandlerProxy (YYY)
+(void)load{
Method originalMethod = class_getInstanceMethod([self class], @selector(userContentController:didReceiveScriptMessage:));
Method swizzledMethod = class_getInstanceMethod([self class], @selector(userContentController:didReceiveScriptMessageS:));
method_exchangeImplementations(originalMethod, swizzledMethod);
}
- (void)userContentController:(nonnull WKUserContentController *)arg1 didReceiveScriptMessageS:(nonnull WKScriptMessage *)arg2;{
NSLog(@"所有h5调用原生的参数打印 %@ %@ %@",arg1,arg2.body,arg2.name);
[self userContentController:arg1 didReceiveScriptMessageS:arg2];
}
原生调用WKWebView
这个就是WKWebView的- (void)evaluateJavaScript:(NSString *)javaScriptString completionHandler:(void (^ _Nullable)(_Nullable id, NSError * _Nullable error))completionHandler;
直接hook就可以
.h
//
// WKWebView+YYY.h
//
//
// Created by YYY on 2020/7/14.
// Copyright © 2020 Alibaba. All rights reserved.
//
#import <WebKit/WebKit.h>
NS_ASSUME_NONNULL_BEGIN
@interface WKWebView (YYY)
@end
NS_ASSUME_NONNULL_END
.m
#import "WKWebView+YYY.h"
#import <objc/runtime.h>
@implementation WKWebView (YYY)
+ (void)load{
{
Method originalMethod = class_getInstanceMethod([NSClassFromString(@"WKWebView") class], @selector(evaluateJavaScript:completionHandler:));
Method swizzledMethod = class_getInstanceMethod([self class], @selector(evaluateJavaScript:completionHandlerS:));
method_exchangeImplementations(originalMethod, swizzledMethod);
}
}
- (void)evaluateJavaScript:(NSString *)javaScriptString completionHandlerS:(void (^)(id _Nullable str7 , NSError * er7))completionHandler{
NSLog(@"交互数据之原生调用h5 %@ ",javaScriptString);
[self evaluateJavaScript:javaScriptString completionHandlerS:^(id _Nullable str, NSError *er) {
if (completionHandler != nil) {
NSLog(@"交互数据之原生调用h5%@和返回数据%@ ",javaScriptString,str);
completionHandler(str,er);
}
}];
}
@end