如何在iOS 6中使用openActiveSessionWithReadPermissions强制在SDK 3.1.1上进行webview登录?

Fed*_*olo 2 login facebook-ios-sdk ios6

我从facebook教程得到了这段代码:

- (BOOL)openSessionWithAllowLoginUI:(BOOL)allowLoginUI {
NSArray *permissions = [[NSArray alloc] initWithObjects:
    @"email", 
    @"user_likes",
    nil];
return [FBSession openActiveSessionWithReadPermissions:permissions
                                          allowLoginUI:allowLoginUI
                                     completionHandler:^(FBSession *session,
                                                     FBSessionState state,
                                                     NSError *error) {
                                     [self sessionStateChanged:session
                                                         state:state
                                                         error:error];
                                 }];
Run Code Online (Sandbox Code Playgroud)

}

但它始终打开Safari选项卡. 我需要强制进行webview登录而不是Safari Tab.

谢谢你的推荐!

rav*_*avi 14

不幸的是,似乎没有公开的api强迫这种行为.有解决方法(即只创建自己的会话并将其设置为活动会话并使用

    [session openWithBehavior:howToBehave
                    completionHandler:handler];
Run Code Online (Sandbox Code Playgroud)

如果你在github中观察openActiveSessionWithPermissions的源代码

+ (BOOL)openActiveSessionWithPermissions:(NSArray*)permissions
                            allowLoginUI:(BOOL)allowLoginUI
                      allowSystemAccount:(BOOL)allowSystemAccount
                                  isRead:(BOOL)isRead
                         defaultAudience:(FBSessionDefaultAudience)defaultAudience
                       completionHandler:(FBSessionStateHandler)handler {
    // is everything in good order?
    [FBSession validateRequestForPermissions:permissions
                             defaultAudience:defaultAudience
                          allowSystemAccount:allowSystemAccount
                                      isRead:isRead];
    BOOL result = NO;
    FBSession *session = [[[FBSession alloc] initWithAppID:nil
                                               permissions:permissions
                                           defaultAudience:defaultAudience
                                           urlSchemeSuffix:nil
                                        tokenCacheStrategy:nil]
                          autorelease];
    if (allowLoginUI || session.state == FBSessionStateCreatedTokenLoaded) {
        [FBSession setActiveSession:session];
        // we open after the fact, in order to avoid overlapping close
        // and open handler calls for blocks
        FBSessionLoginBehavior howToBehave = allowSystemAccount ?
                                                FBSessionLoginBehaviorUseSystemAccountIfPresent :
                                                    FBSessionLoginBehaviorWithFallbackToWebView;
        [session openWithBehavior:howToBehave
                completionHandler:handler];
        result = session.isOpen;
    }
    return result;
}
Run Code Online (Sandbox Code Playgroud)

你也可以使用 - (id)initWithAppID:(NSString*)appID权限:(NSArray*)权限urlSchemeSuffix:(NSString*)urlSchemeSuffix tokenCacheStrategy:(FBSessionTokenCachingStrategy*)tokenCachingStrategy;

要创建会话,如果使用[FBSession alloc] initWithPermissions:permissions],info.plist文件应该具有关键的FacebookAppID,并将您的蚜虫作为值

所以相当于这段代码

[FBSession setActiveSession: [[FBSession alloc] initWithPermissions:permissions] ];             

        [[FBSession activeSession] openWithBehavior:FBSessionLoginBehaviorForcingWebView completionHandler:^(FBSession *session, FBSessionState status, NSError *error) {
            switch (status) {
                case FBSessionStateOpen:
                    // call the legacy session delegate
                    //Now the session is open do corresponding UI changes
                    break;
                case FBSessionStateClosedLoginFailed:
                { // prefer to keep decls near to their use

                    // unpack the error code and reason in order to compute cancel bool
                    NSString *errorCode = [[error userInfo] objectForKey:FBErrorLoginFailedOriginalErrorCode];
                    NSString *errorReason = [[error userInfo] objectForKey:FBErrorLoginFailedReason];
                    BOOL userDidCancel = !errorCode && (!errorReason ||
                                                        [errorReason isEqualToString:FBErrorLoginFailedReasonInlineCancelledValue]);

                    // call the legacy session delegate if needed
                    //[[delegate facebook] fbDialogNotLogin:userDidCancel];
                }
                    break;
                    // presently extension, log-out and invalidation are being implemented in the Facebook class
                default:
                    break; // so we do nothing in response to those state transitions
            }
        }];
Run Code Online (Sandbox Code Playgroud)

注意:1)在等效代码中我跳过验证权限