我一直在为我的应用程序使用 Facebook 登录流程示例,并且在之前的示例应用程序中,此登录过程有效,但现在我收到错误(com.facebook.sdk 错误 2.)。我正在使用 iOS 6 进行构建,并检查了所有 id 和 bundle 标识符是否匹配。
示例代码
appdelegate.m
- (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");
}
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];
NSLog(@"Login Failed");
[alertView show];
}
}
// Opens a Facebook session and optionally shows the login UX.
- (BOOL)openSessionWithAllowLoginUI:(BOOL)allowLoginUI {
NSArray *permissions = [[NSArray alloc] initWithObjects:
@"user_events",
@"user_activites",
@"user_birthday",
nil];
return [FBSession openActiveSessionWithReadPermissions:permissions
allowLoginUI:allowLoginUI
completionHandler:^(FBSession *session,
FBSessionState state,
NSError *error) {
NSLog(@"\nfb sdk error = %@", 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
NSLog(@"Found FaceBook");
return [FBSession.activeSession handleOpenURL:url];
}
主控制器.m
- (IBAction)authButtonAction:(id)sender {
NSLog(@"session open");
AppDelegate *appDelegate = (AppDelegate*)
[[UIApplication sharedApplication] delegate];
// If the user is authenticated, log out when the button is clicked.
// If the user is not authenticated, log in when the button is clicked.
if (FBSession.activeSession.isOpen) {
[appDelegate closeSession];
} else {
// The user has initiated a login, so call the openSession method
// and show the login UX if necessary.
[appDelegate openSessionWithAllowLoginUI:YES];
}
}
- (void)sessionStateChanged:(NSNotification*)notification {
NSLog(@"Login Activated");
self.navigationItem.leftBarButtonItem = authButton;
if (FBSession.activeSession.isOpen) {
authButton = [[UIBarButtonItem alloc] init];
authButton.title=@"Login";
}else{
authButton =[[UIBarButtonItem alloc]init];
authButton.title=@"Logout";
}
}
有任何想法吗?这是 Facebook 的错误还是我错过了什么?