我会尽量深入。我按照 Facebook 开发网站上的指南进行操作,最终得到以下代码:
在我的 AppDelegate.h 中:
@interface AppDelegate : UIResponder <UIApplicationDelegate, FBSessionDelegate> {
...
Facebook *facebook;
}
@property (nonatomic, retain) Facebook *facebook;
在 AppDelegate 实现中,我指定了 facebook appid 等。
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url
sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {
return [facebook handleOpenURL:url];
}
- (void)fbDidLogin {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:[facebook accessToken] forKey:@"FBAccessTokenKey"];
[defaults setObject:[facebook expirationDate] forKey:@"FBExpirationDateKey"];
[defaults synchronize];
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.menuViewController = [[MenuViewController alloc] init];
UIView *menuView = self.menuViewController.view;
facebook = [[Facebook alloc] initWithAppId:@"172938549507285" andDelegate:self];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if ([defaults objectForKey:@"FBAccessTokenKey"]
&& [defaults objectForKey:@"FBExpirationDateKey"]) {
facebook.accessToken = [defaults objectForKey:@"FBAccessTokenKey"];
facebook.expirationDate = [defaults objectForKey:@"FBExpirationDateKey"];
}
if (![facebook isSessionValid]) {
[facebook authorize:nil];
}
...
}
并且以防我的应用程序从未关闭,我在以下位置实现了相同的方法:
- (void)applicationWillEnterForeground:(UIApplication *)application
{
facebook = [[Facebook alloc] initWithAppId:@"172938549507285" andDelegate:self];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if ([defaults objectForKey:@"FBAccessTokenKey"]
&& [defaults objectForKey:@"FBExpirationDateKey"]) {
facebook.accessToken = [defaults objectForKey:@"FBAccessTokenKey"];
facebook.expirationDate = [defaults objectForKey:@"FBExpirationDateKey"];
}
if (![facebook isSessionValid]) {
[facebook authorize:nil];
NSLog(@"Facebook session is not valid");
}
NSLog(@"Application Entering Foreground.. setting segue to login flag");
[defaults setObject:@"yes" forKey:@"shouldgotologin"];
[defaults synchronize];
}
然而,即使在实现了这一切之后,我也从来没有打开过 facebook 登录窗口......它从来没有出现过......它以前出现过一次......但我现在在控制台日志中从来没有看到任何东西......我真的没有知道为什么……我很迷茫。有人能帮我吗?请不要将我引导到 Facebook 开发网站……那是我刚从那里来的,谢谢!