我在其他几篇文章中看到了这个问题,但似乎没有什么对我有用。在关注FB login guide后,我有一个工作原型,允许用户通过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。
任何帮助都会很棒。