移动开发平台 mPaaS iOS端原生与WKWebView交互原理(如何打印蚂蚁框架所有原生与h5交互数据)

 

 

这个问题不难,但网上却搜不到,自己写写当做科普吧

 

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

 

 

 

 

 

 

 

 

 

 

 

 

本demo是WKWebView的基本使用和交互 ,实现了原生调用js的方法、js调用原生的方法、通过拦截进行交互的方法;修改内容 加入沙盒 / /加载沙盒 不带参数 // NSArray * paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); // NSString * path = [paths objectAtIndex:0]; // path = [path stringByAppendingString:[NSString stringWithFormat:@"/app/html/index.html"]]; // NSURL *url = [NSURL URLWithString:[[NSString stringWithFormat:@"file://%@",path] stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLFragmentAllowedCharacterSet]] relativeToURL:[NSURL fileURLWithPath:NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject]]; // [self.wkView loadFileURL:url allowingReadAccessToURL:[NSURL fileURLWithPath: [paths objectAtIndex:0]]]; // 带参数 /* NSArray * paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString * path = [paths objectAtIndex:0]; path = [path stringByAppendingString:[NSString stringWithFormat:@"/app/html/index.html"]]; NSURL * url = [NSURL fileURLWithPath:path isDirectory:NO]; NSURLComponents *urlComponents = [NSURLComponents componentsWithURL:url resolvingAgainstBaseURL:NO]; [queryItemArray addObject:[NSURLQueryItem queryItemWithName:@"version" value:[[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"]]]; [urlComponents setQueryItems:queryItemArray]; [self.wkView loadFileURL:urlComponents.URL allowingReadAccessToURL:[NSURL fileURLWithPath: [paths objectAtIndex:0]]]; */
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值