項目涉及微信登錄、微信分享、微博分享、Facebook分享、Twitter分享。
使用 ShareSDK 的微信登錄方法之前,需配置微信App 的 Secret,然而登錄之后 ShareSDK 返回的 token 不是后臺想要的,導(dǎo)致調(diào)用后臺的登錄方法報錯。(ShareSDK 的微信登錄 跟 原生的微信登錄方法返回的 token 不同)
所以我只能用原生的微信登錄方法,但是在集成了 ShareSDK 的情況下調(diào)用微信登錄之后,死活不走 WXApiDelegate(未集成 ShareSDK 之前會走 WXApiDelegate)。
由于 Twitter 分享的文檔都是英文,F(xiàn)acebook 分享之前也沒做過,我懶得一個個去搞所以就換成了友盟分享,然而在集成了友盟分享的情況下,調(diào)用原生的微信登錄方法之后也是不走 WXApiDelegate。然后我看了下微信SDK源碼之后換了一個方法:
After:
//調(diào)用原生的微信登錄接口
let req = SendAuthReq.init()
req.scope = "snsapi_userinfo"
req.state = "123"
WXApi.sendAuthReq(req, viewController: nil, delegate: self)
Befor:
//調(diào)用原生的微信登錄接口
let req = SendAuthReq.init()
req.scope = "snsapi_userinfo"
req.state = "123"
WXApi.sendReq(req)
并且在登錄界面實現(xiàn)了微信代理方法:
// MARK: 微信代理方法
extension GMLoginView: WXApiDelegate {
func onResp(_ resp: BaseResp!) {
print("-->> 微信代理方法")
}
}
這時終于走了 AppDelegate 的 WXApiDelegate。使用友盟分享SDK實現(xiàn)了微信分享、微博分享、Facebook分享、Twitter分享功能,使用原生的微信登錄接口了實現(xiàn)后臺登錄。
只在集成友盟分享SDK的時候,同時集成微信SDK即可。
微信分享 和 微信登錄的回調(diào)都在 AppDelegate 的 WXApiDelegate 中處理。
雖然不走 GMLoginView 的 WXApiDelegate ,但是這段代碼卻不得不寫,否則就不走 AppDelegate 的 WXApiDelegate,不知道為什么會這樣。
集成友盟分享SDK時的 AppDelegate.swift
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
self.configThirdSDK(launchOptions: launchOptions)
return true
}
func configThirdSDK(launchOptions: [UIApplicationLaunchOptionsKey: Any]?) {
//配置友盟分享
UMSocialManager.default().openLog(true)
UMSocialManager.default().umSocialAppkey = GMConst.kYoumengAppID
UMSocialManager.default().setPlaform(.wechatSession, appKey: GMConst.kWechatAppID, appSecret: nil, redirectURL: nil)
UMSocialManager.default().setPlaform(.sina, appKey: GMConst.kSinaAppID, appSecret: GMConst.kSinaSecret, redirectURL: nil)
UMSocialManager.default().setPlaform(.twitter, appKey: GMConst.kTwitterID, appSecret: GMConst.kTwitterSecret, redirectURL: nil)
UMSocialManager.default().setPlaform(.facebook, appKey: GMConst.kFacebookID, appSecret: nil, redirectURL: nil)
//配置原生微信API
WXApi.registerApp(GMConst.kWechatAppID, enableMTA: true)
}
// MARK: 設(shè)置系統(tǒng)回調(diào)
func application(_ application: UIApplication, open url: URL, sourceApplication: String?, annotation: Any) -> Bool {
let absoluteString = url.absoluteString
if absoluteString.range(of: GMConst.kWechatAppID) != nil {
return WXApi.handleOpen(url, delegate: self as WXApiDelegate)
} else {
return UMSocialManager.default().handleOpen(url, sourceApplication: sourceApplication, annotation: annotation)
}
}
func application(_ application: UIApplication, handleOpen url: URL) -> Bool {
//代碼同上一個方法
}
}
// MARK: 微信代理方法
extension AppDelegate: WXApiDelegate {
func onResp(_ resp: BaseResp!) {
if resp is SendMessageToWXResp { //微信分享
if resp.errCode == 0 {//分享成功
GMHudUtils.showHUD(title: "分享成功")
} else if resp.errCode == -2 {
GMHudUtils.showHUD(title: "分享失敗")
}
} else if resp is SendAuthResp{ //微信登錄
self.loginStatus = LoginStatus.none
if resp.errCode == 0 {//登錄成功
//調(diào)用后臺的登錄接口
} else {
GMHudUtils.showHUD(title: "登錄失敗")
}
}
}
}
GMLoginView.swift
class GMLoginView: UIView {
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
override func awakeFromNib() {
}
@IBAction func loginBtnAction(_ sender: UIButton) {
HUD.flash(.label("登錄中..."), delay: 999) { _ in }
//調(diào)用微信原生的登錄接口
let req = SendAuthReq.init()
req.scope = "snsapi_userinfo"
req.state = "123"
WXApi.sendAuthReq(req, viewController: nil, delegate: self)
}
}
// MARK: 微信代理方法
extension GMLoginView: WXApiDelegate {
func onResp(_ resp: BaseResp!) {
print("-->> 微信代理方法")
}
}