一、準備
1、在微信開放平臺注冊應用的相關信息,用于獲取微信支付或分享需要的WXAppid和UniversalLinks;
2、注冊成功之后,你可以看到如圖所示相關信息:
獲取AppID
獲取UniversalLinks
3、拷貝保存至項目本地,這里我分別命名為:WXAppID和WXUniversalLinks。
二、項目配置
1、導入WechatOpenSDK,推薦用Cocoapods方式:
- 在Podfile文件中加入:
pod 'WechatOpenSDK'
- 執行
pod install
或pod update
- 注意:命令行下執行 pod search WechatOpenSDK,如顯示的 WechatOpenSDK 版本不是最新的,則先執行 pod repo update 操作更新本地 repo 的內容。
2、工程配置
-
在 Xcode 中,選擇你的工程設置項,選中“TARGETS”一欄,在“info”標簽欄下的“URL type“添加“URL scheme”為你所注冊的應用程序 id:
Xcode 設置URL Scheme -
在Xcode中,選擇你的工程設置項,選中“TARGETS”一欄,在 “info”標簽欄的“LSApplicationQueriesSchemes“添加weixin 和weixinULAPI(如下圖所示)
添加weixin 和weixinULAPI -
打開Associated Domains開關,將Universal Links域名加到配置上
配置Universal Links - 配置apple-app-site-association文件內容,并上傳至服務器主域名根目錄下,保證線上域名任何請求均可以訪問到該文件,apple-app-site-association文件內容示例如下:
{
"applinks": {
"apps": [],
"details": [
{
"appID": "TeamID.BundleID",
"paths": ["*"]
}
]
}
}
其中,paths 路徑建議設置*
通配符;
TeamID為開發者中心的Membership下的Team ID;
獲取Team ID
BundleID為Xcode->TARGETS->General->Identity下的配置Bundle ID
獲取Bundle ID
三、具體實現(以iOS項目Swift語言為例)
1、在AppDelegate.swift文件的didFinishLaunchingWithOptions方法里注冊WXApi:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// WXAppID為微信申請的App ID,WXUniversalLinks為微信申請的Universal Links
WXApi.registerApp(WXAppID, universalLink: WXUniversalLinks)
return true
}
2、重寫 AppDelegate 的 handleOpenURL 和 openURL 方法:
// iOS9.0以前使用
func application(_ application: UIApplication, handleOpen url: URL) -> Bool {
let handleUrlStr = url.absoluteString
if let handleUrl = URL(string: handleUrlStr) {
return WXApi.handleOpen(handleUrl, delegate: self)
}
return false
}
// 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
}
3、重寫AppDelegate的continueUserActivity方法:
func application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: @escaping ([UIUserActivityRestoring]?) -> Void) -> Bool {
return WXApi.handleOpenUniversalLink(userActivity, delegate: self)
}
4、喚起微信分享:
// 這里以發送圖片+文字+鏈接形式(微信卡片形式)
guard WXApi.isWXAppInstalled() && WXApi.isWXAppSupport() else {
// 如果未安裝微信或者微信版本過低,提示去安裝或升級
// ...
return
}
let sendMessageToWXReq = SendMessageToWXReq()
sendMessageToWXReq.scene = Int32(場景值:微信好友or朋友圈)
sendMessageToWXReq.bText = false
let wxMediaMessage = WXMediaMessage()
wxMediaMessage.title = "分享標題"
wxMediaMessage.description = "描述文案"
if let _ = shareModel.image {
// 壓縮圖片,防止圖片數據過大導致分享無法成功,這里微信要求大小不能超過64K
let thumImageData = UIImage.generateImageData(image: shareModel.image!, maxSize: CGSize(width: 300, height: 300), maxDataSize: 32, reduceFrequency: 0.2)
if let _ = thumImageData {
wxMediaMessage.thumbData = thumImageData as Data?
}
}
let wxWebPage = WXWebpageObject()
wxWebPage.webpageUrl = "跳轉url"
wxMediaMessage.mediaObject = wxWebPage
sendMessageToWXReq.message = wxMediaMessage
WXApi.send(sendMessageToWXReq)
-
微信分享成功效果如下:
分享成功示例
5、喚起微信支付:
// 數據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)
}
-
微信支付流程如下圖示:
喚起微信支付頁面
輸入密碼
支付成功
6、分享結果或支付結果回調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)