Facebook iOS SDK:isSessionValid返回no,但是当我实际登录时

时间:2012-08-14 16:53:40

标签: iphone ios facebook-graph-api

我试图使用Facebook SDK从我的应用程序登录FB。我虔诚地遵循了本教程https://developers.facebook.com/docs/mobile/ios/build/#implementsso

我的问题 这就是我做的(我没有在我的iPhone上安装Facebook应用程序) 1.我第一次从“iPhone”中的应用程序登录到Facebook时,打开了Safari。我登录了&授予我的应用程序权限。 我从iPhone和iPhone中删除了我的应用程序。重启了我的iPhone。 3.现在,当我再次尝试从我的应用程序登录FB时,isSessionValid调用返回'no',&我尝试打印访问令牌&它返回null。这可以。但是App,在执行isSessionValid时,App会调用(如教程中所示)         [thisAppDelegate.facebook授权:nil]; 这会在iPhone中打开Safari并将我带到一个Facebook页面,该页面显示已登录(当isSessionValid返回null时怎么样?好吧,假设我登录时此应用程序与FB的会话已经结束)并且此应用程序是已经授权。我在下面的屏幕中单击“确定”,应用程序崩溃。

enter image description here

fbLogin调用完成后,我得到一个EXC_BAD_ACCESS(代码1)。这似乎发生在第2行('for'循环)中此方法的FBURLConnection.m中:

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

    return NO;
}

打印'urlHost'的值会给我'graph.facebook.com'的值。

有人可以帮忙吗?

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

我的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]);
}
...

我有一个类FBTalker,它将打开一个FB连接。

来自 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");
    }

...

环境 XCode 4.3.3 Facebook iOS SDK(最新来自Github)https://github.com/facebook/facebook-ios-sdk/ iOS 5 iPhone 4(未安装Facebook应用程序)

1 个答案:

答案 0 :(得分:1)

我也遇到过类似的问题。这是因为,通常在登录时,访问令牌会保存在UIWebView的缓存存储中。如果要一次又一次登录,则需要从webview中删除缓存。 您可以使用此方法单独清除facebook域的缓存。

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

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

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

这种方法对我有用。希望这对你也有帮助。