【问题标题】:iphone app delegate with logic + facebook connect?带有逻辑+ facebook连接的iphone应用程序委托?
【发布时间】:2011-07-26 06:36:07
【问题描述】:

我真的被这个卡住了,所以请帮忙!

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

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

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

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

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];
}

提前致谢

【问题讨论】:

    标签: iphone facebook delegates


    【解决方案1】:

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

    - (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 中,尝试根据 session 是否有效来改变你的视图。

    再试一次!它会起作用的!

    【讨论】:

    • 感谢您的回复!我不确定 facebook 连接是否在 rootviewcontroller 中工作。我会试试看,让你知道!
    • 它应该可以工作。如果在 rootviewcontroller.h 中添加“FBconnect.h”
    • 我能够让 Facebook 登录工作!我错过了 facebook.sessionDelegate=self;并且还需要更改 Facebook.m 文件中的一行。谢谢!
    猜你喜欢
    • 2019-04-03
    • 2012-04-29
    • 2023-01-19
    • 1970-01-01
    • 2011-04-24
    • 1970-01-01
    • 2012-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多