有段时间没有来博客了,我自己都觉得不好意思了,又不是很忙,还老是没有时间
在这里插入图片描述

CocoaPods集成 依赖库


#友盟分享 SDK集成
pod 'UMCCommon'
pod 'UMCSecurityPlugins'
# 集成微信(精简版0.2M)
pod 'UMCShare/Social/ReducedWeChat'
# 集成QQ/QZone/TIM(精简版0.5M)
pod 'UMCShare/Social/ReducedQQ'
# U-Share SDK UI模块(分享面板,建议添加)
pod 'UMCShare/UI'
#开发阶段进行调试SDK及相关功能使用,可在发布 App 前移除
pod 'UMCCommonLog'

# 微信API
pod 'WechatOpenSDK'

AppDelegate初始化

        UMConfigure.setLogEnabled(true)
        UMSocialGlobal.shareInstance()?.isUsingHttpsWhenShareContent = false
        UMConfigure.initWithAppkey(UMAPPKEY, channel: "App Store")
        /* 设置微信的appKey和appSecret */
        UMSocialManager.default()?.setPlaform(UMSocialPlatformType.wechatSession, appKey: WXAPICode, appSecret: WXAppSecret, redirectURL: UniversalLink)

        /* 设置分享到QQ互联的appID
         * U-Share SDK为了兼容大部分平台命名,统一用appKey和appSecret进行参数设置,而QQ平台仅需将appID作为U-Share的appKey参数传进即可。
         */
		UMSocialManager.default()?.setPlaform(UMSocialPlatformType.QQ, appKey: "110****72", appSecret: "jSL*****Mni", redirectURL: "")

        WXApi.startLog(by: WXLogLevel.detail) { (log) in
            PDLog("微信Log:" + log)
        }
        WXApi.registerApp(WXAPICode, universalLink: UniversalLink)
        
//        WXApi.checkUniversalLinkReady { (step, result) in
//            PDLog("UniversalLink", step)
//            PDLog("UniversalLink", result)
//        }
        PDLog("微信Ver:" + WXApi.getVersion())

然后就来重点了,敲黑板,注意听讲

AppDelegate
    func application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: @escaping ([UIUserActivityRestoring]?) -> Void) -> Bool {
        WXApi.handleOpenUniversalLink(userActivity, delegate: self)
        return true
    }
    
    func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {
        let result: Bool = UMSocialManager.default()?.handleOpen(url, options: options) ?? false
        if !result {
            // 其他如支付等SDK的回调
            return WXApi.handleOpen(url, delegate: self)
        }
            return result
    }

//MARK: 微信
extension AppDelegate: WXApiDelegate {
    func onResp(_ resp: BaseResp) {
        
        //         WXSuccess           = 0,    /**< 成功    */
        //        WXErrCodeCommon     = -1,   /**< 普通错误类型    */
        //        WXErrCodeUserCancel = -2,   /**< 用户点击取消并返回    */
        //        WXErrCodeSentFail   = -3,   /**< 发送失败    */
        //        WXErrCodeAuthDeny   = -4,   /**< 授权失败    */
        //        WXErrCodeUnsupport  = -5,   /**< 微信不支持    */
        
        if resp is PayResp {
            PDLog(resp.errStr)
            switch resp.errCode {
                
            case 0 :
                PDToast.showMessage("支付成功")
                NotificationCenter.default.post(name: NSNotification.Name.WXPaySuccessNotification, object: nil)
            case -1:
                PDToast.showMessage("支付失败")
                NotificationCenter.default.post(name: NSNotification.Name.WXPayFailedNotification, object: nil)
            case -2:
                PDToast.showMessage("用户取消")
                NotificationCenter.default.post(name: NSNotification.Name.WXPayFailedNotification, object: nil)
            case -3:
                PDToast.showMessage("发送失败")
                NotificationCenter.default.post(name: NSNotification.Name.WXPayFailedNotification, object: nil)
            case -4:
                PDToast.showMessage("授权失败")
                NotificationCenter.default.post(name: NSNotification.Name.WXPayFailedNotification, object: nil)
            case -5:
                PDToast.showMessage("微信不支持")
                NotificationCenter.default.post(name: NSNotification.Name.WXPayFailedNotification, object: nil)
                
            default:
                PDToast.showMessage("支付失败")
                NotificationCenter.default.post(name: NSNotification.Name.WXPayFailedNotification, object: nil)
            }
            
        }
    }
}

SceneDelegate
    func scene(_ scene: UIScene, openURLContexts URLContexts: Set<UIOpenURLContext>) {
        if let url = URLContexts.first?.url {
            UMSocialManager.default()?.handleOpen(url)
            WXApi.handleOpen(url, delegate: self)
        }
    }
    func scene(_ scene: UIScene, continue userActivity: NSUserActivity) {
        WXApi.handleOpenUniversalLink(userActivity, delegate: self)
    }

@available(iOS 13.0, *)
extension SceneDelegate: WXApiDelegate {
    func onResp(_ resp: BaseResp) {
        
        //         WXSuccess           = 0,    /**< 成功    */
        //        WXErrCodeCommon     = -1,   /**< 普通错误类型    */
        //        WXErrCodeUserCancel = -2,   /**< 用户点击取消并返回    */
        //        WXErrCodeSentFail   = -3,   /**< 发送失败    */
        //        WXErrCodeAuthDeny   = -4,   /**< 授权失败    */
        //        WXErrCodeUnsupport  = -5,   /**< 微信不支持    */
        
        if resp is PayResp {
            PDLog(resp.errStr)
            switch resp.errCode {
                
            case 0 :
                PDToast.showMessage("支付成功")
                NotificationCenter.default.post(name: NSNotification.Name.WXPaySuccessNotification, object: nil)
            case -1:
                PDToast.showMessage("支付失败")
                NotificationCenter.default.post(name: NSNotification.Name.WXPayFailedNotification, object: nil)
            case -2:
                PDToast.showMessage("用户取消")
                NotificationCenter.default.post(name: NSNotification.Name.WXPayFailedNotification, object: nil)
            case -3:
                PDToast.showMessage("发送失败")
                NotificationCenter.default.post(name: NSNotification.Name.WXPayFailedNotification, object: nil)
            case -4:
                PDToast.showMessage("授权失败")
                NotificationCenter.default.post(name: NSNotification.Name.WXPayFailedNotification, object: nil)
            case -5:
                PDToast.showMessage("微信不支持")
                NotificationCenter.default.post(name: NSNotification.Name.WXPayFailedNotification, object: nil)

            default:
                PDToast.showMessage("支付失败")
                NotificationCenter.default.post(name: NSNotification.Name.WXPayFailedNotification, object: nil)
            }
            
        }
    }
}

就问你神奇不神奇,UIApplicationDelegate 代理都不走了,搞了一下午!还没来得及踩坑的,赶紧的,把这块砖搬走吧。在这里插入图片描述

Logo

北京人形旗下天工造物具身智能开源社区,聚焦具身天工与慧思开物两大平台

更多推荐