2012-11-08 160 views
2

我在我的ios應用程序中使用Facebook sdk 3.1來在朋友牆上分享鏈接。我嘗試在的applicationDidFinishLaunching方法打開一個新的會話如下Facebook SDK 3.1 for iOS登錄問題

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{ 
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; 

myAppDelegate = self; 

AudioViewController *viewController = [[AudioViewController alloc] init]; 
self.navController = [[UINavigationController alloc] initWithRootViewController:viewController]; 
[self.navController setNavigationBarHidden:YES]; 
viewController = nil; 


self.window.rootViewController = self.navController; 
[self.window makeKeyAndVisible]; 

if (![self openSessionWithAllowLoginUI:NO]) { 
    // No? Display the login page. 
    [self performSelector:@selector(login) withObject:nil afterDelay:0.5]; 
} 

return YES; 
} 

- (void)login{ 
[self openSessionWithAllowLoginUI:YES]; 
} 

- (BOOL)openSessionWithAllowLoginUI:(BOOL)allowLoginUI { 
return [FBSession openActiveSessionWithReadPermissions:nil 
              allowLoginUI:allowLoginUI 
            completionHandler:^(FBSession *session, FBSessionState state, NSError *error) { 
             [self sessionStateChanged:session state:state error:error]; 
            }]; 
} 

- (void)sessionStateChanged:(FBSession *)session 
        state:(FBSessionState)state 
        error:(NSError *)error{ 

switch (state) { 
    case FBSessionStateOpen: { 
     DLOG(@"session open"); 
    } 
     break; 
    case FBSessionStateClosed: { 

     DLOG(@"session closed"); 


     [FBSession.activeSession closeAndClearTokenInformation]; 


    } 
     break; 
    case FBSessionStateClosedLoginFailed: { 


     DLOG(@"session Failed"); 


    } 
     break; 
    default: 
     break; 
} 

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

if (error) { 
    DLOG(@"error = %@",error); 
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:[NSString stringWithFormat:@"Error: %@", 
                   [AppDelegate FBErrorCodeDescription:error.code]] 
                 message:error.localizedDescription 
                 delegate:nil 
               cancelButtonTitle:@"OK" 
               otherButtonTitles:nil]; 
    [alertView show]; 
} 
} 

隨着一些Facebook的賬戶返回錯誤「操作無法完成」 com.facebook.sdk錯誤2.等進一步的不能發佈在臉書上。

我在這裏做錯了什麼?任何幫助將不勝感激。

+0

您是否在登錄時出現此錯誤? –

+0

是否添加了facebook方法查看您登錄Facebook的方法 –

+0

偏離主題,但通過API張貼到朋友的牆壁已棄用,並且在2013年2月6日後將不可用。 –

回答

1

我有同樣的問題,我已經通過調用openSessionWithAllowLoginUI解決了它:通過NSTimer TRUE。

if (![self openSessionWithAllowLoginUI:NO]) { 
    // No? Display the login page. 
    [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(login) userInfo:nil repeats:NO]; 
} 

的原因是,我們不能在不同的線程得到FB會話中,我們使用的主線程或其他(背景)線程。在你的代碼中,當你檢查會話可用性時,你使用主線程和登錄,你可以通過performSelector函數使用不同的線程。

希望它能幫助你。

謝謝

+0

感謝@woodenlabs提供的解決方案,它的工作原理.. !!! – Dishant