2

我在我的 ios 应用程序中使用 Facebook sdk 3.1 在朋友墙上分享链接。我尝试在 applicationDidFinishLaunching 方法中打开一个新会话,如下所示

- (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。因此无法在 Facebook 上发帖。

我在这里做错了吗?任何帮助将不胜感激。!

4

1 回答 1

1

我有同样的问题,我通过 NSTimer 调用 openSessionWithAllowLoginUI: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 函数使用不同的线程。

希望,它会帮助你。

谢谢

于 2012-11-21T05:48:48.373 回答