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() }
}