Ios 应用程序进入前台后的Facebook请求
我对iOS中的Facebook SDK有问题。一切都很好,但我有一个登录流的问题。当用户点击“登录Facebook”按钮时,他会被引导到Facebook,以允许我的应用程序获得权限。那很好。但当iOS从浏览器更改为我的应用程序并尝试发出请求时,我会收到以下错误消息:Ios 应用程序进入前台后的Facebook请求,ios,facebook,sdk,facebook-login,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 sp
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
}
}];
}
应用前景
- (void)appDidEnterForeground:(NSNotification *)notification {
NSLog(@"App did enter forground again.");
[self viewDidLoad];
}好的,我解决了我自己的问题:我再次非常仔细地阅读了文档,再次做了所有事情,并向我的代表添加了以下内容:
// 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
-(无效)loginView显示日志用户:(FBLoginView*)loginView{
NSLog(@“您已登录。(方法:loginviewshowingloggedinuser”)
}
//If the User is logged in we can fetch the events
[self getEvents];