带逻辑的iphone应用程序委托+;facebook连接?

带逻辑的iphone应用程序委托+;facebook连接?,iphone,facebook,delegates,Iphone,Facebook,Delegates,我真的被困在这一个,所以请帮助 我正在编写一个实现facebook connect的应用程序,因此当应用程序启动时,我需要它检查是否有有效的facebook访问令牌,并检查我提供的appKey是否仍然有效(我不希望一次登录超过一个帐户)。因此,我们需要做的是 应用程序启动->从NSUserDefaults获取facebook/my access key/token->将我的应用程序密钥发送到服务器以确保其仍然有效->如果有效,则显示我的tableviewcontroller 如果它在其他任何地方

我真的被困在这一个,所以请帮助

我正在编写一个实现facebook connect的应用程序,因此当应用程序启动时,我需要它检查是否有有效的facebook访问令牌,并检查我提供的appKey是否仍然有效(我不希望一次登录超过一个帐户)。因此,我们需要做的是

应用程序启动->从NSUserDefaults获取facebook/my access key/token->将我的应用程序密钥发送到服务器以确保其仍然有效->如果有效,则显示我的tableviewcontroller

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

我不知道如何构造我的应用程序委托和视图控制器,以使其正常工作。根据我对facebook connect的了解,大部分事情都必须发生在代理中,因为facebook的应用程序:handleOpenUrl:和fbdilogin方法必须在应用程序代理中调用,但如果我

self.window.rootViewController = self.tableController 

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

我是否需要将视图控制器中的委托或其他内容放回应用程序委托?我不知道

- (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];
}
提前感谢

我的钥匙是什么意思?
在facebook.h中,方法
isSessionValid
只使用两个变量。(accessToken,过期日期)

您应该将上述代码移到RootViewController.m而不是AppDelegate.m 因为MainWindow.xib只能识别一个RootView。
(在您的情况下,您希望拥有更多的RootView、navController和loginController)

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

再试一次!它会成功的

什么是myKey?
在facebook.h中,方法
isSessionValid
只使用两个变量。(accessToken,过期日期)

您应该将上述代码移到RootViewController.m而不是AppDelegate.m 因为MainWindow.xib只能识别一个RootView。
(在您的情况下,您希望拥有更多的RootView、navController和loginController)

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


再试一次!它会成功的

谢谢你的回复!我不确定facebook连接是否在rootviewcontroller中工作。我会试试,让你知道!它应该会起作用。如果您在rootviewcontroller.hI中添加“FBconnect.h”,您就可以让facebook登录正常工作了!我错过了facebook;还需要更改Facebook.m文件中的一行。谢谢谢谢你的回复!我不确定facebook连接是否在rootviewcontroller中工作。我会试试,让你知道!它应该会起作用。如果您在rootviewcontroller.hI中添加“FBconnect.h”,您就可以让facebook登录正常工作了!我错过了facebook;还需要更改Facebook.m文件中的一行。谢谢
- (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];
}
- (BOOL)isSessionValid {
  return (self.accessToken != nil && self.expirationDate != nil
       && NSOrderedDescending == [self.expirationDate compare:[NSDate date]]);
}