Jean
Jean

Reputation: 2625

Facebook iOS SDK : isSessionValid returns no, but when am actually logged in

Am trying to use the Facebook SDK to login to FB from my App. I followed this tutorial https://developers.facebook.com/docs/mobile/ios/build/#implementsso religiously!

MY ISSUE This is what I did (I don't have the Facebook app installed in my iPhone) 1. The first time I logged in from my App in 'iPhone' into Facebook, it opened Safari. I logged in & granted permission to my App. 2. I deleted my App from my iPhone & restarted my iPhone. 3. Now, when I try to login to FB from my App again, the isSessionValid call returns 'no', & I tried printing the access-token & it returns null. This is fine. But the App, on executing isSessionValid, the App then calls (as in the tutorial) [thisAppDelegate.facebook authorize:nil]; which opens Safari in iPhone and takes me to a Facebook page which says am logged in (how come, when the isSessionValid returns null? ok, let's say that this App's session with FB got over though I was logged in) and that this App is already authorized. I click 'okay' in the screen below, the App crashes.

enter image description here

I get a EXC_BAD_ACCESS (code 1) after fbLogin call is done. And this seems to be happening in FBURLConnection.m in this method in line 2 ('for' loop):

- (BOOL)isCDNURL:(NSURL *)url
{
    NSString* urlHost = url.host;
    for (NSString* host in _cdnHosts) {
        if ([urlHost hasSuffix:host]) {
            return YES;
        }
    }

    return NO;
}

Printing the value of 'urlHost' gives me the value 'graph.facebook.com'.

Can someone help please?

CODE SNIPPET

AppDelegate.h

#import <UIKit/UIKit.h>
#import "FBConnect.h"

@interface com_AppDelegate : UIResponder <UIApplicationDelegate, FBSessionDelegate> {

    Facebook *facebook;
}

@property (strong, nonatomic) UIWindow *window;
@property (nonatomic, retain) Facebook *facebook;

@end

My AppDelegate.m

...
@synthesize facebook;


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // Override point for customization after application launch.
    facebook = [[Facebook alloc] initWithAppId:kFBAppId andDelegate:self];


    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    if ([defaults objectForKey:@"FBAccessTokenKey"] 
        && [defaults objectForKey:@"FBExpirationDateKey"]) {
        facebook.accessToken = [defaults objectForKey:@"FBAccessTokenKey"];
        facebook.expirationDate = [defaults objectForKey:@"FBExpirationDateKey"];
    }

    return YES;
}

- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {
    NSLog(@"AppDelegate.handleOpenURL");
    return [facebook handleOpenURL:url]; 
}

- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url
  sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {
    NSLog(@"AppDelegate.openURL");
    return [facebook handleOpenURL:url]; 
}

- (void)fbDidLogin {

    NSLog(@"AppDelegate.fbDidLogin");
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject:[facebook accessToken] forKey:@"FBAccessTokenKey"];
    [defaults setObject:[facebook expirationDate] forKey:@"FBExpirationDateKey"];
    [defaults synchronize];
    NSLog(@"Set the facebook accessToken to '%@' & expiry to '%@'", [facebook accessToken], [facebook expirationDate]);
}
...

I have a class FBTalker which will open a FB connection.

From FBTalker.h

... @interface ViewController_FBTalker : UIViewController { id appDelegate; } @property (nonatomic) com_AppDelegate *thisAppDelegate; ...

FBTalker.m

...
-(void) initializeView {

    appDelegate = [[UIApplication sharedApplication] delegate];
    thisAppDelegate = (com_AppDelegate *) appDelegate;

    /*
     Check for a valid session and if it is not valid call the authorize method which will both log the user in and prompt the user to authorize the app
     */
    NSLog(@"thisAppDelegate.facebook: access token - %@", [thisAppDelegate.facebook accessToken]);
    NSLog(@"thisAppDelegate.facebook: exp date - %@", [thisAppDelegate.facebook expirationDate]);

    if (![thisAppDelegate.facebook isSessionValid]) {
        [thisAppDelegate.facebook authorize:nil];
        NSLog(@"No valid FB Session yet");
        // Checks if the App has permission to publish to the user stream
        /* NSArray *permissions = [[NSArray alloc] initWithObjects:
                                @"publish_stream", 
                                nil];
        [thisAppDelegate.facebook authorize:permissions];
        */
    } else {
        NSLog(@"Valid FB Session exists");
    }

...

ENVIRONMENT XCode 4.3.3 Facebook iOS SDK (latest from Github) https://github.com/facebook/facebook-ios-sdk/ iOS 5 iPhone 4 (no Facebook App installed)

Upvotes: 0

Views: 1103

Answers (1)

Ananth
Ananth

Reputation: 845

Me too had a similar problem, once. Its because, generally on login, the access tokens are saved in UIWebView's cache storage. U need to remove the caches from the webview if you want to login again and again. You can use this method to clear facebook domain's caches alone.

- (void) clearFBCache 
  {
   for(NSHTTPCookie *cookie in [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies]) {

    if([[cookie domain] isEqualToString:@"facebook"]) {

        [[NSHTTPCookieStorage sharedHTTPCookieStorage] deleteCookie:cookie];
    }
   }
  }

This method worked for me. Hope this'll help you too.

Upvotes: 1

Related Questions