我正在首次尝试将 FB 与 iOS SDK 集成。
到目前为止,我已经成功地与我的 fb 应用程序建立了连接FBLoginView
。
这是我相应的调用:
-(void)loginViewShowingLoggedInUser:(FBLoginView *)loginView{
NSLog(@"logged in");
}
-(void)loginViewShowingLoggedOutUser:(FBLoginView *)loginView{
NSLog(@"logged out");
[FBSettings setLoggingBehavior:[NSSet setWithObjects:FBLoggingBehaviorFBRequests, nil]];
if (FBSession.activeSession.isOpen) {
[FBRequestConnection startForMeWithCompletionHandler:^(FBRequestConnection *connection,id<FBGraphUser> user,NSError *error) {
if (!error) {
NSString *fbID = user.id;
NSLog(@"UserID: %@",fbID);
NSLog(@"TESTING: %@",user.name);
}
}];
}
}
- (void)loginViewFetchedUserInfo:(FBLoginView *)loginView
user:(id<FBGraphUser>)user {
NSLog(@"Hello %@!", user.first_name);
}
- (void)loginView:(FBLoginView *)loginView handleError:(NSError *)error {
NSLog(@"FBLoginView encountered an error=%@", error);
}
我有 1 个小问题和 1 个大问题(主题)。
小:请求得到满足,我得到了一个很好的日志:
Response Body:
(
{
body = {
gender = male;
id = ###;
...
};
code = 200;
}
)
但不知何故,我的 2 个测试日志没有显示,有什么想法吗?
大(主题):我在我的应用程序的指定控制器中建立了该连接。我的问题是,每次重新启动应用程序时,连接都会以某种方式丢失(会话不再处于活动状态)。有没有办法使用建立无限连接FBLoginView
?
我在我的 appdelegate 中添加了以下调用和方法:
//upon start:
if (![FBSession activeSession].isOpen) {
[self connectWithFacebook];
}
- (BOOL)openSessionWithAllowLoginUI:(BOOL)allowLoginUI
{
//..
return [FBSession openActiveSessionWithReadPermissions:permissions
allowLoginUI:allowLoginUI
completionHandler:^(FBSession *session, FBSessionState state, NSError *error) {
if (error) {
NSLog (@"Handle error %@", error.localizedDescription);
} else {
[FBSession setActiveSession:session];
[self checkSessionState:state];
}
}];
}
- (void) connectWithFacebook {
[self openSessionWithAllowLoginUI:YES];
}
- (void) checkSessionState:(FBSessionState)state {
switch (state) {
case FBSessionStateOpen:
break;
case FBSessionStateCreated:
break;
case FBSessionStateCreatedOpening:
break;
case FBSessionStateCreatedTokenLoaded:
break;
case FBSessionStateOpenTokenExtended:
// I think this is the state that is calling
break;
case FBSessionStateClosed:
break;
case FBSessionStateClosedLoginFailed:
break;
default:
break;
}
}
这允许重新登录,但每次打开应用程序时都会再次显示登录屏幕,这不是很用户友好。有没有解决方案,如果是,我错过了什么或我可以朝什么方向前进?