Is there a way to check if a user is signed in wit

2019-08-03 09:36发布

I am following this tutorial

https://parse.com/docs/ios_guide#twitterusers/iOS

For this app, I'm doing a sign in with Twitter deal. There's one problem. If a user is not signed into twitter (they didn't sign in from settings), nothing is displayed when this code runs:

[PFTwitterUtils logInWithBlock:^(PFUser *user, NSError *error) {
    if (!user) {
        NSLog(@"Uh oh. The user cancelled the Twitter login.");
        return;
    } else if (user.isNew) {
        NSLog(@"User signed up and logged in with Twitter!");
    } else {
        NSLog(@"User logged in with Twitter!");
    }    
}];

I am wondering if there is a way to check if a user is signed in from settings so I can set up a message saying they should sign in from settings. Does anyone know a way to check this?

3条回答
The star\"
2楼-- · 2019-08-03 10:04

You can just check if the service is available and if not then do whatever. And this is checking that there is an account in Settings|Twitter

This is in Swift but you get the idea

import Social

if !SLComposeViewController.isAvailableForServiceType(SLServiceTypeTwitter){
        println("No Account Setup")
    } else {
        println("Yes Account Setup")
    }
查看更多
【Aperson】
3楼-- · 2019-08-03 10:04

Swift 3

let twitterSession = Twitter.sharedInstance().sessionStore.session()

Returns

No logged in user: nil

Logged in user: TWTRSession

查看更多
欢心
4楼-- · 2019-08-03 10:15

Swift 4

I am using TwitterKit and installed it with cocoapod Twitterkit has sessionstore which stores session for the logged in users.

if TWTRTwitter.sharedInstance().sessionStore.session() != nil {
            print("user signed in with twitter ..")
}

For logout from twitter

func logoutTwitter() {
        let store = TWTRTwitter.sharedInstance().sessionStore
        if let userID = store.session()?.userID {
            store.logOutUserID(userID)
        }
}

This is how to get information about logged in user

func getTwitterUserInformation() {
    let store = TWTRTwitter.sharedInstance().sessionStore
    if let userID = store.session()?.userID {

        let client = TWTRAPIClient.withCurrentUser()
        client.loadUser(withID: userID) { (user, error) in
            print(user?.name)
            print(user?.screenName)
        }

    }
}
查看更多
登录 后发表回答