iphone app委托与逻辑+ facebook连接?

时间:2011-07-26 06:36:08

标签: iphone facebook delegates

我真的坚持这个,所以请帮忙!

我正在编写一个实现facebook连接的应用程序,所以当应用程序启动时,我需要检查它是否有有效的facebook访问令牌,并检查我提供的appKey是否仍然有效(我不要不希望一次登录多个帐户)。所以需要发生的是......

应用程序启动 - >从NSUserDefaults获取facebook /我的访问密钥/令牌 - >将我的appkey发送到服务器以确保它仍然有效 - >如果有效,则显示我的tableviewcontroller。

如果它在其他任何地方失败(facebook访问令牌无效,或者他们的应用程序的appkey无效),那么他们将被带到facebook连接按钮的View。从那里登录后,他们将显示tableviewcontroller

我不知道如何构建我的app委托和查看控制器以使其工作。根据我对facebook连接的了解,大部分内容必须在委托中发生,因为facebook的应用程序:handleOpenUrl:和fbDidLogin方法必须在app委托中调用,但如果我做了

self.window.rootViewController = self.tableController 

self.window.rootViewController = self.loginButtonViewController

之前,我将无法访问这些方法

我是否需要将视图控制器中的委托或其他内容放回到应用代理中?我没有线索..

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
facebook = [[Facebook alloc] initWithAppId:@"MY_APP_ID"];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if ([defaults objectForKey:@"FBAccessTokenKey"] 
    && [defaults objectForKey:@"FBExpirationDateKey"]) {
    facebook.accessToken = [defaults objectForKey:@"FBAccessTokenKey"];
    facebook.expirationDate = [defaults objectForKey:@"FBExpirationDateKey"];
}
NSString *myKey;
if ([defaults stringForKey:@"myKey"]) {
    myKey= [[NSString alloc] initWithString:[defaults stringForKey:@"myKey"]];
}
else{
    myKey = [[NSString alloc] initWithString:@""];
}
//SEND THE KEY + FBID TO SERVER
if ([facebook isSessionValid] /*&& [response==OK]*/) {
    self.window.rootViewController = self.navController;
    //delegate data to EventsTableController
}
else{
    self.window.rootViewController = self.loginController;
}

[self.window makeKeyAndVisible];
return YES;
}
- (void)fbDidLogin {
NSLog(@"did login");
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:[facebook accessToken] forKey:@"FBAccessTokenKey"];
[defaults setObject:[facebook expirationDate] forKey:@"FBExpirationDateKey"];
[defaults synchronize];
NSString *myKey;
[facebook requestWithGraphPath:@"me" andDelegate:self];
if ([defaults stringForKey:@"myKey"]) {
    myKey= [[NSString alloc] initWithString:[defaults stringForKey:@"myKey"]];;
}
else{
    myKey= [[NSString alloc] initWithString:@""];
}
//NSString *validKey = [[NSString alloc] initWithString:@"OK"];
//Send myKey and validKey to server
//server will do its thang and send data

[self.window addSubview:self.navController.view];
[self.window makeKeyAndVisible];
}

提前致谢

1 个答案:

答案 0 :(得分:1)

myKey是什么意思?
在facebook.h中,方法isSessionValid仅使用两个变量。 (accessToken,expirationDate)

- (BOOL)isSessionValid {
  return (self.accessToken != nil && self.expirationDate != nil
       && NSOrderedDescending == [self.expirationDate compare:[NSDate date]]);
}

您应该将上面的代码移到RootViewController.m而不是AppDelegate.m 因为MainWindow.xib只能识别一个RootView (在你的情况下,你想拥有更多的RootView,navController,loginController) See this Page

因此,我建议您将授权代码移至RootViewController.m
(等viewDidLoad或viewWillAppear方法)
接下来,在RootViewController中,尝试根据会话是否有效来更改视图。

再试一次!它会工作!