我很难让这个工作。我有一个 IOS 应用程序,我正在尝试集成到Facebook SDK 3.1
. 用户可以选择登录 Facebook,如果他们这样做我正在缓存令牌。返回的令牌到期日期提前几周,我一切正常,登录/注销,返回前台。但是,每次我关闭应用程序时FBSession
都不会保留。我知道这一点,因为当应用程序重新启动时,应用程序委托会执行[self openSessionWithAllowLoginUI:NO]
并尝试刷新会话,但这总是返回 null。我已经关注了其他教程和其他帖子,但似乎看不到我在我的应用程序委托中做错了什么。
为什么我在应用程序关闭时会输掉这个会话,所以我很头疼。我已经附上了我的 appDelegate。
AppDelegate.m
#import "AppDelegate.h"
@implementation AppDelegate
NSString *const FBSessionStateChangedNotification=@"ro.Tag:FBSessionStateChangedNotification";
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
[self openSessionWithAllowLoginUI:NO];
NSLog(@"%@",[[FBSession activeSession] accessToken]);
return YES;
}
- (void)applicationDidBecomeActive:(UIApplication *)application
{
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
// We need to properly handle activation of the application with regards to SSO
// (e.g., returning from iOS 6.0 authorization dialog or from fast app switching).
[FBSession.activeSession handleDidBecomeActive];
}
/*
* Callback for session changes.
*/
- (void)sessionStateChanged:(FBSession *)session
state:(FBSessionState) state
error:(NSError *)error
{
switch (state) {
case FBSessionStateOpen:
if (!error) {
// We have a valid session
NSLog(@"User session found");
NSLog(@"session %@",session);
}
break;
case FBSessionStateClosed:
case FBSessionStateClosedLoginFailed:
[FBSession.activeSession closeAndClearTokenInformation];
break;
default:
break;
}
[[NSNotificationCenter defaultCenter]
postNotificationName:FBSessionStateChangedNotification
object:session];
if (error) {
UIAlertView *alertView = [[UIAlertView alloc]
initWithTitle:@"Error"
message:error.localizedDescription
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alertView show];
}
}
/*
* Opens a Facebook session and optionally shows the login UX.
*/
- (BOOL)openSessionWithAllowLoginUI:(BOOL)allowLoginUI {
NSArray *permissions = [[NSArray alloc] initWithObjects:
@"email",
@"user_games_activity",
@"user_location",
@"user_likes",
@"user_birthday",
nil];
//NSLog(@"permissions: %@",permissions);
return [FBSession openActiveSessionWithReadPermissions:permissions
allowLoginUI:allowLoginUI
completionHandler:^(FBSession *session,
FBSessionState state,
NSError *error) {
[self sessionStateChanged:session
state:state
error:error];
}];
}
/*
* If we have a valid session at the time of openURL call, we handle
* Facebook transitions by passing the url argument to handleOpenURL
*/
- (BOOL)application:(UIApplication *)application
openURL:(NSURL *)url
sourceApplication:(NSString *)sourceApplication
annotation:(id)annotation {
// attempt to extract a token from the url
return [FBSession.activeSession handleOpenURL:url];
}
/*
*Logout
*
*/
- (void) closeSession {
[FBSession.activeSession closeAndClearTokenInformation];
}
@end