iOS13 微信支付openSDK1.8.6 回调失败
xcode 11.3.1(11C504) swift4
iPhone设备:iOS13.3.1
微信 v7.0.11
问题描述:
集成微信支付已经成功,但是不走回调,也就是说APP不能立即知道是不是支付成功了。
好些场景情况下,我们是要作些处理的,这样更加的提高用户的体验,比如说充值,我们需要立即给用户的余额加上。
解决方法:
1)在iOS13中,引入了分屏,这个是之前没有的,当您用xcode11建一个新的工程的时候,会发现多了一个SceneDelegate文件,这个文件就包括了场景Scene
这里面可以建window对象,也就是说这个从AppDelegate中分离出来了,目的就是为了支持分屏。
这种情况下,微信支付回调,会走SceneDelegate
2)那么如何处理呢,有些设备因为比较老,还不是iOS13,比如iOS12等等,有些微信的版本并没有超过7.0.5,那么微信支付还是会走AppDelegate
3) 这样的话,我们既要满足iOS13, 又要满足之前的版本,可以作以下处理:
3.1)加入版本判断
3.2)将以前不支持的SceneDelegate,加入进来即可
下面是具体的实现:

在AppDelegate加入方法,让AppDelegate知道有SceneDelegate
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
//注册微信打印日志
WXApi.startLog(by: WXLogLevel.init(rawValue: 1)!) { (msg) in
print(msg)
}
//注册APPID
let result = WXApi.registerApp(WX_APPID, universalLink: WX_UniversalLink)
print(result)
if #available(iOS 13.0, *) {
print("如果是iOS13,那么进入scene")
}else{
window = UIWindow()
window?.frame = UIScreen.main.bounds
window?.rootViewController = ViewController()
window?.makeKeyAndVisible()
}
return true
}
// MARK: UISceneSession Lifecycle
@available(iOS 13.0, *)
func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {
// Called when a new scene session is being created.
// Use this method to select a configuration to create the new scene with.
return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role)
}
func onReq(_ req: BaseReq) {
//onReq是微信终端向第三方程序发起请求,要求第三方程序响应。第三方程序响应完后必须调用sendRsp返回。在调用sendRsp返回时,会切回到微信终端程序界面。
}
func onResp(_ resp: BaseResp) { //如果第三方程序向微信发送了sendReq的请求,那么onResp会被回调。sendReq请求调用后,会切到微信终端程序界面。
print("wx:\(resp.errCode)")
}
private func application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: @escaping ([UIUserActivityRestoring]?) -> Void) -> Bool {
let rtn = WXApi.handleOpenUniversalLink(userActivity, delegate: self)
print("userActivity:\(rtn)")
return rtn
}
func application(_ application: UIApplication, handleOpen url: URL) -> Bool {
let rtn = WXApi.handleOpen(url, delegate: self)
print("handleOpen:\(rtn)")
return rtn
}
func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {
let urlKey: String = options[UIApplication.OpenURLOptionsKey.sourceApplication] as! String
print(urlKey)
return WXApi.handleOpen(url, delegate: self)
}
以上的代码,因为没有低版本的微信,所以没有测试,但七不离八,应该没问题。 以上不要忘记实现WXApiDelegate
下面就来看一下SceneDelegate的代码
class SceneDelegate: UIResponder, UIWindowSceneDelegate, WXApiDelegate {
//回调的入口,就在这里
@available(iOS 13.0, *)
func scene(_ scene: UIScene, continue userActivity: NSUserActivity) {
print(#function)
WXApi.handleOpenUniversalLink(userActivity, delegate: self)
}
func onReq(_ req: BaseReq) {
//onReq是微信终端向第三方程序发起请求,要求第三方程序响应。第三方程序响应完后必须调用sendRsp返回。在调用sendRsp返回时,会切回到微信终端程序界面。
}
//如果第三方程序向微信发送了sendReq的请求,那么onResp会被回调。sendReq请求调用后,会切到微信终端程序界面。
func onResp(_ resp: BaseResp) {
print("wx:\(resp.errCode)")
if resp.errCode == 0 {
print("微信支付回调成功")
}
}
//这里加载window,原来的window, 在iOS13中如何支持分屏,那么就在这里了。
@available(iOS 13.0, *)
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
window = UIWindow()
window?.windowScene = scene as? UIWindowScene
window?.frame = UIScreen.main.bounds
window?.rootViewController = ViewController()
window?.makeKeyAndVisible()
}
}
道法自然

浙公网安备 33010602011771号