关闭应用程序时,IOS会丢失FBSession

关闭应用程序时,IOS会丢失FBSession,第1张

概述我正在努力实现这项工作.我有一个 IOS应用程序,我正在尝试集成到Facebook SDK 3.1.用户可以选择登录Facebook,如果他们正在缓存令牌.返回的令牌到期日期是提前几周,我让一切正常,登录/退出,返回前台.但是,每次关闭应用程序时,都不会保留FBSession.我知道这一点,因为当应用程序重新启动时,app委托执行[self openSessionWithAllowLoginUI: 我正在努力实现这项工作.我有一个 IOS应用程序,我正在尝试集成到Facebook SDK 3.1.用户可以选择登录Facebook,如果他们正在缓存令牌.返回的令牌到期日期是提前几周,我让一切正常,登录/退出,返回前台.但是,每次关闭应用程序时,都不会保留FBSession.我知道这一点,因为当应用程序重新启动时,app委托执行[self openSessionWithAllowLoginUI:NO]并尝试刷新会话,但是这总是返回null.我已经关注了其他教程和其他帖子,似乎无法在我的应用委托中看到我做错了什么.

我正在敲打我的头,因为我在应用程序关闭时失去了这个会话.我已经附上了我的appDelegate.

AppDelegate.m

#import "AppDelegate.h"@implementation AppDelegateNsstring *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
解决方法 谢谢你们的评论.幸运的是,我终于能够追查这个问题了.发生的事情是我从根视图控制器移动了我的逻辑,但仍然有一个调用

[self openSessionWithAllowLoginUI:NO]

所以这意味着我连续两次打电话.一旦进入appDelegate并再次进入根视图控制器.第二个电话似乎清除我的令牌自动登出我.一旦我能够识别出这个并从我的根视图控制器中删除功能,一切都按预期工作.

谢谢你的帮助.

总结

以上是内存溢出为你收集整理的关闭应用程序时,IOS会丢失FBSession全部内容,希望文章能够帮你解决关闭应用程序时,IOS会丢失FBSession所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1039770.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-24
下一篇 2022-05-24

发表评论

登录后才能评论

评论列表(0条)

保存