简体   繁体   English

有没有一种方法可以检查用户是否使用Twitter登录?

[英]Is there a way to check if a user is signed in with Twitter?

I am following this tutorial 我正在关注本教程

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

For this app, I'm doing a sign in with Twitter deal. 对于此应用程序,我正在使用Twitter交易登录。 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: 如果用户未登录Twitter(他们未通过设置登录),则此代码运行时将不显示任何内容:

[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? 有谁知道一种检查方法?

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 并检查设置| Twitter中是否有帐户

This is in Swift but you get the idea 这是在Swift中,但您明白了

import Social

if !SLComposeViewController.isAvailableForServiceType(SLServiceTypeTwitter){
        println("No Account Setup")
    } else {
        println("Yes Account Setup")
    }

Swift 4 斯威夫特4

I am using TwitterKit and installed it with cocoapod Twitterkit has sessionstore which stores session for the logged in users. 我正在使用TwitterKit并将其与cocoapod一起安装。Twitterkit具有sessionstore,用于存储已登录用户的会话。

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

For logout from twitter 从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)
        }

    }
}

Swift 3 迅捷3

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

Returns 返回

No logged in user: nil 没有登录用户:nil

Logged in user: TWTRSession 登录用户:TWTRSession

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM