I need to delete simulator cookies whenever needed. I need to delete the Facebook cookie from the iOS simulator.
I tried the following code, and it does not works.
- (IBAction)Logout:(id)sender {
NSHTTPCookieStorage *cookieStorage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
for (NSHTTPCookie *each in [[cookieStorage cookiesForURL:#"Facebook.com"] copy ]) {
[cookieStorage deleteCookie:each];
}
}
Related
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];
}
}
I am integrated Instagram in iPhone,At first time login page display on my iPhone and after logout when again login than credentials are not Asking and directly login. i also 'nil' old credentials on logout and i also clear cookie at logout but it not working.when i delete cookie from safari then it work fine. how to solve it programetically.
Logout Button code as below
-(void)doLogout
{
IGAppDelegate* appDelegate = (IGAppDelegate*)[UIApplication sharedApplication].delegate;
[cookies deleteCookie:cookie];
// clear cookie
NSHTTPCookieStorage* cookies = [NSHTTPCookieStorage sharedHTTPCookieStorage];
NSArray* instagramCookies = [cookies cookiesForURL:[NSURL URLWithString:#"https://instagram.com/"]];
NSLog(#"Array is == %#",instagramCookies);
for (NSHTTPCookie* cookie in instagramCookies)
{
[cookies deleteCookie:cookie];
}
// accessToken set nil
[[NSUserDefaults standardUserDefaults] setObject:nil forKey:#"accessToken"];
[[NSUserDefaults standardUserDefaults] synchronize];
[self.navigationController popViewControllerAnimated:YES];
}
We have to use WebView instead of safari so this problem will be solved.
I have made some changes in crino's sdk in which this problem has been resolved.
You can get code from https://github.com/gouravgupta72/instagramIOSsdk/ here.
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
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.