如何使用户从Twitter结构ios注销
我做了一个基本的应用程序,使用Twitter的结构,允许用户在我的应用程序内鸣叫,并提供Twitter的login。每件事情都是我想要的。
我的应用如何工作
如果用户没有login到Twitter,我的应用程序允许他login,如果他是login,然后直接让他鸣叫。
现在大部分来了
正如我在许多应用程序中看到的,我可以从应用程序中删除我签名的帐户。而且我无法获得任何方法来帮助我实现这一目标。 我希望允许用户在他/她想要的时候从我的应用程序中的twitter中注销。
我GOOGLE了,但我没有find任何东西
这是我的代码:
- (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]); } }]; }
更新:2016年5月 – 框架已经改变,所以这个答案不再相关。
看到这个答案: https : //stackoverflow.com/a/35765833/940709和这个答案: https : //stackoverflow.com/a/30916904/940709改为。
[[[Twitter sharedInstance] sessionStore] logOutUserID:USERID];
这是来自基金会框架的NSCookie的一个问题我解决这个问题与下面的代码的帮助
NSURL *url = [NSURL URLWithString:@"https://api.twitter.com"]; NSArray *cookies = [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookiesForURL:url]; for (NSHTTPCookie *cookie in cookies) { [[NSHTTPCookieStorage sharedHTTPCookieStorage] deleteCookie:cookie]; }
你需要使用下面的代码
[[[Twitter sharedInstance] sessionStore] logOutUserID:USERID];
提供您要注销的用户的用户标识。
这是Swift 3最好的简单答案:
let store = Twitter.sharedInstance().sessionStore if let userID = store.session()?.userID { store.logOutUserID(userID) }
或者你可以使用Naeem的答案,但在store.session之后添加()
从Twitter注销代码文档:
Objective-C的
TWTRSessionStore *store = [[Twitter sharedInstance] sessionStore]; NSString *userID = store.session.userID; [store logOutUserID:userID];
迅速
let store = Twitter.sharedInstance().sessionStore if let userID = store.session?.userID { store.logOutUserID(userID) }
对于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)
首先确保某个用户已login,然后执行注销。
NSString *signedInUserID = [TWTRAPIClient clientWithCurrentUser].userID; if (signedInUserID) { [[Twitter sharedInstance].sessionStore logoutUserID:signedInUserID]; }
虽然login提到方法为webBasedForceLogin,所以它不会创build条目进入Safaricaching。
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) }
使用如下:
[TWTRSessionStore logout]
推荐使用:
[Twitter logOut]
已弃用。
鼓励用户调用 – [TWTRSessionStore logout]
而不是直接在Twitter
实例上调用此方法。