2

友達の壁でリンクを共有するために、iOSアプリでFacebooksdk3.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 に答える