2

我从 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];
                                 }];

}

但它总是会打开一个 Safari 选项卡。 我需要强制 webview 登录而不是 Safari 选项卡。

预先感谢!

4

2 回答 2

14

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

    [session openWithBehavior:howToBehave
                    completionHandler:handler];

如果你在 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;
}

你也可以使用 - (id)initWithAppID:(NSString*)appID permissions:(NSArray*)permissions 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
            }
        }];

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

于 2012-10-15T15:43:45.293 回答
-1
 NSArray *permissions = @[@"basicinfo",etc...];
 FBSession *session = [[FBSession alloc] initWithPermissions:permissions];
 [FBSession setActiveSession:session];
 [session openWithBehavior:FBSessionLoginBehaviorForcingWebView completionHandler:^(FBSession *session, FBSessionState status, NSError *error) {}];
于 2014-03-31T08:39:29.053 回答