How to detect that user has revoked access in iOS app

ios
twitterkit

#1

Hi,

This is my pod configuration:
pod ‘TwitterKit’, ‘3.3.0’
pod ‘TwitterCore’, ‘3.1.0’

My code to detect there is a user logged to compose Tweets:

if TWTRTwitter.sharedInstance().sessionStore.hasLoggedInUsers() {
            self.presentTwitterComposeView()
        } else {
            TwitterNetworkManager.instance.logIn({
                print("logged")
                self.presentTwitterComposeView()
            }, failure: { (error) in
                print("not logged: \(error.debugDescription)")
                let alert = UIAlertController(title: "Message_Title_Not_Twitter_Account".localized(), message: "Message_Text_Not_Twitter_Account".localized(), preferredStyle: .alert)
                 alert.addAction(UIAlertAction(title: "Message_ok".localized(), style: UIAlertActionStyle.default, handler: nil))
                self.present(alert, animated: false, completion: nil)
            })
        }

The code works perfect but once the user revoke the access to the app, the compose appear and when user send a pop up appear:

Tweet failed to send

Is there any way to detect the user has revoked the app to launch the login process?

Thank you


#2

There’s no built-in method for this in Twitter Kit itself, but you could call the account/verify_credentials endpoint using the ApiClient to check whether the user’s tokens are valid - that would tell you if the user had revoked the app or not.


#3

Thanks for your answer @andypiper
That’s is I need.

Could you show me how to call account/verify_credentials endpoint? Maybe exist some example or a link to the documentation.


#4

The API documentation is here, I’d suggest using the API Client in Twitter Kit to call it.


#5

Thank you so much.
Right now is working.

I’m verifying the account with this method:

let userID = TWTRTwitter.sharedInstance().sessionStore.session()?.userID
        
let client = TWTRAPIClient(userID: userID)
        
var clientError : NSError?
        
let request = client.urlRequest(withMethod: "GET", urlString: "https://api.twitter.com/1.1/account/verify_credentials.json", parameters: nil, error: &clientError)
        
client.sendTwitterRequest(request) { (response, data, error) in
            
            if error != nil {
                failure(error)
            }else{
                success()
            }
}