【发布时间】: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];
}
提前致谢
【问题讨论】: