我最難得到這個工作。我有一個IOS應用程序,我試圖集成到Facebook SDK 3.1
。如果用戶正在緩存令牌,用戶可以選擇登錄Facebook。返回的令牌過期日期是提前幾周,並且我有一切正常工作,登錄/註銷,返回到前臺。但是,每次關閉應用程序FBSession
都未被保留。我知道這是因爲當應用程序重新啓動時,應用程序委託執行[self openSessionWithAllowLoginUI:NO]
並嘗試刷新會話,但這總是返回null。我跟着其他教程和其他帖子,似乎無法看到我在我的應用程序委託中做錯了什麼。iOS在關閉應用程序時丟失FBSession
爲什麼我在應用程序關閉時失去了本次會話,我正在撞牆。我已附加了我的appDelegate。
AppDelegate.m
#import "AppDelegate.h"
@implementation AppDelegate
NSString *const [email protected]"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
您是否嘗試過在openSessionWithAllowLoginUI中打印錯誤。可能是你得到錯誤。 – meth
嘗試使用Facebook iOS SDK 3.2(.1),然後再進一步追究:3.2更新對令牌保存和其他認證流程問題進行了修復。 – cbowns