应用程序进入前台后Facebook请求

时间:2014-10-18 20:00:35

标签: ios facebook sdk facebook-login

我在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];

}

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];

}

1 个答案:

答案 0 :(得分:0)

好吧,好吧,我解决了自己的问题:我再次仔细阅读了文档,再次做了一切,将其添加到我的代表处:

   // 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];

}