The project involves wechat login, wechat sharing, Weibo sharing, Facebook sharing and Twitter sharing.

Before using the Wechat login method of ShareSDK, you need to configure the Secret of wechat App. However, after login, the token returned by ShareSDK does not match the background. An error occurs when the login method in the background is invoked (the token returned by ShareSDK’S wechat login method is different from the original wechat login method).

So I can only use the native wechat login method, but after the integration of ShareSDK to log in using wechat, WXApiDelegate is not used (WXApiDelegate will be used before the integration of ShareSDK).

Since the documents shared by Twitter are all in English, and Facebook sharing has not been done before, I am too lazy to do it one by one, so I changed to Umeng sharing. However, in the case of integration of Umeng sharing, WXApiDelegate is not used after invoking the native wechat login method. Then I looked at the source code of wechat SDK and changed a method:

After:
// Invoke the native wechat login interfacelet req = SendAuthReq.init()
req.scope = "snsapi_userinfo"
req.state = "123"
WXApi.sendAuthReq(req, viewController: nil, delegate: self)
Copy the code
Befor:
// Invoke the native wechat login interfacelet req = SendAuthReq.init()
req.scope = "snsapi_userinfo"
req.state = "123"
WXApi.sendReq(req)
Copy the code

And the wechat agent method is realized in the login interface:

// Extension GMLoginView: WXApiDelegate {func onResp(_ resp: BaseResp!) {print("-->> wechat proxy method")}}Copy the code

Finally, WXApiDelegate for the AppDelegate. Using umeng sharing SDK to achieve wechat sharing, Weibo sharing, Facebook sharing, Twitter sharing functions, using the original wechat login interface to achieve background login.

Only when integrating umeng share SDK, integrate wechat SDK at the same time.

Wechat sharing and wechat login callbacks are handled in the AppDelegate WXApiDelegate.

While GMLoginView’s WXApiDelegate is not used, this code has to be written, otherwise WXApiDelegate is not used in AppDelegate.

Integrate app Delegate.swift when sharing SDK with friends

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]?) {// Configure 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: Gmconconst. KFacebookID, appSecret: nil, redirectURL: nil) // Configure the native wechat API wxapi.registerApp (gmconconst. KWechatAppID,enableMTA: trueFunc application(_ application: UIApplication, open url: url,sourceApplication: String? , annotation: Any) -> Bool {let absoluteString = url.absoluteString
        ifabsoluteString.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: }} // MARK: extension AppDelegate: WXApiDelegate {func onResp(_ resp: BaseResp!) {ifResp is SendMessageToWXResp {// wechat shareifResp.errcode == 0 {// Share successful gmhudutils.showhud (title:"Share the success")}else if resp.errCode == -2 { 
                GMHudUtils.showHUD(title: "Share failure")}}else ifResp is SendAuthResp{// wechat login self.loginStatus = loginStatus. NoneifResp. errCode == 0 {// Login succeeded // call the login interface in the background}else {
                GMHudUtils.showHUD(title: "Login failed")}}}}Copy the code

GMLoginView.swift

class GMLoginView: UIView { required init? (coder aDecoder: NSCoder) { super.init(coder: aDecoder) } override funcawakeFromNib() {        
 
    }
    
    @IBAction func loginBtnAction(_ sender: UIButton) {
        
        HUD.flash(.label("Logging in..."), delay: 999) { _ in} // Call the native login interface of wechatlet req = SendAuthReq.init()
        req.scope = "snsapi_userinfo"
        req.state = "123"Wxapi.sendauthreq (req, viewController: nil, delegate: self)}} WXApiDelegate { func onResp(_ resp: BaseResp!) {print("-->> wechat proxy method")}}Copy the code