我一直在为我的应用程序和之前的示例应用程序使用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];
}
maincontroller.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的错误还是我错过了什么?