iOS 唤起微信小程序

最近做了一个新功能。App里面点击按钮,唤起微信小程序。

App配置

在这里插入图片描述
稍后再说applink 的配置步骤。
上面的applink不是随便填写的,是需要和微信开发者平台上保持一致。
在这里插入图片描述
上面的这些配置数据都需要从微信开发者平台申请获取。

微信开发者平台配置

方案1 sharesdk:

项目里面已经使用了shareSDK(v 4.4.9版本)并且包含了微信SDK,那么就可以直接使用shareSDK的封装方法调用。
在这里插入图片描述

.h文件

// 引入头文件
#import <WechatConnector/WechatConnector.h>

在需要按钮点击的地方使用下面的代码

// 点击按钮 唤起微信小程序
 [WeChatConnector openMiniProgramWithUserName:@"需要跳转的小程序的原始名称" path:@"需要跳转的小程序的页面地址" miniProgramType:0 extMsg:@"" extDic:@{
    
    } complete:^(BOOL success) {
    
    
        if (success) {
    
    
            NSLog(@"ok===");
       }else{
    
    
            NSLog(@"no");
      }
 }];

问题:在使用上面的方法的时候不能够使用剪切板功能。
UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
pasteboard.string = [NSString stringWithFormat:@“%@”,self.car.VINCode];
// 如果需要使用这个功能,就不建议使用shareSDK封装的方法了。

方案2:WechatOpenSDK(推荐)

1、配置:
在这里插入图片描述
2、在Appdelegate里面注册微信
在这里插入图片描述
3、需要使用唤起小程序的地方使用以下代码

UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
 pasteboard.string = [NSString stringWithFormat:@"需要复制的内容"];
        
 UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"" message:@"即将打开  ”助手“ 小程序" preferredStyle:UIAlertControllerStyleAlert];
[alertController addAction:[UIAlertAction actionWithTitle:@"允许" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
    
    
 // 拉起微信小程序
 WXLaunchMiniProgramReq *launchMiniProgramReq = [WXLaunchMiniProgramReq object];
launchMiniProgramReq.userName = @"gh_123456789";  //拉起的小程序的username
launchMiniProgramReq.path = @"pages/index/index";    //拉起小程序页面的可带参路径,不填默认拉起小程序首页
 launchMiniProgramReq.miniProgramType = WXMiniProgramTypeRelease; //拉起小程序的类型
 [WXApi sendReq:launchMiniProgramReq completion:nil];
}]];
[alertController addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]];

[self presentViewController:alertController animated:YES completion:nil];

猜你喜欢

转载自blog.csdn.net/c1o2c3o4/article/details/128345533