我正在尝试使用FBConnect SDK连接到Facebook。用户首次尝试登录时一切正常:调用委托方法session:didLogin
,然后我就可以获得更新用户状态和上传图片的扩展权限。
但是,当用户点击注销按钮时,尝试再次连接,从第二次开始,始终会导致session:didLogin
从未调用。在缓存会话时(用户单击FBLoginDialog中的复选框)和不缓存会话时都会发生这种情况。
我只需要在会话建立时正确识别,以便开始显示用户需要点击的按钮以获取扩展权限。
以下可能性中的正确/预期行为是什么?
1)如果会话被缓存,那么第二次用户登录session:didLogin
将不会被调用但会话实际上是连接的(即_session.IsConnected
必须是YES
)而且没有还需要做其他事情来建立会议;
2)如果会话被缓存,那么用户登录session:didLogin
的第二次将不会被调用且会话未连接,因此需要进一步的操作来建立会话(我该怎么办?这种情况?);
3)如果未缓存会话,则将调用用户登录session:didLogin
的第二次并建立会话;
相关代码如下。如果出现问题,请告诉我您当前的最佳做法是实现多个正确的登录,而与会话状态无关(缓存与否)。提前谢谢。
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
if (self = [super initWithNibName:@"FacebookViewController" bundle:nibBundleOrNil]) {
if (kGetSessionProxy) {
_session = [[FBSession sessionForApplication:kApiKey getSessionProxy:kGetSessionProxy delegate:self] retain];
} else {
_session = [[FBSession sessionForApplication:kApiKey secret:kApiSecret delegate:self] retain];
}
}
return self;
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
BOOL resumed = [_session resume];
_loginButton.style = FBLoginButtonStyleWide;
if (_session.isConnected) {
_permissionButton.hidden = NO;
}
else{
_permissionButton.hidden = YES;
}
_statusButton.hidden = YES;
_photoButton.hidden = YES;
if(([_session isConnected] || resumed) && self.name){
_label.text = [NSString stringWithFormat:@"%@ %@", NSLocalizedString(@"Logged in as", nil), self.name];
}
else{
_label.text = [NSString stringWithFormat:@"%@", NSLocalizedString(@"You are not logged in", nil)];
}
}
- (void)session:(FBSession*)session didLogin:(FBUID)uid {
NSLog(@"session:didLogin:");
if(_session && session != _session){
[_session release], _session = nil;
}
if(!_session){
_session = [session retain];
}
_label.text = @"";
_permissionButton.hidden = YES;
_statusButton.hidden = YES;
_photoButton.hidden = YES;
NSString* fql = [NSString stringWithFormat:@"select uid,name from user where uid == %lld", session.uid];
NSDictionary* params = [NSDictionary dictionaryWithObject:fql forKey:@"query"];
[[FBRequest requestWithDelegate:self] call:@"facebook.fql.query" params:params];
}
- (void)dealloc {
[_session.delegates removeObject: self];
[_session release], _session = nil;
[_permissionButton release], _permissionButton = nil;
[_statusButton release], _statusButton = nil;
[_photoButton release], _photoButton = nil;
[name release], name = nil;
[super dealloc];
}
答案 0 :(得分:0)
现在它有效。以下是处理会话的相关代码。我没有显示其他方法,因为会话实例在其他地方没有以任何方式修改。我希望这可能对其他人有用。
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
if (self = [super initWithNibName:@"FacebookViewController" bundle:nibBundleOrNil]) {
if (kGetSessionProxy) {
_session = [[FBSession sessionForApplication:kApiKey getSessionProxy:kGetSessionProxy delegate:self] retain];
} else {
_session = [[FBSession sessionForApplication:kApiKey secret:kApiSecret delegate:self] retain];
}
}
return self;
}
- (void)viewDidLoad {
[_session resume];
...
}
- (void)dealloc {
[_session.delegates removeObject: self];
[_session release], _session = nil;
...
[super dealloc];
}