-
Notifications
You must be signed in to change notification settings - Fork 15
接入微信SDK跳转微信小程序指南
Vajra Sheng edited this page Jul 11, 2022
·
4 revisions
- 用户需前置接入微信Open SDK
- App开店SDK版本大于等于 7.1.19
在初始化SDK时,需要选择配置是否已接入微信Open SDK,配置为YES时会通过微信Open SDK的方式进行跳转和回跳,配置为NO则通过原有URL Scheme的方式跳转,默认为NO。
示例代码如下:
- (void)setupAppSDK {
YZConfig *config = [[YZConfig alloc] initWithClientId:CLIENT_ID andAppKey:APP_KEY];
/// config.xxx = xxx; 此处示例代码忽略其他属性赋值
config.useWechatSDK = YES;
[YZSDK.shared initializeSDKWithConfig:config];
YZSDK.shared.delegate = self;
}在上述已配置通过微信Open SDK接入的方式下,用户在H5下单支付页面选择微信支付后,SDK会通过代理方法中 YZNotice 的 YZGoToWechatMiniProgram 事件告知到开发者需要进行微信小程序的跳转。示例代码如下:
#import "WXApi.h"
- (void)webView:(YZWebView *)webView didReceiveNotice:(YZNotice *)notice {
switch (notice.type) {
case YZGoToWechatMiniProgram: {
id response = notice.response;
if (!response || ![response isKindOfClass:[NSDictionary class]]) {
return;
}
/// 跳转微信小程序及对应参数解析
NSDictionary *dict = (NSDictionary *)response;
NSString *appId = [dict valueForKey:@"appId"] ?: @"";
NSString *path = [dict valueForKey:@"path"] ?: @"";
NSInteger type = [[dict valueForKey:@"miniprogramType"] integerValue] ?: 0;
WXLaunchMiniProgramReq *launchMiniProgramReq = [WXLaunchMiniProgramReq object];
launchMiniProgramReq.userName = appId;
launchMiniProgramReq.path = path;
launchMiniProgramReq.miniProgramType = type;
[WXApi sendReq:launchMiniProgramReq completion:^(BOOL success) {
if (!success) {
NSLog(@"小程序跳转失败,请确保安装微信并重试");
}
}];
}
default:
break;
}
}