1.开放平台取两个参数 WXAppid和UniversalLinks
2.pod 'WechatOpenSDK'
3.桥接文件#import "WXApi.h"
5.AppDelegate
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// WXAppID为微信申请的App ID,WXUniversalLinks为微信申请的Universal Links
WXApi.registerApp(WXAppID, universalLink: WXUniversalLinks)
return true
}
重写 AppDelegate 的 handleOpenURL 和 openURL 方法
// iOS9.0及以后推荐使用
func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {
let handleUrlStr = url.absoluteString
if let handleUrl = URL(string: handleUrlStr) {
return WXApi.handleOpen(handleUrl, delegate: self)
}
return false
}
// 必须实现
func application(_ application: UIApplication, open url: URL, sourceApplication: String?, annotation: Any) -> Bool {
let handleUrlStr = url.absoluteString
if let handleUrl = URL(string: handleUrlStr) {
return WXApi.handleOpen(handleUrl, delegate: self)
}
return false
}
重写AppDelegate的continueUserActivity方法
func application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: @escaping ([UIUserActivityRestoring]?) -> Void) -> Bool {
return WXApi.handleOpenUniversalLink(userActivity, delegate: self)
}
分享结果或支付结果回调App
// 实现WXApiDelegate代理方法
func onResp(_ resp: BaseResp) {
guard resp.isKind(of: PayResp.self) else {
// 微信分享结果回调
let wxSendResult = resp as! SendMessageToWXResp
debugPrint(wxSendResult.errCode)
return
}
// 微信支付结果回调
let payresp = resp as! PayResp
debugPrint(payresp.errCode)
唤起微信支付:
// 数据model用于接收后端返回的唤起微信支付必要参数
let req = PayReq()
/** 商家向财付通申请的商家id */
req.partnerId = model.partnerid
/** 预支付订单 */
req.prepayId = model.prepayid
/** 随机串,防重发 */
req.nonceStr = model.noncestr
/** 时间戳,防重发 */
req.timeStamp = UInt32(model.timestamp)
/** 商家根据财付通文档填写的数据和签名 */
req.package = model.package
/** 商家根据微信开放平台文档对数据做的签名 */
req.sign = model.sign
WXApi.send(req) { (bool) in
// 返回是否成功唤起微信支付
debugPrint(bool)
}