I am executing the following code to logout of Facebook from my iPhone application:
if ([mFacebook isSessionValid]) {
[mFacebook logout];
}
This code runs successfully and the delegate is called after this in which I am clearing the access token:
- (void)fbDidLogout
{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults removeObjectForKey:#"FBAccessTokenKey"];
[defaults removeObjectForKey:#"FBExpirationDateKey"];
[defaults synchronize];
}
But when I again log in to Facebook, it does not ask for username and password.
What am I doing wrong?
Are you using FB mobile Login Dialog? In Facebook.m, add the following code to remove cookies at m.facebook.com domain.
- (void)invalidateSession {
...
NSArray* facebookMCookies = [cookies cookiesForURL:
[NSURL URLWithString:#"https://m.facebook.com"]];
for (NSHTTPCookie* cookie in facebookMCookies) {
[cookies deleteCookie:cookie];
}
...
}
Try adding these at the end of your fbDidLogout block:
mFacebook.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];
}
}
Just add this line to fbDidLogout method
[mFacebook invalidateSession];
This will surely make you logout from Facebook.
Try this code:
- (void) fbDidLogout {
// Remove saved authorization information if it exists
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if ([defaults objectForKey:#"FBAccessTokenKey"]) {
[defaults removeObjectForKey:#"FBAccessTokenKey"];
[defaults removeObjectForKey:#"FBExpirationDateKey"];
[defaults synchronize];
}
}
Don't forget to create an instance for facebook.h in your AppDelegate.
Try to log off from the Facebook application. It might just take the token from the Facebook application.
Related
I am using Old facebook sdk for implemnt facebook app on my application.
This is my code
facebook = [[Facebook alloc] initWithAppId:#"!!!!!!!"];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
//[defaults setObject:[facebook accessToken] forKey:#"FBAccessTokenKey"];
// [defaults setObject:[facebook expirationDate] forKey:#"FBExpirationDateKey"];
NSString *accessTokenString = [defaults objectForKey:#"FBAccessTokenKey"];
NSDate *expirationDate = [defaults objectForKey:#"FBExpirationDateKey"];
if([accessTokenString length]>0 && expirationDate != nil)
{
accessToken =accessTokenString;
_expirationDate =expirationDate;
hasFBLoggedInBefore=YES;
}
else
{
hasFBLoggedInBefore = NO;
}
NSArray *permissions = [NSArray arrayWithObjects: #"offline_access",#"publish_actions",#"email",#"publish_stream",nil];
[facebook authorize:permissions delegate:self];
[facebook authorize:permissions delegate:self];
Sharing and all those things are fine..Now am facing problem..Suppose Ios6 device have no facebook application is installed then safari is using for loading sdk..if Ios6 have facebook application then sdk is open through native app.Then i am trying to login sdk through native app ,it goes to my home page(Logined user time line) not goes to permission and allthose things..what i am missing?
I have integrated Instagram in my App, I am able to Login, and Logout but when i am trying to re-Login than its not asking for the credentials again. its getting logged in with the same old credentials, but when i am deleting the the cookies and history from Safari/Settings from iPhone or Simulator than only its working else not working
The Login Process that i am using is
// here i can store accessToken
IGAppDelegate* appDelegate = (IGAppDelegate*)[UIApplication sharedApplication].delegate;
[[NSUserDefaults standardUserDefaults] setObject:appDelegate.instagram.accessToken forKey:#"accessToken"];
[[NSUserDefaults standardUserDefaults] synchronize];
IGListViewController* viewController = [[IGListViewController alloc] init];
[self.navigationController pushViewController:viewController animated:YES];
for Logout i am trying like this way :
static NSString* kDialogBaseURL = #"https://instagram.com/";
NSHTTPCookieStorage* cookies = [NSHTTPCookieStorage sharedHTTPCookieStorage];
NSArray* instagramCookies = [cookies cookiesForURL:[NSURL URLWithString:kDialogBaseURL]];
NSLog(#"Cookies %#",cookies);
NSLog(#"Cookies %#",instagramCookies);
for (NSHTTPCookie* cookie in instagramCookies)
{
NSLog(#"cookies is %#",cookie);
[cookies deleteCookie:cookie];
}
NSLog(#"Cookies %#",cookies);
NSHTTPCookieStorage* cookies = [NSHTTPCookieStorage sharedHTTPCookieStorage];
for (NSHTTPCookie* cookie in [cookies cookies]) {
if ([cookie.domain.uppercaseString containsString:#"instagram.com".uppercaseString]) {
[cookies deleteCookie:cookie];
}
}
I'm using Xcode 4.3.1
I use the below code to get the Facebook access token.
In Appdelegate:
facebook=[[Facebook alloc]initWithAppId:appid andDelegate:first];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if ([defaults objectForKey:#"FBAccessTokenKey"]
&& [defaults objectForKey:#"FBExpirationDateKey"])
{
facebook.accessToken = [defaults objectForKey:#"FBAccessTokenKey"];
facebook.expirationDate = [defaults objectForKey:#"FBExpirationDateKey"];
accessToken=facebook.accessToken;
date=facebook.expirationDate;
}
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {
return [ self.facebook handleOpenURL:url];
}
Facebook SSO (login)
In viewController:
-(IBAction)LoginClick:(id)sender
{
accesstoken=xapp.accessToken;
date=xapp.date;
[self fbDidExtendToken:xapp.accessToken expiresAt:xapp.date]; //For extending tokens
NSLog(#"accesstoken:%#",accesstoken);
if (![xapp.facebook isSessionValid])
{
[xapp.facebook authorize:permissions];
}
else
{
[xapp.facebook authorize: nil];
}
}
Here I can't able to handle the sessions properly.My requirement is for the first time user must login and he should authorize the app.Once he done the app should not ask for authorization.Bt now its asking authorization for each and ever time I'm running the app.How can I solve this.....
Do u save the token to user defaults after login ?
// this is a delegate method of FBSessionDelegate, called after user successfully login
- (void)fbDidLogin
{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:[xapp.facebook accessToken] forKey:#"FBAccessTokenKey"];
[defaults setObject:[xapp.facebook expirationDate] forKey:#"FBExpirationDateKey"];
[defaults synchronize];
}
Use the Latest FBGraph API.... Check this link How to share or post by mail, twitter and facebook from the current application? for further queries. This post includes all the latest techniques for FB, Twitter and EMail.
I use the facebook-ios-sdk for my iPhone app and launch the authorization screen when the user push a button. But i always get a different authorization screen. I enabled the "enhanced auth dialog" in the advanced settings...
Other apps use this one:
facebook = [[Facebook alloc] initWithAppId:#"167080433419201" andDelegate:self];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if ([defaults objectForKey:#"FBAccessTokenKey"] && [defaults objectForKey:#"FBExpirationDateKey"]) {
facebook.accessToken = [defaults objectForKey:#"FBAccessTokenKey"];
facebook.expirationDate = [defaults objectForKey:#"FBExpirationDateKey"];
}
if (![facebook isSessionValid]) {
NSArray *permissions = [[NSArray alloc] initWithObjects:#"user_about_me",nil];
[facebook authorize:permissions];
}
Check the permissions that you set while authorizing the facebook.
this is just an example
NSArray *permissions = [[NSArray alloc] initWithObjects:
#"user_likes",
#"read_stream",
nil];
[facebook authorize:permissions];
you can find more permission types here:https://developers.facebook.com/docs/authentication/permissions/
OAuth requires user to login to get granted permissions if there are no permissions in their app privacy list.
After login and allowing the app permissions request, we can get access_token from the redirected url.
So your situation is you must login first and your ios test app may already exist on the facebbook app privacy list.
The second screen had already been loged in, but the first screen was not.
You should adopt the FBSessionDelegate to store the access_token & expiration date.
- (void)fbDidLogin {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:[facebook accessToken] forKey:#"FBAccessTokenKey"];
[defaults setObject:[facebook expirationDate] forKey:#"FBExpirationDateKey"];
[defaults synchronize];
}
I found the solution, i tried different permissions. But the authorization screen takes a while before it changed to the right one...
Hello I am using MGTwitterEngine to create twitter app.I created different views for followers list, user time line and so on... Once I login It will save user credentials with following code
- (void) storeCachedTwitterOAuthData: (NSString *) data forUsername: (NSString *) username {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject: data forKey: #"authData"];
[defaults synchronize];
}
- (NSString *) cachedTwitterOAuthDataForUsername: (NSString *) username
{
return [[NSUserDefaults standardUserDefaults] objectForKey: #"authData"];
}
And I am passing consumer key and secret in view did appear . My question is should I write this consumer key and this user credential code in all view.. or I can do it some other way? And how to logout from twitter?
you should delete cookies for log out thing..
like this..
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];
}
}
I define consumer secret and key as a constant, they are static, so I just have to import a header, or even better put them in a constants.h and import with the prefix file in all files.
Logging out:
Logout using MGTwitterEngine ?
how to logout from Twitter in iPhone.I am using OAuth+MGTwitterEngine Library, there is a clearAccessToken method in the engine class.