Q:
公司要集成优络支付,以前没听过这家,网上资料也比较少。他家提供的集成文档在此
我们的APP是用ReactNative写的,套了个iOS的壳子,所以,界面都是RN的,在调用的时候,就一直崩溃,报错如下:
可以看到,崩在了SDK里面,根据日志,猜测他们应该是获取当前的viewController,在上面创建了个webview。
然鹅当前页面是RN的,直接在上面加webview各种爆奇葩问题,而且,我也不可以在当前controller上present原生ViewController,因为我用iOS套路取出的当前RN的controller,很有可能根本没办法present一个iOS的ViewControler。。。。
A:
SO,我就创建了个1像素的window,赋值给appDeletage,作为keyWindow,又在这个window上随便创建一个viewController,这样,不管SDK内是怎么获取当前viewController,都会是一个正常的iOS的viewController,而且,因为是1像素的window,所以不会挡住之前的RN界面。
不过要记得,支付完, 要把appDeletage的keyWindow换回原来的window。代码如下,写的比较乱,哈哈哈,见谅~
//AppDelegate.h
@property (nonatomic, strong) UIWindow *nativeWindow;
//AppDelegate.m
- (UIWindow *)nativeWindow
{
if (!_nativeWindow) {
_nativeWindow = [[UIWindow alloc]initWithFrame:CGRectMake(0, [UIScreen mainScreen].bounds.size.height,[UIScreen mainScreen].bounds.size.width,[UIScreen mainScreen].bounds.size.height)];
_nativeWindow.alpha = 1;
_nativeWindow.windowLevel = UIWindowLevelNormal;
}
return _nativeWindow;
}
//RNTools.m自己的一个工具类
#pragma mark - 优络H5微信支付
- (void)wechatPayWithresponseDict:(NSDictionary *)responseDict viewController:(UIViewController *)viewController
{
dispatch_main_sync_safe(^{
//配置keyWindow和viewController
UIViewController *vc = [[UIViewController alloc]init];
self.delegate.nativeWindow.rootViewController = vc;
[self.delegate.nativeWindow setFrame:CGRectMake(0, 0, 1, 1)];
[self.delegate.nativeWindow makeKeyAndVisible];
//调优络微信SDK
ULOPayReqObject *payReqObj = [[ULOPayReqObject alloc]init];
if ([responseDict[@"trade_type"] isEqualToString:@"trade.weixin.h5pay"]) {
payReqObj.prepayUrl = responseDict[@"prepay_url"];
} else {
payReqObj.prepayUrl = responseDict[@"code_url"];
}
payReqObj.prepayId = responseDict[@"prepay_id"];
[[ULOPaySDK defaultService] sendPayReq:payReqObj viewController:viewController completeBlock:^(ULOPayRespObject *respObject) {
// NSLog(@"ios YL %@, %@", @(respObject.status), respObject.returnMsg);
// if (respObject.status) { //支付成功
// NSLog(@"ios yl_H5_WX success");
// } else { //支付失败
// NSLog(@"ios yl_H5_WX failed");
// }
// [RNTools alertShowMsg:[NSString stringWithFormat:@"%@",respObject.returnMsg]];
[[RNTools sharedInstance] removeNative];
}];
})
}
//移除,并还原
- (void)removeNative{
[self.delegate.nativeWindow resignKeyWindow];
self.delegate.nativeWindow = nil;
[self.delegate.window makeKeyAndVisible];
}
dispatch_main_sync_safe是我的一个宏,用法参见我的文章
OK~ 打完收工(~ ̄▽ ̄)~