【问题标题】:Facebook request after app enter foreground应用程序进入前台后的 Facebook 请求
【发布时间】:2014-10-18 20:00:35
【问题描述】:

iOS 中的 Facebook SDK 有问题。一切正常,但我的登录流程有问题。当用户点击“登录 Facebook”按钮时,他会被定向到 Facebook 以允许我的应用程序获得权限。没关系。但是,当 iOS 从浏览器更改为我的应用程序并尝试发出请求时,我收到以下错误消息:

FBSDKLog: Error for request to endpoint 'search?q=concert&type=event&limit=10': An open FBSession must be specified for calls to this endpoint.

当我关闭应用程序并重新启动它并执行我的请求时,它可以正常工作。我得到了我的要求。这是我的代码,我知道它不是很整洁。

ViewdidLoad:

- (void)viewDidLoad {
[super viewDidLoad];


  [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(appDidEnterForeground:) name:UIApplicationWillEnterForegroundNotification object:nil];

//Some other Code



if ([[FBSession activeSession]state])
{
    NSLog(@"User is logged in");
    [loginView setHidden:TRUE];


} else {
    // try to open session with existing valid token
    NSArray *permissions = [[NSArray alloc] initWithObjects:
                            @"user_events",
                            nil];
    FBSession *session = [[FBSession alloc] initWithPermissions:permissions];
    [FBSession setActiveSession:session];
    if([FBSession openActiveSessionWithAllowLoginUI:NO]) {

    } else {
        NSLog(@"User is logged out");

        //Create the FB Login-Button
        loginView = [[FBLoginView alloc] initWithReadPermissions:@[@"user_events"]];
        // Align the button in the center horizontally
        loginView.frame = CGRectOffset(loginView.frame, (self.view.center.x - (loginView.frame.size.width / 2)), 200);

        [self.view addSubview:loginView];


    }
}

[self getEvents];

}

获取事件:

-(void)getEvents{
//Get General Events



[FBRequestConnection startWithGraphPath:@"search?q=concert&type=event&limit=10"
                      completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {


                          if (!error) {
                                    //Some Code

                          else {
                              // An error occurred, we need to handle the error
                              // See: https://developers.facebook.com/docs/ios/errors

                          }

                      }];


   }

appDidEnterForeground

- (void)appDidEnterForeground:(NSNotification *)notification {
NSLog(@"App did enter forground again.");


[self viewDidLoad];

}

【问题讨论】:

    标签: ios facebook sdk facebook-login


    【解决方案1】:

    好的,我解决了我自己的问题:我再次非常仔细地阅读了文档,并再次将所有内容添加到我的代表中:

       // Whenever a person opens the app, check for a cached session
    if (FBSession.activeSession.state == FBSessionStateCreatedTokenLoaded) {
    
        // If there's one, just open the session silently, without showing the user the login UI
        [FBSession openActiveSessionWithReadPermissions:@[@"public_profile"]
                                           allowLoginUI:NO
                                      completionHandler:^(FBSession *session, FBSessionState state, NSError *error) {
                                          // Handler for session state changes
                                          // This method will be called EACH time the session state changes,
                                          // also for intermediate states and NOT just when the session open
                                          [self sessionStateChanged:session state:state error:error];
                                      }];
    
    
    
    }
    
    }
    
    
    
    // This method will handle ALL the session state changes in the app
    
    
    -(void)sessionStateChanged:(FBSession *)session state:(FBSessionState) state error:  (NSError *)error
       {
           // If the session was opened successfully
      // customize your code...
    
       }
    

    然后我正在执行我的方法,其中包含我这样的请求

    // Logged-in user experience
    

    - (void)loginViewShowingLoggedInUser:(FBLoginView *)loginView { NSLog(@"你已经登录了。(方法:loginviewshowingloggedinuser");

    //If the User is logged in we can fetch the events
    [self getEvents];
    

    }

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多