如何让用户从Twitter Fabric iOS登出

8
我使用 Twitter 的 Fabric 创建了一个基本应用程序,允许用户在我的应用程序中发布推文和使用 Twitter 登录。一切都按我想要的方式运作。
我的应用程序如何工作:
如果用户未登录 Twitter,则我的应用程序允许他登录;如果用户已经登录,则直接允许他发布推文。
现在关键的部分来了:
正如我在许多应用程序中看到的那样,我可以从应用程序中删除已签名的帐户,但是我找不到任何可以帮助我实现这一点的方法。我想允许用户在应用程序内随时从 Twitter 注销。
我搜索过谷歌,但没有找到任何有用的信息。
以下是我的代码:
- (IBAction)LogOut:(id)sender
{
    [[Twitter sharedInstance]logOut];
}

- (IBAction)LogMeIn:(id)sender
{
[[Twitter sharedInstance] logInWithCompletion:^
 (TWTRSession *session, NSError *error) {
     if (session) {
         NSLog(@"signed in as %@", [session userName]);
         [LoginButton setTitle:@"LogOut" forState:normal];

     } else {
         NSLog(@"error: %@", [error localizedDescription]);
     }
 }];
}

1
在Swift 4.2中,根据Dory Daniel的回答,可以使用以下代码: let store = Twitter.sharedInstance().sessionStore if let userID = store.session()?.userID { store.logOutUserID(userID) } - Naresh
9个回答

14

这是使用Foundation框架中的NSCookie时遇到的问题,我通过以下代码解决了这个问题。

NSURL *url = [NSURL URLWithString:@"https://api.twitter.com"];
NSArray *cookies = [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookiesForURL:url];
for (NSHTTPCookie *cookie in cookies)
{
    [[NSHTTPCookieStorage sharedHTTPCookieStorage] deleteCookie:cookie];
}

我不确定为什么这个答案只有2个赞。这是我发现的唯一可行的解决办法,可以成功地使用fabric退出Twitter。非常感谢,这真的很棒。 - Will Von Ullrich
1
我使用了这段代码,但仍然无法切换用户。我将其放在以下代码之后:[[Twitter sharedInstance] logOut];有任何想法吗? - Héctor
请检查系统设置的 Twitter 账户是否已注销。如果没有任何用户登录,则应该正常工作。 - Hiren kanetiya
这在之前对我来说是可行的,但现在似乎在iOS 8.1上不再起作用了。我不确定它是否从未在iOS 8.1上工作过,或者因为该应用程序现在使用iOS 10 SDK和更新的Twitter SDK构建,其他事情是否发生了变化... - xaphod
在 [[Twitter sharedInstance] logOut] 之后,这段代码应该放在哪里使用? - Harminder Singh

13

你需要使用以下代码

[[[Twitter sharedInstance] sessionStore] logOutUserID:USERID];

提供您想注销的用户的用户ID。


由于[[Twitter sharedInstance] logOut];即将被弃用,我认为这应该是被接受的答案。 - orenk86

9

我该怎么调用它呢?文档只给了我 -(void)logout,但没有展示如何使用。 - Dalvik
Twitter对象是单例模式。在您的代码中调用[[Twitter sharedInstance] logOut];。 - SeanCAtkinson
当我运行这段代码时,什么也没有发生...在注销后,当我按下登录按钮时,它显示我已经登录。 - Dalvik
不,文档明确表示错误将是非零的,而不是会话对象。以下是关于会话对象的说明:http://i.imgur.com/aPXVWoO.png - SeanCAtkinson
很遗憾,退出方法似乎并没有真正将用户退出。 - Daniel Roberts
显示剩余5条评论

8

以下是关于 Swift 3 的最佳简明答案:

let store = Twitter.sharedInstance().sessionStore
        if let userID = store.session()?.userID {
            store.logOutUserID(userID)
        }

或者您可以使用Naeem的答案,但在store.session后面添加()。

3

来自Twitter文档的注销代码:

Objective-C

TWTRSessionStore *store = [[Twitter sharedInstance] sessionStore];
NSString *userID = store.session.userID;

[store logOutUserID:userID];

Swift

let store = Twitter.sharedInstance().sessionStore

if let userID = store.session()?.userID {
  store.logOutUserID(userID)
}

2
尝试这个,对于 Swift :
/**
 *  Deletes the local Twitter user session from this app. This will not remove the system Twitter account nor make a network request to invalidate the session.
 *
 *  @param userID ID of the user to log out
 */
Twitter.sharedInstance().sessionStore.logOutUserID(userId)

0
在登录时,将方法设置为webBasedForceLogin,这样就不会在Safari缓存中创建条目。
private func twitterLogin() {
    Twitter.sharedInstance().logIn(withMethods: .webBasedForceLogin, completion: { (session, error) in
        if let error = error {
            print(error.localizedDescription)
        }

        guard let session = session else {
            return
        }

        print("success! Welcome \(session.userName).")
        self.twitterButton.setTitle("TWITTER LOGOUT", for: .normal)
    })
}

private func twitterLogout() {
    let sessionStore = Twitter.sharedInstance().sessionStore
    if let userID = sessionStore.session()?.userID {
        sessionStore.logOutUserID(userID)
    }

    twitterButton.setTitle("TWITTER LOGIN", for: .normal)
}

1
logInwithMethods: 在 Twitter kit 3.0 中不可用。 - ARS

0

首先确保有用户已登录,然后执行注销操作。

NSString *signedInUserID = [TWTRAPIClient clientWithCurrentUser].userID;
if (signedInUserID) {
   [[Twitter sharedInstance].sessionStore logoutUserID:signedInUserID];
}

-2

使用以下代码:

[TWTRSessionStore logout]

已弃用:

[Twitter logOut] 

已过时。

建议用户调用[TWTRSessionStore logout]而不是直接在Twitter实例上调用此方法。


似乎 TWTRSessionStore 没有 logout 方法,你有文档链接吗? - zevarito
[[[Twitter sharedInstance] sessionStore] logOutUserID:@"xxxxxx"]: - JAHelia

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接