我在iOS中第一次使用facebook SDK。我试图关注Facebook Documentations以登录用户:Login Tutorial和Getting Started。
我在按下按钮时向新视图控制器添加FBLoginView
以提示用户登录。我面临的问题是,在FBLoginView
创建后,我立即被带到两个FBLoginViewDelegate
方法: loginViewShowingLoggedOutUser:和 loginView:handleError: < / p>
loginView:handleError:
将错误列为FBErrorCategoryUserCancelled
,而我从未取消任何请求!用于登录的模态弹出窗口显示2秒钟,然后自动解除取消状态。
我真的无法理解为什么会这样。我在模拟器上测试它。我在.plist文件中需要所有3个密钥用于Facebook SDK集成。为什么请求会自动取消?请帮我解决这个问题..谢谢。
代码:
#import "FBLoginViewController.h"
@interface FBLoginViewController ()
@end
@implementation FBLoginViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
FBLoginView *loginview = [[FBLoginView alloc] initWithReadPermissions:[NSArray arrayWithObjects:@"basic_info",@"user_photos",@"friend_photos", nil]];
loginview.frame = CGRectOffset(loginview.frame, (self.view.center.x - (loginview.frame.size.width / 2)), 5);
loginview.delegate = self;
[self.view addSubview:loginview];
}
#pragma mark - FBLoginDelegate
-(void) loginViewFetchedUserInfo:(FBLoginView *)loginView user:(id<FBGraphUser>)user
{
NSLog(@"user info: %@",user);
}
-(void) loginViewShowingLoggedInUser:(FBLoginView *)loginView
{
NSLog(@"User logged in");
}
-(void) loginViewShowingLoggedOutUser:(FBLoginView *)loginView
{
NSLog(@"User logged out");//Logs on console
}
-(void) loginView:(FBLoginView *)loginView handleError:(NSError *)error
{
NSString *alertMessage, *alertTitle;
// If the user should perform an action outside of you app to recover,
// the SDK will provide a message for the user, you just need to surface it.
// This conveniently handles cases like Facebook password change or unverified Facebook accounts.
if ([FBErrorUtility shouldNotifyUserForError:error]) {
alertTitle = @"Facebook error";
alertMessage = [FBErrorUtility userMessageForError:error];
// This code will handle session closures that happen outside of the app
// You can take a look at our error handling guide to know more about it
// https://developers.facebook.com/docs/ios/errors
} else if ([FBErrorUtility errorCategoryForError:error] == FBErrorCategoryAuthenticationReopenSession) {
alertTitle = @"Session Error";
alertMessage = @"Your current session is no longer valid. Please log in again.";
// If the user has cancelled a login, we will do nothing.
// You can also choose to show the user a message if cancelling login will result in
// the user not being able to complete a task they had initiated in your app
// (like accessing FB-stored information or posting to Facebook)
} else if ([FBErrorUtility errorCategoryForError:error] == FBErrorCategoryUserCancelled) {
NSLog(@"user cancelled login");
// For simplicity, this sample handles other errors with a generic message
// You can checkout our error handling guide for more detailed information
// https://developers.facebook.com/docs/ios/errors
} else {
alertTitle = @"Something went wrong";
alertMessage = @"Please try again later.";
NSLog(@"Unexpected error:%@", error);
}
if (alertMessage) {
[[[UIAlertView alloc] initWithTitle:alertTitle
message:alertMessage
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil] show];
}
}
@end
答案 0 :(得分:0)
奇怪,但它对我有用。在viewDidLoad中,我更改了FBLoginView
的初始化:
FBLoginView *loginview = [[FBLoginView alloc] initWithReadPermissions:[NSArray arrayWithObjects:@"basic_info",@"user_photos",@"friend_photos", nil]];
为:
FBLoginView *loginview = [[FBLoginView alloc] init];
无权限初始化会立即启动模式弹出窗口。
答案 1 :(得分:0)
问题可能是你的列表中的最后一项是零。