gpt4 book ai didi

IOS 关闭应用程序时丢失 FBSession

转载 作者:可可西里 更新时间:2023-11-01 05:45:31 24 4
gpt4 key购买 nike

我正经历着最艰难的时期来完成这项工作。我有一个 IOS 应用程序,我正在尝试集成到 Facebook SDK 3.1。用户可以选择登录 Facebook,如果他们这样做的话,我正在缓存 token 。返回的 token 到期日期提前几周,我一切正常,登录/注销,返回前台。但是,每次我关闭应用程序时,都不会保留 FBSession。我知道这一点,因为当应用程序重新启动时,应用程序委托(delegate)执行 [self openSessionWithAllowLoginUI:NO] 并尝试刷新 session ,但这总是返回 null。我已经关注了其他教程和其他帖子,但似乎看不出我在我的应用程序委托(delegate)中做错了什么。

我正在用头撞墙思考为什么我会在应用程序关闭时失去这个 session 。我附上了我的 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

最佳答案

谢谢你们的评论。幸运的是,我终于能够找到问题所在。发生的事情是我已经将我的逻辑从 Root View Controller 中移出,但仍然调用了

[自行打开SessionWithAllowLoginUI:NO]

所以这意味着我连续调用了两次。一次在 appDelegate 中,一次在 Root View Controller 中。第二个电话似乎清除了我的 token ,自动将我注销。一旦我能够识别出这一点并从我的 Root View Controller 中删除该功能,一切都会按预期进行。

感谢您的帮助。

关于IOS 关闭应用程序时丢失 FBSession,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15699131/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com