2011-07-26 65 views
1

我真的停留在這一個,所以請幫助!我正在寫一個實現Facebook連接的應用程序,所以當應用程序啓動時,我需要它來檢查它是否有一個有效的Facebook訪問令牌,並且還檢查他們提供的appKey是否仍然有效(我不希望同時登錄多個帳戶)。所以需要發生的是..iPhone應用程序委託與邏輯+ Facebook連接?

應用程序啓動 - >從NSUserDefaults獲取Facebook /我的訪問密鑰/令牌 - >發送我的appkey到服務器,以確保它仍然有效 - >如果有效,然後顯示我的tableviewcontroller。

如果它在其他地方失敗(Facebook訪問令牌無效,或者它的appkey無效),那麼它們將被帶到facebook連接按鈕的視圖。他們從那裏登錄後,他們將顯示在桌面控制器

我不知道如何構建我的應用程序委託和視圖控制器爲此工作。從我瞭解的Facebook連接,大部分的東西在委託的情況發生,因爲Facebook的應用程序: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]; 
} 

在此先感謝

回答

1

什麼意思的myKey?
在facebook.h中,方法isSessionValid只使用兩個變量。 (的accessToken,到期日期)

- (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中,根據會話是否有效來嘗試更改視圖。

再試一次!它會工作!

+0

感謝您的回覆!我不知道如果Facebook連接在rootviewcontroller中工作。我會嘗試並讓你知道! –

+0

它應該工作。如果您在您的rootviewcontroller.h中添加「FBconnect.h」 – manutd

+0

我能夠使Facebook登錄工作!我錯過了facebook.sessionDelegate = self;還需要更改Facebook.m文件中的一行。謝謝! –

相關問題