微信支付SDK官方说明文档很坑,非常不详细,以下是本人的总结,希望新手不被官方坑。
Info
1.png
2.png
引用
3.png
桥接头文件 xxx-Bridging-Header.h
import "WXApi.h"
4.png
Other Linker Flags
5.png
AppDelegate
在 didFinishLaunchingWithOptions 中注册微信支付:
WXApi.registerApp("YourAPPID", withDescription: "YourDescription")
返回App后走这里,有两个方法是由于iOS版本不同而产生:
extension AppDelegate {
func application(_ application: UIApplication, handleOpen url: URL) -> Bool {
return WXApi.handleOpen(url, delegate: self)
}
func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey: Any] = [:]) -> Bool {
return WXApi.handleOpen(url, delegate: self)
}
}
微信支付回调:
extension AppDelegate: WXApiDelegate {
func onResp(_ resp: BaseResp!) {
if let response = resp as? PayResp {
switch response.errCode {
case WXSuccess.rawValue:
NotificationCenter.default.post(name: NSNotification.Name.WechatPayObserver, object: nil, userInfo: ["PayResult": PayResult.successful])
default:
NotificationCenter.default.post(name: NSNotification.Name.WechatPayObserver, object: nil, userInfo: ["PayResult": PayResult.failed])
}
}
}
}