[Swift]App跳转到微信小程序

1、微信开放平台添加应用并关联小程序

2、微信开放平台SDK集成

SDK下载地址:https://open.weixin.qq.com/cgi-bin/showdocument?action=dir_list&t=resource/res_list&verify=1&id=open1419319164&token=&lang=zh_CN

AppDelegate.swift

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder,UIApplicationDelegate {
    
    var window: UIWindow?

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        WXApi.registerApp("微信开放平台的AppID")
        return true
    }
    
    /// iOS 8 及以下
    func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {
        return WXApi.handleOpen(url, delegate: self)
    }
    
    /// iOS 9 及以上
    func application(_ application: UIApplication, open url: URL, sourceApplication: String?, annotation: Any) -> Bool {
        return WXApi.handleOpen(url, delegate: self)
    }
}

extension AppDelegate: WXApiDelegate {
    func onReq(_ req: BaseReq) {}
    func onResp(_ resp: BaseResp) {}
}

3、info.plist文件设置白名单

4、TARGETS-info设置微信小程序APPID

5、调起微信小程序

    /// 跳转到小程序
    ///
    /// - Parameters:
    ///   - originalID: 小程序原始ID gh_开头
    ///   - path: 小程序页面路径
    static func jumpToMiniProgram(_ originalID: String, path: String) {
        
        let miniProgram = WXLaunchMiniProgramReq.object()
        // 小程序原始ID,小程序信息可查看,以gh_开头
        miniProgram.userName = originalID
        // 小程序页面路径
        miniProgram.path = path
        // 小程序版本
        /*
        WXMiniProgramTypeRelease = 0,     // 正式版
        WXMiniProgramTypeTest = 1,        // 开发版 
        WXMiniProgramTypePreview = 2,     // 体验版 
        */
        miniProgram.miniProgramType = .release
        WXApi.send(miniProgram)
    }

你可能感兴趣的:([Swift]App跳转到微信小程序)