我在其他几篇文章中看到了这个问题,但似乎没有什么对我有用。遵循FB 登录指南后,我有了一个工作原型,允许用户使用 FB 进行身份验证。但是,本指南指示您将所有 FB OAuth 逻辑直接放入 AppDelegate 中,这对我来说并不理想,因为我将支持的不仅仅是 FB 身份验证。所以我所做的就是将登录逻辑移到它自己的类中。从那时起,在用户成功通过 FB 进行身份验证后似乎不会调用 FB 委托方法,并且应该将控制权传递回我的应用程序。
我有一个模式登录视图,它将我的 FacebookAuth 类作为代表。当我尝试创建会话时,会出现 FB 登录页面并授权我的应用程序。之后,不会调用其他委托方法。谁能提供一些提示?
@interface FacebookAuth()
@property (nonatomic, strong) User *sessionUser;
@end
@implementation FacebookAuth
@synthesize sessionIsOpen = _sessionIsOpen;
@synthesize sessionUser = _sessionUser;
-(id)initWithViewDelegate:(id<AuthorizationViewDelegate>)viewDelegate
{
NSAssert(viewDelegate != nil, @"FacebookAuth must be initialized with a view delegate.");
self = [super init];
if (self) {
self.viewDelegate = viewDelegate;
}
return self;
}
-(id)init
{
self = [super init];
if (self) {
self = [self initWithViewDelegate:nil];
}
return self;
}
-(BOOL)sessionIsOpen{
return (FBSession.activeSession.state == FBSessionStateOpen);
}
-(void)requestSessionUserWithSuccess:(void (^)(User *))success Failure:(void (^)(NSError *))error{
if(!self.sessionIsOpen){
error([NSError errorWithDomain:@"Session Closed" code:0 userInfo:nil]);
return;
}
if(self.sessionUser == nil){
[[FBRequest requestForMe] startWithCompletionHandler:
^(FBRequestConnection *connection,
NSDictionary<FBGraphUser> *user,
NSError *err) {
if (!err) {
User *ses_user = [[User alloc]initWithId:user.id first:user.first_name last:user.last_name sessionToken: [[[FBSession activeSession] accessTokenData] accessToken]];
self.sessionUser = ses_user;
success(ses_user);
} else{
error(err);
}
}];
} else{
success(self.sessionUser);
}
}
-(void)openSession
{
[FBSession openActiveSessionWithReadPermissions:nil
allowLoginUI:YES
completionHandler:
^(FBSession *session,
FBSessionState state, NSError *error) {
[self sessionStateChanged:session state:state error:error];
}];
}
-(void)closeSession{
[FBSession.activeSession closeAndClearTokenInformation];
self.sessionUser = nil;
self.sessionIsOpen = NO;
};
-(void)sessionStateChanged:(FBSession *)session
state:(FBSessionState) state
error:(NSError *)error
{
switch (state) {
case FBSessionStateOpen:{
self.sessionIsOpen = YES;
[self.viewDelegate loginSuccess];
}
break;
case FBSessionStateClosed:{
self.sessionIsOpen = NO;
}
break;
case FBSessionStateClosedLoginFailed:{
[FBSession.activeSession closeAndClearTokenInformation];
self.sessionIsOpen = NO;
[self.viewDelegate loginFailure:error];
}
break;
default:
break;
}
}
-(BOOL)application:(UIApplication *)application
openURL:(NSURL *)url
sourceApplication:(NSString *)sourceApplication
annotation:(id)annotation
{
return [FBSession.activeSession handleOpenURL:url];
}
我为大代码片段道歉,但我不想遗漏任何可能对帮助有价值的东西。
打电话后
-(无效)openSession:
出现 facebook 身份验证页面,我授权该应用程序。之后,我期望调用 FB 委托方法:
-(BOOL)application:(UIApplication *)application
openURL:(NSURL *)url
sourceApplication:(NSString *)sourceApplication
annotation:(id)annotation
从来都不是。我是否需要将此类声明为一种 FB 委托?我正在使用可用的最新 Facebook SDK。
任何帮助都会很棒。