Unable To Logout of Facebook using Socialize SDK - iphone

I am using Socialize SDK in lieu of Sharekit to integrate mail, twitter and facebook in my app.
I only need to post some string on the user's profile which is working fine for facebook and twitter.
Following is the workflow:
User clicks on share , selects facebook/twitter.
If it is for the first time user is sharing, facebook/twitter login screen pops up
User Logs in and after authentication, share is successful.
If user shares for the second time, facebook/twitter login screen doesn't popsup for authentication and the share is successful.
If user wants to logout of facebook/twitter, he goes to the settings panel and clicks on twitter / facebook button to logout.
[When it goes back to share after log gin out, user clicks on twitter , login screen pops up but when user clicks on facebook, a shadow box appears for few seconds and disappears and user is logged in with the previous account. ]
How would i resolve this issue ?
I have tried using
[SocializeThirdPartyFacebook removeLocalCredentials] and also
[SZFacebookUtils unlink];
how should i go about it
I tried Clearing all the cache and cookies as well but still the same result
NSHTTPCookieStorage* cookies = [NSHTTPCookieStorage sharedHTTPCookieStorage];
NSArray* facebookCookies = [cookies cookiesForURL:
[NSURL URLWithString:#"http://login.facebook.com"]];
for (NSHTTPCookie* cookie in facebookCookies) {
NSLog(#"In For");
[cookies deleteCookie:cookie];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults removeObjectForKey:kSocializeFacebookAuthAppId];
[defaults removeObjectForKey:kSocializeFacebookAuthLocalAppId];
[defaults removeObjectForKey:kSocializeFacebookStringForAPI];
[defaults removeObjectForKey:kSocializeConsumerKey];
[defaults removeObjectForKey:kSocializeConsumerSecret];

Facebook and twitter SDK save access token in cookies.
So you have to clear all cache and cookies when you are trying to use logging mechanism in your code.

I solved it:
1.In the advance settings of the app on facebook, i enabled native/desktop app
2.Disabled SSO in basic settings
3.Added de-auth callback url in advance settings
4.Added the following piece of code:
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];
}
}

Related

Cookie from ios webview

I wanted to ask some things about a piece of code i'm trying to make without having any previous contact with ios or objective-c.
This piece of code will:
Open a WebView with a specific url where the user will login
(done)
After the user logs in it will take the cookie created
from that login.
It will use that cookie in a next request to
load another site that requires authentication.
I'm stuck a bit at part 2 because it has to a) wait in another thread till the user does the login (how?) and b) because i can't seem to get the specific cookie for the site easily. I have only found and tried this poc but how do I filter out only the cookie for the site I want?
NSHTTPCookie *cookie;
NSHTTPCookieStorage *cookieJar = [NSHTTPCookieStorage sharedHTTPCookieStorage];
for(cookie in [cookieJar cookies]) {
NSLog(#"%#", cookie);
}
Any ideas on how to make the 2a/b parts? Objective-c syntax seems a bit confusing.
I found a solution by following this https://www.inkling.com/read/learning-ios-programming-alasdair-allan-2nd/chapter-7/embedding-a-web-browser-in-your .
Used the webViewDidFinishLoad delegate and got the cookies from there with the cookiesForURL method.
NSArray* availableCookies = [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookiesForURL:[NSURL URLWithString:#"MYURL"]];
Cookies are created as follows,
NSDictionary *properties = [NSDictionary dictionaryWithObjectsAndKeys:
url, NSHTTPCookieOriginURL,
#"testCookies", NSHTTPCookieName,
#"1", NSHTTPCookieValue,
nil];
NSHTTPCookie *cookie = [NSHTTPCookie cookieWithProperties:properties];
You can use the cookie object to get the origin URL attribute and filter.
NSDictionary *cookieProperties = [cookie properties];
NSURL *originURL = [cookieProperties objectForKey:NSHTTPCookieOriginURL];
After filtering the cookie you need you can get the cookie value using,
cookie.value

logout from facebook completely

I have integrated facebook SDK and able to login to facebook. If user is enabled with facebook account on iOS settings then it ask for permission to acces in app otherwise it goes to safari for facebook login and come back to app. For logout I used following code:
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 closeAndClearTokenInformation];
But it is unable to logout completely. If I again click the login button it still showing access token as well as not moving to safari for facebook login again it means it has not logged out completely from facebook. I am unable to recognized the issue. If anyone know about this please help me out.
I would be very thankful to you.
Are you using version 3.x of the Facebook SDK for iOS? If so, the FBSession class should have a method called
-[FBSession closeAndClearTokenInformation]
Hopefully this will work for you.

fb ios sdk session for use with existing fb connect website

How does my fb connect webserver authenticate a user that logged in via fb ios sdk? I have a website which uses facebook connect. In it, i do use the app secret to authenticate the user via a cookie created by the facebook javascript sdk via the facebook python library:
def get_user_from_cookie(cookies, app_id, app_secret):
"""Parses the cookie set by the official Facebook JavaScript SDK.
cookies should be a dictionary-like object mapping cookie names to
cookie values.
If the user is logged in via Facebook, we return a dictionary with the
keys "uid" and "access_token". The former is the user's Facebook ID,
and the latter can be used to make authenticated requests to the Graph API.
If the user is not logged in, we return None.
Download the official Facebook JavaScript SDK at
http://github.com/facebook/connect-js/. Read more about Facebook
authentication at http://developers.facebook.com/docs/authentication/.
"""
cookie = cookies.get("fbs_" + app_id, "")
if not cookie: return None
args = dict((k, v[-1]) for k, v in cgi.parse_qs(cookie.strip('"')).items())
payload = "".join(k + "=" + args[k] for k in sorted(args.keys())
if k != "sig")
sig = hashlib.md5(payload + app_secret).hexdigest()
expires = int(args["expires"])
if sig == args.get("sig") and (expires == 0 or time.time() < expires):
return args
else:
return None
Now, I'm wondering how to connect a user who logs in via the iphone to my website. Do I just send the access token over to my webserver and based on the the access token make a call to the graph(just bypass the above function)? If that is it, then what about all the validation the above function offers?
The cookie fetch and validation is useful for Google AppEngine or JavaScript SDK access.
For accessing user session initiated in iOS app, in Python SDK, just use:
graph = facebook.GraphAPI(access_token)
user = graph.get_object("me")
Hope that helps.
If you want use Facebook iOS API you need:
Alloc and Init a Facebook object
Facebook *facebookObject = [[Facebook alloc] init];
Check permissions
NSArray *permissions = [NSArray arrayWithObjects:#"publish_stream", #"offline_access",nil];
Authorize facebook
[facebookObject authorize:APP_ID permissions:permissions delegate:self];
After that you can used NSUserDefaults to keep session like that :
To store session I use NSUserDefaults.
[[NSUserDefaults standardUserDefaults] setObject:facebookObject.accessToken forKey:#"AccessToken"];
[[NSUserDefaults standardUserDefaults] setObject:facebookObject.expirationDate forKey:#"ExpirationDate"];
[[NSUserDefaults standardUserDefaults] synchronize];
After that I can catch Access with :
facebookObject.accessToken = [[NSUserDefaults standardUserDefaults] stringForKey:#"AccessToken"];
facebookObject.expirationDate = (NSDate *) [[NSUserDefaults standardUserDefaults] objectForKey:#"ExpirationDate"];
Like that you can keep session alive and the user enter login and password only once. I think that is why cookies.

Facebook login screen flashing on iPhone app when already authenticated

I'm integrating facebook with my Application, and I want to give users the option to post a story to their FB wall from within my app. It's my understanding this requires users to 1. login/authorize and select 'publish' from a separate screen to post the story.
My goal is to have both these events occur when a user presses a button in my app. The only tiny glitch I'm noticing is that if the user has already logged in and authorized the app, the login screen flashes briefly before the 'publish story' screen. Is there any sort of property I can check to see if a user has already authorized the application to prevent the login screen from flashing? This is the code I'm working with right now:
if (facebook == nil)
{
facebook = [[Facebook alloc] init];
}
if (!facebook.accessToken)
{
[facebook authorize:#"###############" permissions:[NSArray arrayWithObject:#"publish_stream"] delegate:self];
}else
{
[self fbDidLogin];
}
Casey, this might help you:
The main issue I have found with that flashing login-screen had to do with the cookies Facebook stores in the iPhone.
What I did is:
1. Store the facebook access_token and expiration_date elsewhere (NSUserDefaults might be a good place).
This way, you can check if the user has already authenticated. If the user hasn't authenticated, then you can show the login screen.
2. Another thing that is important, is to know that Facebook will save some cookies as part of the authentication process. So, what happens is: the user authenticates and then turns off the app, then the next he opens it and taps login; Facebook will use its cookies, causing that annoying screen to appear and disappear.
You can try deleting the facebook cookies each time you want the user to login. That did the job for me.
Here you have a code snippet that removes the cookies.
-(void) deleteFacebookCookies{
NSHTTPCookieStorage* cookies = [NSHTTPCookieStorage sharedHTTPCookieStorage];
NSArray* facebookCookies = [cookies cookiesForURL:
[NSURL URLWithString:#"http://login.facebook.com"]];
for (NSHTTPCookie* cookie in facebookCookies) {
[cookies deleteCookie:cookie];
}
}
HTH, cheers!
If you use FBConnect you can just use [[FBSession session] isConnected], and [[FBSession session] logout, this is for old SDK, there are some changes in auth in current version.

iphone NSHTTPCookieStorage avaible on app reopen?

I am working on my app here- and it pretty much comes to this. I have a login box where a users log's in and then it saves the cookie data on return like so:
NSArray * all = [NSHTTPCookie cookiesWithResponseHeaderFields:[resp allHeaderFields] forURL:[NSURL URLWithString:#"http://myurl]];
NSHTTPCookieStorage *sharedHTTPCookieStorage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
[sharedHTTPCookieStorage setCookies:all forURL:[NSURL URLWithString:#"http://myurl"] mainDocumentURL:nil];
After it safes that cookie i take it to the home view - My problem is - if the users closes the prgoram, the phone restarts and so forth - are the cookies stored locally on the phone its sefl? I am trying to access that cookie again on the didFinishLaunchingWithOptions. I have the following code now..
NSHTTPCookieStorage *sharedHTTPCookieStorage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
NSArray *cookies = [sharedHTTPCookieStorage cookiesForURL:[NSURL URLWithString:#"http://iphone.wazgood.com"]];
NSLog(#"count: %i", [cookies count]);
Every time - it comes up empty on the cookie data - any ideas on if the cookies are cleared every time the user clsoes the program out - or is it bc im testing on the iPhone emulator?
In case you or anyone else is still having this problem, it could be that the cookies are set to expire when the session ends (when the app closes). You can check for this behavior by looking at the sessionOnly property of your NSHTTPCookies (the getter method is -(BOOL)isSessionOnly).