不幸的是,似乎没有公共 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)在等效代码中,我跳过了验证权限