Which analog this function TwitterCore.getInstance().getSessionManager().clearActiveSession() on iOS?

ios
oauth
swift

#1

Hello! I try to regenerate a regenerating user token for our app. Android team made it with help of TwitterCore.getInstance().getSessionManager().clearActiveSession();
TwitterCore.getInstance().logOut(); . How can I do it on iOS? Please, help me.

My code

func auth(userEmail: String?, successLoginCallback: ((usr : User) -> Void)?, failLoginCallback: ( (message: String) -> Void )?) {
    Twitter.sharedInstance().sessionStore.reloadSessionStore()
    Twitter.sharedInstance().startWithConsumerKey(twitterKey, consumerSecret: twitterSecret)
    Twitter.sharedInstance().logInWithCompletion { (session, error) in
        if session != nil {
            TWTRAPIClient.clientWithCurrentUser().requestEmailForCurrentUser({ (twitterEmail, error) in
                // oauth parameters
                let oauthSession = TWTRSession(authToken: session!.authToken, authTokenSecret: session!.authTokenSecret, userName: session!.userName, userID: session!.userID)

                let oauthSigning = TWTROAuthSigning(authConfig: Twitter.sharedInstance().authConfig, authSession: oauthSession)
                let headers = oauthSigning.OAuthEchoHeadersToVerifyCredentials()
                guard let authHeaders = headers as? [String : AnyObject] else {
                    return
                }
                var passEmail: String?
                if userEmail != nil {
                    passEmail = userEmail
                }
                UserManager.sharedManager.logInViaTwitter(passEmail, parameters: authHeaders, success: { (user) in
                    self.userIsLogginedViaTwitter = true
                    self.twitterSuccessLogin(user)
                    print("user token", user.token?.expr)
                    successLoginCallback?(usr: user)
                    }, fail: { (errorMessage) in
                        if errorMessage != nil {
                            self.twitterFailsLogin(errorMessage!)
                            failLoginCallback?(message: errorMessage!)
                        }
                })
            })
        }
        if error != nil {
            print(error?.localizedDescription)
        }
    }
}

 func logOut(success: (() -> ())?) {
        Twitter.sharedInstance().startWithConsumerKey(twitterKey, consumerSecret: twitterSecret)
        guard let userID = Twitter.sharedInstance().sessionStore.session()?.userID else { return }
        Twitter.sharedInstance().sessionStore.logOutUserID(userID)
        userIsLogginedViaTwitter = false
        let cookieStorage: NSHTTPCookieStorage = NSHTTPCookieStorage.sharedHTTPCookieStorage()
        cookieStorage.cookies?.forEach({ (cook) in
            cookieStorage.deleteCookie(cook)
        })
        success?()
    }



 // MARK: - Twitter functions 
    func logInViaTwitter(email: String?, parameters: [String : AnyObject], success: ((user: User) -> ())?, fail: ((errorMessage: String?) -> ())?) {
        let url = "\(Networking.baseUrl)/auth/login/tw"
        guard let oauth_url = parameters["X-Auth-Service-Provider"] as? String else { return }
        guard let oauth_params = parameters["X-Verify-Credentials-Authorization"] as? String else { return }
        var passParameters = [String : AnyObject]()
        if email != nil {
            passParameters = ["email" : email!, "oauth_url" : oauth_url, "oauth_params": oauth_params]
        } else {
            passParameters = ["oauth_url" : oauth_url, "oauth_params": oauth_params]
        }
        print("Twitter manager passParameters", passParameters, "Twitter manager passParameters")

        Networking.manager.request(.POST, url, parameters: passParameters, encoding: .JSON, headers: nil).responseObject { (response: Response<RegisterResponse, NSError>) in
            guard let result = response.result.value else {
                fail?(errorMessage: nil)
                return
            }

            if result.meta?.errors.count == 0 {
                print(result.response)
                guard let user = Mapper<User>().map(result.response) else {
                    fail?(errorMessage: nil)
                    return
                }
                // Save state in Twitter manager
                TwitterManager.sharedManager.userIsLogginedViaTwitter = true
                self.saveCurrentUser(user)
                success?(user: user)
            } else {
                fail?(errorMessage: result.meta?.errors.first?.message)
            }

        }
    }

Twitter OAuth on iOS
#2

Hey @skysanchezsky,

If you’re interested in managing user sessions on iOS, please take a look at this documentation: https://docs.fabric.io/apple/twitter/log-in-with-twitter.html#manage-sessions

-Mike


#3

Thank you so much


#4

Happy to help and happy coding :slight_smile:


#5