I implemented Facebook Graph API in my iPhone App and I successfully post it in a wall. But each time I end my application and come back it stores my credentials (in Safari I guess as a cookie). It asks my permission with my previous credentials. But at this point I want my Facebook API should prompt for a new username and password login for requesting permission. In simple I want to logout of my Facebook when i select a button in my App.
fbGraph.accessToken = nil;
NSHTTPCookie *cookie;
NSHTTPCookieStorage *storage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
for (cookie in [storage cookies])
{
NSString *domainName = [cookie domain];
NSRange domainRange = [domainName rangeOfString:#"facebook"];
if(domainRange.length > 0)
{
[storage deleteCookie:cookie];
}
}
How did you log in to facebook in the first place? If you are using facebook iOS SDK, you just basically have to call [facebook logout:self];. Otherwise if you're implementing Graph API yourself you just need to clear out your cookies and delete your access token.
when you are call the facebook button at that method last line u wil write this method [facebook logout]
NSHTTPCookieStorage* cookies = [NSHTTPCookieStorage sharedHTTPCookieStorage];
for (NSHTTPCookie* cookie in [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies])
{
[cookies deleteCookie:cookie];
}
These codes are enough to implement logout functionality.
Its working perfectly on my app.
Related
The problem is that Facebook use Safari when logged in, so when I try logout from my app it does keep credentials and cookies of Safari.
In other words, the only way to logout correctly is to open Safari and logout from Facebook.
My question is, is there any work around this way?
FBSDKLoginManager *loginManager = [FBSDKLoginManager new];
[loginManager logOut];
I also tried:
[FBSDKAccessToken setCurrentAccessToken:nil];
[FBSDKProfile setCurrentProfile:nil];
Although they are being called inside the logout method :(
Finally I tried removing cookies
NSHTTPCookieStorage* cookies = [NSHTTPCookieStorage sharedHTTPCookieStorage];
NSArray* facebookCookies = [cookies cookiesForURL:
[NSURL URLWithString:#"http://login.facebook.com"]];
for (NSHTTPCookie* cookie in facebookCookies) {
[cookies deleteCookie:cookie];
}
No luck at all!
I am using Fabric SDK for Twitter login into my app.
I want to remove session and cookie of Twitter from my iOS App. Because I am logging 1st time successfully from Twitter using this SDK. But if user wants to login from other credentials using Twitter into my app, then its not possible without clear session.
For clear session of Twitter, I am using following Code. But its not working.
[[Twitter sharedInstance]logOut];
[[Twitter sharedInstance]logOutGuest];
NSHTTPCookieStorage *cookieList = [NSHTTPCookieStorage sharedHTTPCookieStorage];
for (NSHTTPCookie *x in cookieList.cookies)
{
if ([[x valueForKey:#"domain"] isEqualToString:#".twitter.com"])
{
[cookieList deleteCookie:x];
}
}
Look forward to hearing your responses!
Try adding this to remove cookies
let cookie = NSHTTPCookie.self
let cookieStorage = NSHTTPCookieStorage.sharedHTTPCookieStorage()
for cookie in cookieStorage.cookies!
{
cookieStorage.deleteCookie(cookie)
}
In Objective-C
NSHTTPCookieStorage *cookieStorage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
for (NSHTTPCookie *httpCookie in cookieStorage.cookies)
{
if ([[httpCookie valueForKey:#"domain"] isEqualToString:#".twitter.com"])
{
[cookieStorage deleteCookie:httpCookie];
}
}
Did you use breakpoints in order to know if it is entering inside de if condition?
Btw, you have to change the code, you can't delete an object from the array while you are enumerating it. Maybe you can use another variable to persist the cookie when you find it and remove it out of the forin loop
// EDIT
NSHTTPCookieStorage *cookieList = [NSHTTPCookieStorage sharedHTTPCookieStorage];
NSMutableArray *cookies = [[NSMutableArray alloc] init];
for (NSHTTPCookie *x in cookieList.cookies)
{
if ([[x valueForKey:#"domain"] isEqualToString:#".twitter.com"])
{
[cookies addObject:x];
}
}
if (cookies.count > 0) {
for (NSHTTPCookie *cookie in cookies) {
[cookieList deleteCookie:cookie];
}
}
I created an array because I don't know how many cookies persist from twitter
Shared Instance Logout does not work. I know this is a big problem for developers. Does anyone have a solution? Thanks
I ran into this issue about 3 months ago and have just now found a solution to this. Apparently clearing cookies does in fact get rid of Twitter's stored information for previously logged in users. The code below is working for me:
!WARNING! Make sure when you first log in that you Disallow Twitter from accessing your accounts on the device. This causes Twitter to force login the user each time instead of looking straight at your Twitter account saved to your device. Hope this helps!
- (IBAction)twitterLogout:(id)sender {
[[Twitter sharedInstance] logOut];
[self.view insertSubview:_logoutTwitter atIndex:16];
NSHTTPCookie *cookie;
NSHTTPCookieStorage *storage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
for (cookie in [storage cookies])
{
NSString* domainName = [cookie domain];
NSRange domainRange = [domainName rangeOfString:#"Twitter"];
if(domainRange.length > 0)
{
[storage deleteCookie:cookie];
}
}
NSURL *url = [NSURL URLWithString:#"https://api.twitter.com"];
NSArray *cookies = [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookiesForURL:url];
for (NSHTTPCookie *cookie in cookies)
{
[[NSHTTPCookieStorage sharedHTTPCookieStorage] deleteCookie:cookie];
}
}
The Facebook SDK for iOS come with some sample applications. E.g. FriendPickerSample and SessionLoginSample. In both when I try to login it will remember my previous facebook users. But I don't want that because I want to use it with another facebook user. How do I clear the info Facebook has cached so that login will act the same as the very first time I tried to login on this device with this particular app?
Does facebook cache all its information in the app bundled? And what info does it store and in what files?
You need to remove all the keys stored by facebook once user logs out.
- (void)fbDidLogout
{
NSLog(#"Logged out of facebook");
NSHTTPCookie *cookie;
NSHTTPCookieStorage *storage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
for (cookie in [storage cookies])
{
NSString* domainName = [cookie domain];
NSRange domainRange = [domainName rangeOfString:#"facebook"];
if(domainRange.length > 0)
{
[storage deleteCookie:cookie];
}
}
}
[[FBSession activeSession] close];
[[FBSession activeSession] closeAndClearTokenInformation];
We have got sruck in the iOS facebook login logout issue. When I login to facebook using my application it will prompt for user permission with 'login' and 'cancel' button. But this screen appears only on the very first time. ie Once we logged in using safari or the app and even if we logged out from facebook , application the screen prompting for user permission displays only an 'ok' button. It doesnt allow to sign in as a different user. Why the screen with 'login' and 'cancel' button not displaying each time the application launches? I tried by deleting cookies and removing NSUserDefaults but no luck.
The problem is after logout, I am unable to login to the facebook as another user. It still shows as the same user.
I am calling the below logout function in sdk
(void)logout:(id<FBSessionDelegate>)delegate {
self.sessionDelegate = delegate;
[_accessToken release];
_accessToken = nil;
[_expirationDate release];
_expirationDate = nil;
NSHTTPCookieStorage* cookies = [NSHTTPCookieStorage sharedHTTPCookieStorage];
NSArray* facebookCookies = [cookies cookiesForURL:
[NSURL URLWithString:#"http://login.facebook.com"]];
for (NSHTTPCookie* cookie in facebookCookies) {
[cookies deleteCookie:cookie];
}
if ([self.sessionDelegate respondsToSelector:#selector(fbDidLogout)]) {
[_sessionDelegate fbDidLogout];
}
}
Also in fbDidLogout delegate function I removed all NSUserDefaults objects
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if ([defaults objectForKey:#"FBAccessTokenKey"]) {
[defaults removeObjectForKey:#"FBAccessTokenKey"];
[defaults removeObjectForKey:#"FBExpirationDateKey"];
[defaults synchronize];
}
regrds
Shihab
You can clear the session as well as clearing the cookies with the following code:
FBSession* session = [FBSession activeSession];
[session closeAndClearTokenInformation];
[session close];
[FBSession setActiveSession:nil];
NSHTTPCookieStorage* cookies = [NSHTTPCookieStorage sharedHTTPCookieStorage];
NSArray* facebookCookies = [cookies cookiesForURL:[NSURL URLWithString:#"https://facebook.com/"]];
for (NSHTTPCookie* cookie in facebookCookies) {
[cookies deleteCookie:cookie];
}
FBSession openWithBehavior:completionHandler: can be used..
FBSession *fbSession = [[FBSession alloc] initWithPermissions:[NSArray arrayWithObjects:#"email",#"publish_actions",#"publish_stream", nil]];
[fbSession openWithBehavior:FBSessionLoginBehaviorForcingWebView completionHandler:^(FBSession *session,FBSessionState state, NSError *error){
[FBSession setActiveSession:fbSession]; // Retain the Active Session.
}];
For Logging out, Ans by Ellen S.. worked fine for iOS .
I modified fbDidLogout method and it worked, here is the code:
-(void) fbDidLogout
{
NSLog(#"Logged out of facebook");
NSHTTPCookie *cookie;
NSHTTPCookieStorage *storage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
for (cookie in [storage cookies])
{
NSString* domainName = [cookie domain];
NSRange domainRange = [domainName rangeOfString:#"facebook"];
if(domainRange.length > 0)
{
[storage deleteCookie:cookie];
}
}
}//End of Method
The method successfully logs out the user.
Hope this will help!
I just figure it out I got in the settings of my iPhone and got to privacy chose the Facebook tab and turn off where it says Applications that have requested access to you Facebook account will appear here. It works!!!
When login to set to loginBehavior, so when you exit, with the other account login, won't appear only authorized, without the login screen login.loginBehavior =FBSDKLoginBehaviorWeb; i use facebook 4.11,it's work