我对用于登录的 Facebook API 感到困惑。我在网上找不到任何有用的信息。
我的 FB 会话委托方法没有被调用,并且访问令牌和过期日期值没有被设置,所以我认为我从未登录过。
我恢复到一个非常简单的应用程序,只有两个按钮(登录、注销)和一个用于查看状态信息的标签。
以下是视图控制器的代码,所有处理都在其中进行:
//
#import <UIKit/UIKit.h>
#import "Facebook.h"
@interface facebook_login_testViewController : UIViewController <FBSessionDelegate>{
UIButton *loginButton;
UIButton *logoutButton;
UILabel *info;
Facebook *facebook;
}
@property (nonatomic, retain) Facebook *facebook;
- (void) loggingIn;
- (void) loggingOut;
@end
and
#import "facebook_login_testViewController.h"
@implementation facebook_login_testViewController
@synthesize facebook;
- (void)dealloc
{
[super dealloc];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
loginButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[loginButton setTitle: @"Log In" forState:UIControlStateNormal];
[loginButton addTarget:self action:@selector(loggingIn) forControlEvents:UIControlEventTouchUpInside];
loginButton.frame = CGRectMake(200, 200, 200, 50);
logoutButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[logoutButton setTitle: @"Log Out" forState:UIControlStateNormal];
[logoutButton addTarget:self action:@selector(loggingOut) forControlEvents:UIControlEventTouchUpInside];
logoutButton.frame = CGRectMake(200, 300, 200, 50);
info = [[UILabel alloc] initWithFrame:CGRectMake(200, 400, 400, 600)];
info.numberOfLines = 0;
[self.view addSubview:loginButton];
[self.view addSubview:logoutButton];
[self.view addSubview:info];
info.text = @"Waiting to log in...\n\nPress the login button.";
facebook = [[Facebook alloc] initWithAppId:@"159...........5" andDelegate:self];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if ([defaults objectForKey:@"FBAccessTokenKey"]
&& [defaults objectForKey:@"FBExpirationDateKey"]) {
facebook.accessToken = [defaults objectForKey:@"FBAccessTokenKey"];
facebook.expirationDate = [defaults objectForKey:@"FBExpirationDateKey"];
}
}
- (void)viewDidUnload
{
[super viewDidUnload];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return YES;
}
- (void) loggingIn{
if (![facebook isSessionValid]) {
[facebook authorize:nil];
}
}
- (void) loggingOut{
info.text = [NSString stringWithFormat:@"\naccess token: %@\nexpiration date: %@\nfacebood description:%@\n",facebook.accessToken, facebook.expirationDate, facebook];
[facebook logout:self];
}
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {
return [facebook handleOpenURL:url];
}
- (void)fbDidLogin {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:[facebook accessToken] forKey:@"FBAccessTokenKey"];
[defaults setObject:[facebook expirationDate] forKey:@"FBExpirationDateKey"];
[defaults synchronize];
}
@end
.plist 文件设置如下
这看起来确实太简单了,不会出错。当我点击登录按钮时,我可以追踪authorize:permission
, 进而authorize:permissions localAppId:localAppId
,据我所知,我没有看到任何看起来会导致isSessionValid
变得真实。
我等待,然后按下注销按钮,然后查看一些 Facebook 参数,以防需要发生异步情况,但它们仍然为空。
有人能看到我缺少什么吗?