参照微信开放平台进行项目设置
比起以往多的步骤就是设置universalLink,及apple-app-site-association的生成和上传,这个可以自行百度搜索结果还是很多的,记得文件不加后缀名就行了。
做好准备工作后在Appdelegate中进行注册及回调方法的处理
WXApi.registerApp("appkey", universalLink: "universalLink")
两个双引号内容根据自己项目填写
func application(_ application: UIApplication, handleOpen url: URL) -> Bool {
return WXApi.handleOpen(url, delegate: self)
}
func application(_ application: UIApplication, open url: URL, sourceApplication: String?, annotation: Any) -> Bool {
return WXApi.handleOpen(url, delegate: self)
}
func application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: @escaping ([UIUserActivityRestoring]?) -> Void) -> Bool {
return WXApi.handleOpenUniversalLink(userActivity, delegate: self)
}
//微信支付回调
func onResp(_ resp: BaseResp) {
print(resp)
if resp.isKind(of: SendAuthResp.self) {
let response = resp as! SendAuthResp
let code = response.code
if code != nil{
LoginAgent.requstWeixinLogin(code: code!)
}
}else if(resp.isKind(of: PayResp.self)){
let response = resp as! PayResp
switch (response.errCode) {
case 0://支付成功
PayManager.shared.requstPayResult()
break
default:
ShowManager.showMessage(text: response.errStr)
break
}
}
}
以上内容都是在Appdelegate中填写,如果项目中既有微信支付又有微信登录需要对回调结果进行区分
微信开放平台的文档说明了在SceneDelegate中重新以下方法
@available(iOS 13.0, *)
func scene(_ scene: UIScene, continue userActivity: NSUserActivity) {
WXApi.handleOpenUniversalLink(userActivity, delegate: self)
}
但这也只是保证了根据universalLink跳转到该应用的时候可以进入该代理方法,如果没用执行微信登录的回调则需要在SceneDelegate重写以下方法
@available(iOS 13.0, *)
func scene(_ scene: UIScene, openURLContexts URLContexts: Set) {
let context = URLContexts.first;
WXApi.handleOpen(context!.url, delegate: self)
}
同样还有微信的回调方法也要在SceneDelegate重写
//微信支付回调
func onResp(_ resp: BaseResp) {
print(resp)
if resp.isKind(of: SendAuthResp.self) {
let response = resp as! SendAuthResp
let code = response.code
if code != nil{
LoginAgent.requstWeixinLogin(code: code!)
}
}else if(resp.isKind(of: PayResp.self)){
let response = resp as! PayResp
switch (response.errCode) {
case 0://支付成功
PayManager.shared.requstPayResult()
break
default:
ShowManager.showMessage(text: response.errStr)
break
}
}
}
记得universalLink一定要设置和校验成功
另外附上调起微信登录的方法
let req = SendAuthReq()
req.scope = "snsapi_userinfo"
req.state = "123"
WXApi.send(req, completion: nil)
QQ与微信大致相同,但qq的文档上似乎差的更多些,还是首先参照文档进行集成SDK和基本的设置
记得tencentOAuth需声明为成员属性,不可为局部变量,否则任何回调都不会被触发,另外和微信一样也需要到开放平台设置
UniversalLink设置格式平台也有详细说只是藏的略深,基本格式就是https://域名/qq_conn/你的appkey
TencentOAuth的初始化方法也需要替换一下使用最新的方法
tencentOAuth = TencentOAuth.init(appId: "1107971571", andUniversalLink: "https://api.shichulian.com/qq_conn/1107971571", andDelegate: self)
接下来便是调起QQ
@objc func qqOnclick() {
let permissions = ["get_user_info","get_simple_userinfo","add_t"]
tencentOAuth.authorize(permissions, localAppId: "1107971571", inSafari: false)
}
官方文档要求在Appdelegate中重新以下两个方法
func application(_ application: UIApplication, handleOpen url: URL) -> Bool {
return TencentOAuth.handleOpen(url)
}
func application(_ application: UIApplication, open url: URL, sourceApplication: String?, annotation: Any) -> Bool {
return TencentOAuth.handleOpen(url)
}
但在我的项目中不管是iOS12 还是iOS13都没有走这两个方法,在12上会执行以下这个代理方法,所以我们可以在该方法里实现QQ的handleOpen方法,支付宝的支付回调也会到这个代理方法,如果项目中用到支付宝支付需要在方法里进行判断
func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {
if url.host == "safepay"{
PayManager.shared.alipayResult(url: url)
}else{
TencentOAuth.handleOpen(url)
}
return true
}
在iOS13中会执行SceneDelegate中的以下方法,所以需要在改方法里进行处理,如果你的项目中也有微信的登录功能也需要加上如下的判断
@available(iOS 13.0, *)
func scene(_ scene: UIScene, continue userActivity: NSUserActivity) {
if TencentOAuth.canHandleUniversalLink(userActivity.webpageURL) {
TencentOAuth.handleUniversalLink(userActivity.webpageURL)
}else{
WXApi.handleOpenUniversalLink(userActivity, delegate: self)
}
}
至此就会根据结果进入QQ登录对应的三个代理方法
func tencentDidLogin() {
}
func tencentDidNotLogin(_ cancelled: Bool) {
ShowManager.showMessage(text: "用户取消登录")
}
func tencentDidNotNetWork() {
ShowManager.showMessage(text: "请检查网络")
}