如何让用户从 twitter fabric 注销 ios
How to make User logout from twitter fabric ios
我已经使用 Twitter 的结构制作了一个基本应用程序,允许用户在我的应用程序中发推文并提供登录 Twitter.Every 一切如我所愿。
How my app Works
如果用户没有登录到 Twitter,我的应用程序允许他登录,如果他已经登录,则直接允许他发推文。
Now the Big Part Comes
正如我在许多应用程序中看到的那样,我可以从 app.And 中删除我的签名帐户,但我无法获得任何帮助我实现此目的的方法。我想允许用户在 he/She 需要时从我的应用程序中的 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]);
}
}];
}
更新:2016 年 5 月 - 框架已更改,因此此答案不再相关。
看到这个答案:
这个答案:
相反。
[[[Twitter sharedInstance] sessionStore] logOutUserID:USERID];
这是 Foundation 框架中 Cookie 的问题,我借助以下代码解决了这个问题
NSURL *url = [NSURL URLWithString:@"https://api.twitter.com"];
NSArray *cookies = [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookiesForURL:url];
for (NSHTTPCookie *cookie in cookies)
{
[[NSHTTPCookieStorage sharedHTTPCookieStorage] deleteCookie:cookie];
}
使用如下:
[TWTRSessionStore logout]
已弃用:
[Twitter logOut]
已弃用。
鼓励用户调用 - [TWTRSessionStore logout]
而不是直接在 Twitter
实例上调用此方法。
您需要使用以下代码
[[[Twitter sharedInstance] sessionStore] logOutUserID:USERID];
提供您要注销的用户的用户 ID。
首先确保某个用户已登录,然后执行注销。
NSString *signedInUserID = [TWTRAPIClient clientWithCurrentUser].userID;
if (signedInUserID) {
[[Twitter sharedInstance].sessionStore logoutUserID:signedInUserID];
}
对于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)
从 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)
}
这是Swift的最佳简单答案 3:
let store = Twitter.sharedInstance().sessionStore
if let userID = store.session()?.userID {
store.logOutUserID(userID)
}
或者您可以使用 Naeem 的答案,但在 store.session
之后添加 ()
虽然登录提到的方法是 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)
}
我已经使用 Twitter 的结构制作了一个基本应用程序,允许用户在我的应用程序中发推文并提供登录 Twitter.Every 一切如我所愿。
How my app Works
如果用户没有登录到 Twitter,我的应用程序允许他登录,如果他已经登录,则直接允许他发推文。
Now the Big Part Comes
正如我在许多应用程序中看到的那样,我可以从 app.And 中删除我的签名帐户,但我无法获得任何帮助我实现此目的的方法。我想允许用户在 he/She 需要时从我的应用程序中的 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]);
}
}];
}
更新:2016 年 5 月 - 框架已更改,因此此答案不再相关。
看到这个答案:
[[[Twitter sharedInstance] sessionStore] logOutUserID:USERID];
这是 Foundation 框架中 Cookie 的问题,我借助以下代码解决了这个问题
NSURL *url = [NSURL URLWithString:@"https://api.twitter.com"];
NSArray *cookies = [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookiesForURL:url];
for (NSHTTPCookie *cookie in cookies)
{
[[NSHTTPCookieStorage sharedHTTPCookieStorage] deleteCookie:cookie];
}
使用如下:
[TWTRSessionStore logout]
已弃用:
[Twitter logOut]
已弃用。
鼓励用户调用 - [TWTRSessionStore logout]
而不是直接在 Twitter
实例上调用此方法。
您需要使用以下代码
[[[Twitter sharedInstance] sessionStore] logOutUserID:USERID];
提供您要注销的用户的用户 ID。
首先确保某个用户已登录,然后执行注销。
NSString *signedInUserID = [TWTRAPIClient clientWithCurrentUser].userID;
if (signedInUserID) {
[[Twitter sharedInstance].sessionStore logoutUserID:signedInUserID];
}
对于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)
从 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)
}
这是Swift的最佳简单答案 3:
let store = Twitter.sharedInstance().sessionStore
if let userID = store.session()?.userID {
store.logOutUserID(userID)
}
或者您可以使用 Naeem 的答案,但在 store.session
之后添加 ()虽然登录提到的方法是 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)
}