i am using the facebook login button provided by the fb sdk for implementing the facebook login in my app.When i am logging out the facebook button does change back to login again.but when i click on it,the safari app opens showing "you have alreay authorised the app"click confirm or cancel.Actually i needed the facebook login and password screen here.I cannot switch user until i reset the simulator.how to reset the contents in safari programmatically each time i logout so that the login screen is shown each time.
Please note that when i clear the website data of safari manually it shows the login page again.can it be done programmatically.
code i am using for login is as follows
//creating the login button
FBLoginView *loginView =
[[FBLoginView alloc] initWithReadPermissions:
#[#"public_profile", #"email", #"user_friends"]];
loginView.frame=CGRectMake(50, 500, 225, 55);
loginView.delegate=self;
[self.view addSubview:loginView];
//delegate method when logged in
- (void)loginViewFetchedUserInfo:(FBLoginView *)loginView
user:(id<FBGraphUser>)user {
NSUserDefaults *defaults=[NSUserDefaults standardUserDefaults];
[defaults setObject:[user objectForKey:#"id"] forKey:#"LoggedInUserID"] ;
[self checkandsaveNewUserInBackend:user];
[self performSegueWithIdentifier:#"Login" sender:self];
}
//logout code
- (IBAction)logoutButtonPressed:(id)sender {
[FBSession.activeSession closeAndClearTokenInformation];
[self.navigationController popToRootViewControllerAnimated:YES];
}
try this it user clicks on logout and remove all keys stored in userdefault/cookies
NSLog(#"Logged out 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];
}
}
Try this...
[FBSession.activeSession closeAndClearTokenInformation];
[FBSession.activeSession close];
[FBSession setActiveSession:nil];
May work for you...
And for clear cookies from browser...follow this link \
http://www.iossnippet.com/snippets/web/how-to-delete-cookies-nshttpcookie-in-objective-c-ios/
In the
//logout code
- (IBAction)logoutButtonPressed:(id)sender {
Use following code
if (FBSession.activeSession.state == FBSessionStateOpen
|| FBSession.activeSession.state == FBSessionStateOpenTokenExtended) {
// Close the session and remove the access token from the cache
// The session state handler (in the app delegate) will be called automatically
[FBSession.activeSession closeAndClearTokenInformation];
// If the session state is not any of the two "open" states when the button is clicked
} else {
// Open a session showing the user the login UI
// You must ALWAYS ask for public_profile permissions when opening a session
[FBSession openActiveSessionWithReadPermissions:#[#"public_profile"]
allowLoginUI:YES
completionHandler:
^(FBSession *session, FBSessionState state, NSError *error) {
}];
}
Related
I have integrate facebook api and i easily login but after logout my app from Facebook when i again login then is shows you have already authorised and come to the app home page.But I want if I logout account then next time again require username and password to use the different user.
I already yes ,status and review option from Facebook developers account to live but yet this problem occurs.
your logout is fine I think you are not clear the current session of facebook, when you click the logout button You have to implement the two methods for logout.
FBSDKLoginManager *loginManager = [[FBSDKLoginManager alloc] init];
[loginManager logOut];
set the currentAccessToken to nil
[FBSDKAccessToken setCurrentAccessToken:nil];
[FBSDKProfile setCurrentProfile:nil];
for loginview
- (void)loginButtonDidLogOut:(FBSDKLoginButton *)loginButton {
// do like delete the permission, this means you fully logout from facebook
[[[FBSDKGraphRequest alloc] initWithGraphPath:#"me/permissions/"
parameters:nil
HTTPMethod:#"DELETE"]
startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
NSLog(#"deleted successfully");
}];
}
the above method clear the current session , when you click the facebook button again it ask the new permission for new user.
if you want to clear the safari cookies also use
Only clears the local FB session information but not the Safari cookies. So, after I log in the user, I clear the Safari cookies:
NSLog(#"Logged out 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];
}
}
if you want to access the multiple user in the same time see sample app name Scrumptious you can get this app from here
I am using the sample program to learn the facebook-ios-sdk, I found a problem: if I logged into Facebook on my device and run that sample app, everything is fine, however, when I delete my Facebook account from my device and run that sample app again, I can still pass the login process and SCViewController can still be seen (There is a fast-app-switch to Facebook app process, but only I need to do is to click the "OKey" button, I don't need to fill any email/password information to log into the Facebook).
I checked the code and found that after my account is removed from the device, the token in FBSession.activeSession.accessToken is still considered as valid. Is there any problem? and How can I clear the token and make the log in dialog pop up? I have already invoked [FBSession.activeSession closeAndClearTokenInformation] when log out, the token should be cleared, according to the Facebook sdk document, but it is not the case.
the environment I use: XCode 4.6.1, iPad 6.1 simulator and facebook-ios-sdk v3.2.1.
updated: paste some code here:
in SCAppDelegate.m, I added 3 functions, which is not in the sample code but in the SDK online document:
- (void)showLoginView
{
UIViewController* topViewController = [self.navigationController topViewController];
UIViewController* modalViewController = [topViewController modalViewController];
// If the login screen is not already displayed, display it. If the login screen is
// displayed, then getting back here means the login in progress did not successfully
// complete. In that case, notify the login view so it can update its UI appropriately.
if (![modalViewController isKindOfClass:[SCLoginViewController class]]) {
SCLoginViewController* loginViewController = [[SCLoginViewController alloc]
initWithNibName:#"SCLoginViewController"
bundle:nil];
[topViewController presentModalViewController:loginViewController animated:NO];
} else {
SCLoginViewController* loginViewController = (SCLoginViewController*)modalViewController;
[loginViewController loginFailed];
}
}
- (void)sessionStateChanged:(FBSession*)session state:(FBSessionState)state error:(NSError*)error
{
switch (state) {
case FBSessionStateOpen: {
UIViewController* topViewController = [self.navigationController topViewController];
if ([[topViewController modalViewController]isKindOfClass:[SCLoginViewController class]]) {
[topViewController dismissModalViewControllerAnimated:YES];
}
}
break;
case FBSessionStateClosed:
case FBSessionStateClosedLoginFailed:
[self.navigationController popToRootViewControllerAnimated:NO];
[FBSession.activeSession closeAndClearTokenInformation];
[self showLoginView];
break;
default:
break;
}
if (error) {
UIAlertView* alertView = [[UIAlertView alloc]initWithTitle:#"Error" message:error.localizedDescription delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alertView show];
}
}
- (void)openSession
{
[FBSession openActiveSessionWithReadPermissions:nil allowLoginUI:YES completionHandler:^(FBSession* session, FBSessionState status, NSError* error){
[self sessionStateChanged:session state:status error:error];}];
}
Then in SCLoginViewController.m, I add a button, instead of using the existing FBLoginView object, to do the login job. The handler function for that button is as follows:
- (IBAction)performLogin:(id)sender {
//[self.spinner startAnimating];
SCAppDelegate* appDelegate = [UIApplication sharedApplication].delegate;
[appDelegate openSession];
}
Then in SCViewController.m, I add another button to do the log out job. The handler function for that button is as follows:
- (IBAction)logoutButtonWasPressed:(id)sender {
[FBSession.activeSession closeAndClearTokenInformation];
}
other code is almost the same as that in the sample code.
Apart from the clearing the token, you also need to clear the cookie that is stored in Safari when you use it to log into Facebook.
The following works for me with Facebook SDK 3+ on iOS 5.1+:
[FBSession.activeSession closeAndClearTokenInformation];
NSHTTPCookieStorage *storage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
for(NSHTTPCookie *cookie in [storage cookies])
{
NSString *domainName = [cookie domain];
NSRange domainRange = [domainName rangeOfString:#"facebook"];
if(domainRange.length > 0)
{
[storage deleteCookie:cookie];
}
}
We use Facebook SSO in our application. On clicking Facebook login app goes to Facebook app, displays login. But after login it won't return to app. But if a user is already logged in Facebook app, login button click switches to Facebook app and shows permission dialog. On clicking allow it returns to app. Is it the default behavior? Or is there any problem with my code or our Facebook app settings? Thanks in advance.
SHAppDelegate *appDelegate = [SHAppDelegate application];
NSArray * permissions = [[NSArray alloc]initWithObjects:FACEBBOK_PERMISSION, nil];
if (!appDelegate.session.isOpen) {
appDelegate.session = [[[FBSession alloc] initWithPermissions:permissions] autorelease];
if (appDelegate.session.state == FBSessionStateCreatedTokenLoaded) {
[appDelegate.session openWithCompletionHandler:^(FBSession *session,
FBSessionState status,
NSError *error) {
[self updateView];
}];
}
}
if (appDelegate.session.isOpen) {
[appDelegate.session closeAndClearTokenInformation];
} else
{
if (appDelegate.session.state != FBSessionStateCreated) {
// Create a new, logged out session.
appDelegate.session = [[FBSession alloc] init];
}
// if the session isn't open, let's open it now and present the login UX to the user
[appDelegate.session openWithCompletionHandler:^(FBSession *session,
FBSessionState status,
NSError *error) {
// and here we make sure to update our UX according to the new session state
[self updateView];
}];
}
I have the same issue. This is not a .plist trouble - without URLSchemes(with fbXXXXX) facebook app and safari never return to you application.
In this case, the Faccebook app does not return only when the user is not logged in it.
Safari handles this situation correctly. The same way a Facebook app does not return control if the user canceled the permissions confirmation.
In other situations, all right.
I am using iOS facebook SDK 3.0. How can i check if the user is already logged in?
I tried the line below but it does not work properly. It sometimes returns NO although I am logged in. Any suggestions?
if (FBSession.activeSession.isOpen == YES)
{
// post to wall else login
}
-- EDIT --
this is how I open my Facebook session:
NSArray *permissions = [[NSArray alloc] initWithObjects:
#"user_likes",
#"read_stream",
#"publish_actions",
nil];
return [FBSession openActiveSessionWithPermissions:permissions
allowLoginUI:allowLoginUI
completionHandler:^(FBSession *session,
FBSessionState state,
NSError *error) {
[self sessionStateChanged:session
state:state
error:error];
}];
The first time it needs login and so it works. If i try this while I am already logged in the FBSession.activeSession.isOpen returns NO.
You can check if you have a valid token by trying to open a new session without allowing the
login UI
if (FBSession.activeSession.isOpen)
{
// post to wall
} else {
// try to open session with existing valid token
NSArray *permissions = [[NSArray alloc] initWithObjects:
#"user_likes",
#"read_stream",
#"publish_actions",
nil];
FBSession *session = [[FBSession alloc] initWithPermissions:permissions];
[FBSession setActiveSession:session];
if([FBSession openActiveSessionWithAllowLoginUI:NO]) {
// post to wall
} else {
// you need to log the user
}
}
If you are using FBSDK greater then 4.x then there is no concept of FBSession. You have to find the active session only by using [FBSDKAccessToken currentAccessToken] simply check if it has nil value, no active session else it is.
Instead, you should check [FBSDKAccessToken currentAccessToken] at
viewDidLoad or similar. If a current token is available, do the
post-login work. You can also use currentAccessToken to retrieve
cached tokens.
you can find more here https://developers.facebook.com/docs/ios/upgrading-4.x
FBSession.activeSession has been replaced with [FBSDKAccessToken currentAccessToken] and FBSDKLoginManager. There is no concept of
session state. Instead, use the manager to login and this sets the
currentAccessToken reference.
i did it as in the Facebook example
if (FBSession.activeSession.state == FBSessionStateCreatedTokenLoaded)
{
}
The session is active if the state is either in FBSessionStateOpen or in FBSessionStateOpenTokenExtended. You can use the function below to check if the user is logged in:
- (BOOL)isSessionOpen
{
return FBSession.activeSession.state == FBSessionStateOpen || FBSession.activeSession.state == FBSessionStateOpenTokenExtended;
}
How are you opening your FBSession?
If you're creating an instance, be sure to set FBSession.activeSession. That was my issue for a while.
if ([FBSDKAccessToken currentAccessToken])
{
NSLog(#"Already login");
//[FBSession openActiveSessionWithAllowLoginUI: YES];
}
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