2013-04-02 73 views
2

問題:的Facebook的iOS SDK不調用完成處理程序

使用FB SDK和方法openActiveSessionWithReadPermissions,完成處理器似乎並不被調用時,該應用程序從Facebook的網頁或Facebook應用程序,我重新打開得到這個錯誤輸出:

FBSDKLog:從FBSessionStateCreated到FBSessionStateClosed FBSDKLog FBSession 無效過渡:FBSession過渡從FBSessionStateCreated到FBSessionStateCreatedOpening

語境

  • 使用Facebook SDK 3.2.1
  • 應用程序是專爲iOS 5.0及
  • 測試模擬器上的iOS 5.1
  • 測試的iPhone 4S使用的Xcode 4.6.1 ,運行iOS 6.1.2(不使用6.0社交框架,想測試實現5.0+)
  • 更新最初爲iOS 3.0構建的應用程序和u唱一個老版本的sharekit,但現在已經更新了ARC和共享工具包的實現,我相信已經被註釋掉了 - 希望這個問題不是與舊的共享工具包功能衝突,不能在代碼
  • 採取

步驟來解決搜索整個堆棧溢出

,發現提到類似的問題,但不是一個解決方案

具體細節:

這些是我採取的實施步驟t應用內的Facebook連接。

高層步驟:

  • 我有我的FB方法設立的AppDelegate
  • 在一個特定的視圖控制器我有一個按鈕,調用openSessionWithAllowLoginUI方法來啓動登錄過程

代碼

AppDelegate中。 m

- (void)applicationDidFinishLaunching:(UIApplication *)application 
{ 
// set up facebook logging 
    [FBSettings setLoggingBehavior:[NSSet setWithObjects:FBLoggingBehaviorFBRequests, FBLoggingBehaviorFBURLConnections, FBLoggingBehaviorAccessTokens, FBLoggingBehaviorSessionStateTransitions, nil]]; 

    // Call the ACAccountStore method renewCredentialsForAccount, which will update the OS's understanding of the token state 
    ACAccountStore *accountStore; 
    ACAccountType *accountTypeFB; 
    if ((accountStore = [[ACAccountStore alloc] init]) && 
     (accountTypeFB = [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierFacebook])){ 

     NSArray *fbAccounts = [accountStore accountsWithAccountType:accountTypeFB]; 
     id account; 
     if (fbAccounts && [fbAccounts count] > 0 && 
      (account = [fbAccounts objectAtIndex:0])){ 

      [accountStore renewCredentialsForAccount:account completion:^(ACAccountCredentialRenewResult renewResult, NSError *error) { 
       //we don't actually need to inspect renewResult or error. 
       if (error){ 

       } 
      }]; 
     } 
    } 

- (void)applicationDidBecomeActive:(UIApplication *)application 
{ 
    // We need to properly handle activation of the application with regards to Facebook Login 
    // (e.g., returning from iOS 6.0 Login Dialog or from fast app switching). 
    NSLog(@"Calling app did become active"); 
    [FBSession.activeSession handleDidBecomeActive]; 
} 

/* 
* Callback for session changes. 
*/ 
- (void)sessionStateChanged:(FBSession *)session 
         state:(FBSessionState) state 
         error:(NSError *)error 
{ 
    NSLog(@"Session State Changed"); 

    switch (state) { 
     case FBSessionStateOpen: 
      if (!error) { 
       // We have a valid session 
       NSLog(@"User session found"); 
      } 
      break; 
     case FBSessionStateClosed: 
     case FBSessionStateClosedLoginFailed: 
      [FBSession.activeSession closeAndClearTokenInformation]; 
      break; 
     default: 
      break; 
    } 

    [[NSNotificationCenter defaultCenter] 
    postNotificationName:FBSessionStateChangedNotification 
    object:session]; 

    if (error) { 
     UIAlertView *alertView = [[UIAlertView alloc] 
            initWithTitle:@"Error" 
            message:error.localizedDescription 
            delegate:nil 
            cancelButtonTitle:@"OK" 
            otherButtonTitles:nil]; 
     [alertView show]; 
    } 
} 

/* 
* Opens a Facebook session and optionally shows the login UX. 
*/ 
- (BOOL)openSessionWithAllowLoginUI:(BOOL)allowLoginUI { 
    NSLog(@"Openning session with Facebook"); 
    return [FBSession openActiveSessionWithReadPermissions:nil 
               allowLoginUI:allowLoginUI 
             completionHandler:^(FBSession *session, 
                  FBSessionState state, 
                  NSError *error) { 
              [self sessionStateChanged:session 
                   state:state 
                   error:error]; 
             }]; 
} 


/* 
* If we have a valid session at the time of openURL call, we handle 
* Facebook transitions by passing the url argument to handleOpenURL 
*/ 
- (BOOL)application:(UIApplication *)application 
      openURL:(NSURL *)url 
    sourceApplication:(NSString *)sourceApplication 
     annotation:(id)annotation { 
    // attempt to extract a token from the url 
    NSLog(@"Calling open URL"); 
    return [FBSession.activeSession handleOpenURL:url]; 
} 

- (void) closeSession { 
    NSLog(@"Clossing Facebook Sessions"); 
    [FBSession.activeSession closeAndClearTokenInformation]; 
} 

- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url 
{ 
    NSLog(@"handleOpenUrl Called"); 
    return [FBSession.activeSession handleOpenURL:url]; 

} 

視圖控制器與按鈕

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view from its nib. 

    // Register for Facebook change notification 
    [[NSNotificationCenter defaultCenter] 
    addObserver:self 
    selector:@selector(sessionStateChanged:) 
    name:FBSessionStateChangedNotification 
    object:nil]; 
} 

- (IBAction)login:(id)sender { 
    ATIAppDelegate *myAppDelegate = (ATIAppDelegate *)[[UIApplication sharedApplication]delegate]; 
    [myAppDelegate openSessionWithAllowLoginUI:YES]; 
} 

我認爲這個問題可能呢?

  • 它似乎可能是與令牌的東西?
  • 或者也許是通知中心?
  • 注意,在測試過程中,我一直打算和撤銷Facebook的應用程序到我的帳戶的訪問,然後嘗試再次登錄到應用程序,我看到這些已經引起問題與其他用戶
+0

好吧,我弄明白了 - 這個問題與FB本身無關,應用程序(我正在更新其他人的代碼)在.plist中有一個設置 - '應用程序不在後臺運行'設置爲true。 這意味着,一旦應用程序從Facebook應用程序或Facebook移動網站重新啓動,它就沒有準備好處理下一步。 – drc

+0

當我在幾個小時內使用stackoverflow時,我會添加這個作爲答案。 – drc

+0

嘿DRC,我有同樣的錯誤,但我沒有看到「應用程序不在後臺運行」.plist中的任何地方。這是應用程序主.plist還是應用程序具有的附加.plist? – SasaT

回答

3

好我弄明白了 - 這個問題與FB本身無關,應用程序(我正在更新其他人的代碼)在.plist中有一個設置 - '應用程序不在後臺運行'設置爲true。

這意味着,一旦應用程序從Facebook應用程序或Facebook移動網站重新啓動,它就沒有準備好處理下一步。

enter image description here

0

如果您不希望應用在後臺運行,可以綁定到FBSDKApplicationDelegate在你的AppDelegate像這樣:

import UIKit 
import FBSDKLoginKit 

@UIApplicationMain 
class AppDelegate: UIResponder, UIApplicationDelegate { 

    var window: UIWindow? 


    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { 
     return FBSDKApplicationDelegate.sharedInstance().application(application, didFinishLaunchingWithOptions: launchOptions) 
    } 

    func applicationWillResignActive(_ application: UIApplication) { 
     FBSDKAppEvents.activateApp() 
    } 

    func application(_ application: UIApplication, open url: URL, sourceApplication: String?, annotation: Any) -> Bool { 
     return FBSDKApplicationDelegate.sharedInstance().application(application, open: url, sourceApplication: sourceApplication, annotation: annotation) 
    } 
} 

https://forums.developer.apple.com/thread/50332 http://ashishkakkad.com/tag/fbsdkapplicationdelegate/

相關問題