[英]Facebook SDK FBLoginView
I have implement Facebook SDK into my project. 我已经在我的项目中实现了Facebook SDK。 I login using
FBLoginView
. 我使用
FBLoginView
登录。 When I first go to the app i create the StartView
with this FBLoginView
and sign with Facebook. 当我第一次去的应用程序,我创建
StartView
这个FBLoginView
以及与Facebook签署。 After I close my app I want to launch it for second time. 关闭我的应用程序后,我想第二次启动它。 But now I don't want to show StartView with login button and I want to show the main page from my app.
但是现在我不想显示带有登录按钮的StartView,而是想要显示应用程序的主页。 How I can make this?
我该怎么做?
In my AppDelegate
I wrote this: 在我的
AppDelegate
我这样写:
if (FBSession.activeSession.state == FBSessionStateCreatedTokenLoaded){
//Go to theMain page
}
It will work with FBLoginView
or it will be work only with customLoginButton
? 它将与
FBLoginView
一起FBLoginView
或者仅与customLoginButton
一起customLoginButton
?
In your condition, add: 根据您的条件,添加:
// 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];
}];
Then implement the method sessionStateChanged:session
: 然后实现方法
sessionStateChanged:session
:
// 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
if (!error && state == FBSessionStateOpen){
NSLog(@"Session opened");
// Show the user the logged-in UI
[self goToMainPage];
return;
}
if (state == FBSessionStateClosed || state == FBSessionStateClosedLoginFailed){
// If the session is closed
NSLog(@"Session closed");
// Show the user the logged-out UI
[self userLoggedOut];
}
// Handle errors
if (error){
NSLog(@"Error");
NSString *alertText;
NSString *alertTitle;
// If the error requires people using an app to make an action outside of the app in order to recover
if ([FBErrorUtility shouldNotifyUserForError:error] == YES){
alertTitle = @"Something went wrong";
alertText = [FBErrorUtility userMessageForError:error];
[self showMessage:alertText withTitle:alertTitle];
} else {
// If the user cancelled login, do nothing
if ([FBErrorUtility errorCategoryForError:error] == FBErrorCategoryUserCancelled) {
NSLog(@"User cancelled login");
// Handle session closures that happen outside of the app
} else if ([FBErrorUtility errorCategoryForError:error] == FBErrorCategoryAuthenticationReopenSession){
alertTitle = @"Session Error";
alertText = @"Your current session is no longer valid. Please log in again.";
[self showMessage:alertText withTitle:alertTitle];
// Here we will handle all other errors with a generic error message.
// We recommend you check our Handling Errors guide for more information
// https://developers.facebook.com/docs/ios/errors/
} else {
//Get more error information from the error
NSDictionary *errorInformation = [[[error.userInfo obje ctForKey:@"com.facebook.sdk:ParsedJSONResponseKey"] objectForKey:@"body"] objectForKey:@"error"];
// Show the user an error message
alertTitle = @"Something went wrong";
alertText = [NSString stringWithFormat:@"Please retry. \n\n If the problem persists contact us and mention this error code: %@", [errorInformation objectForKey:@"message"]];
[self showMessage:alertText withTitle:alertTitle];
}
}
// Clear this token
[FBSession.activeSession closeAndClearTokenInformation];
// Show the user the logged-out UI
[self userLoggedOut];
}
}
You can read the complete doc for more information 您可以阅读完整的文档以获取更多信息
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.