0

ネイティブ ログイン用の最新の FB SDK を使用して iOS アプリに取り組んでいます。設定の [これらのアプリにアカウントの使用を許可する] でアプリをオフにすると、エラー「com.facebook.sdk エラー 2」が予想されます。来ます。

アプリで「これらのアプリがアカウントを使用できるようにする」がオフになっている場合でも、このエラーを解決するエレガントな方法はありますか? 解決策を検索しましたが、すべての回答は、そのオプションをオンにする必要があると言っています。しかし、より良い方法は、ユーザーがそのオプションをオフに切り替えた場合でも、ユーザーが自分のデバイスで Facebook にまったくログインしないのと同じように、シームレスに高速アプリ切り替えの方法にフォールバックしてログインできるようにすることだと思います。最新の FB SDK でこれを行うにはどうすればよいですか? ありがとう!

====================================更新============= ============================ 非推奨関数 openActiveSessionWithPermissions:allowLoginUI:completionHandler を使って解決しました

最初に、ユーザーがこのオプションをオフに切り替えているかどうかを確認する必要があります。

    self.useAccountAllowed = true;
    ACAccountStore *accountStore;
    ACAccountType *accountTypeFB;
    if ((accountStore = [[ACAccountStore alloc] init]) &&
        (accountTypeFB = [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierFacebook] ) ){

        NSArray *fbAccounts = [accountStore accountsWithAccountType:accountTypeFB];
        id account;
        if (!fbAccounts)
        {
            //do not log into FB on the device
        }
        else if ([fbAccounts count] == 0) {
            [FBSession.activeSession closeAndClearTokenInformation];
            self.useAccountAllowed = false;  //user switch this option off
        } 

次に openSession 関数で、self.useAccountAllowed が false の場合に非推奨の関数を使用します。

if (self.useAccountAllowed) {
        [FBSession openActiveSessionWithReadPermissions:nil allowLoginUI:YES completionHandler:^(FBSession* session, FBSessionState status, NSError* error){
            [self sessionStateChanged:session state:status error:error];}];
    }
    else {
        NSArray* lPermission = FBSession.activeSession.permissions;
        [FBSession openActiveSessionWithPermissions:lPermission allowLoginUI:YES completionHandler:^(FBSession* session, FBSessionState status, NSError* error){
            [self sessionStateChanged:session state:status error:error];}];

それが正しい方法かどうかはわかりません。

4

1 に答える 1

0

これが私がそれを解決した方法です。AppDelegate 実装ファイルのメソッドで、FB SDK ドキュメントで推奨されているようにapplicationDidBecomeActive、通常のメソッドを使用します。さらに、Settings でユーザー権限をチェックする新しいメソッドを追加します (以下の例で呼び出しています)。[FBSession.activeSession handleDidBecomeActive]checkPermissionSettings

- (void)applicationDidBecomeActive:(UIApplication *)application
{
    NSLog(@"applicationDidBecomeActive: in NHOCAppDelegate");
    //
    // The flow back to your app may be interrupted (for ex: if the user clicks the Home button
    // while if authenticating via the Facebook for iOS app).
    // If this happens, the Facebook SDK can take care of any cleanup that may include starting a fresh session.
    //
    [FBSession.activeSession handleDidBecomeActive];
    [self checkPermissionSettings];
}

//
// Verify if the user pressed the Home Button, went to Settings and deauthorized the app via "Allow These Apps to Use Your Account..."
// If so, redirect him to the login screen (this happens automagically, see below).
//
- (void)checkPermissionSettings
{
    NSLog(@"checkPermissionSettings: in NHOCAppDelegate");
    //
    // Now 'startForMeWithCompletionHandler' may return 'FBSessionStateClosed' (meaning that the user probably unauthorized the app in Settings).
    //
    // If that is the case:
    //
    //  - Hide the 'logged' View Controller
    //  - Remove it (NHOCLoggedVC) from the Notification Center
    //  - Show the 'login' View Controller
    //  - And finally add it (NHOCLoginVC) to the Notification Center, closing the loop
    //
    // Check the console for further info.
    //
    [FBRequestConnection startForMeWithCompletionHandler:^(FBRequestConnection *connection, id<FBGraphUser> user, NSError *error) {

        if (!error) {
            //
            // Everything went fine... The app is in good shape.
            // Notice that 'user.location' requires user_location permission
            //
            NSLog(@"user.location: %@: ", [user.location objectForKey:@"name"]);
        }
    }];
}

設計どおりに機能させるために、通知センターも使用しています。ここで例全体を確認できます。

フィードに公開する FB SDK + ストーリーボード

お役に立てば幸いです。

于 2013-04-17T04:18:44.430 に答える