Facebook loginViewFetchedUserInfo被调用两次

Idr*_*raf 16 iphone facebook-graph-api ios facebook-ios-sdk

我在我的应用程序中使用facebook SDK 3.0.登录到Facebook后,委托方法被调用两次.

- (void)loginViewFetchedUserInfo:(FBLoginView *)loginView
                            user:(id<FBGraphUser>)user {
    //loginThroughFb=TRUE;
    NSString *userId=[[NSString alloc] initWithString:[user id]];
    [self soapCallForLogin:@"" password:@"" deviceId:@"" fbid:userId];
    NSLog(@"%@",userId);
    [userId release];

}
Run Code Online (Sandbox Code Playgroud)

Hej*_*azi 9

我尝试了'HelloFacebookSample'项目,该方法只调用一次.

所以我想这种情况的最佳解决方案是保持对最后一个用户对象的引用,并将其与下一次调用的新对象进行比较,如果它们相等,则可以忽略该调用.

- (void)loginViewFetchedUserInfo:(FBLoginView *)loginView user:(id<FBGraphUser>)user {
    if (![self isUser:cachedUser equalToUser:user]) {
        cachedUser = user;
        /// Do something
    }
}

- (BOOL)isUser:(id<FBGraphUser>)firstUser equalToUser:(id<FBGraphUser>)secondUser {
    return
        [firstUser.objectID isEqual:secondUser.objectID] &&
        [firstUser.name isEqual:secondUser.name] &&
        [firstUser.first_name isEqual:secondUser.first_name] &&
        [firstUser.middle_name isEqual:secondUser.middle_name] &&
        [firstUser.last_name isEqual:secondUser.last_name] &&
        ...
}
Run Code Online (Sandbox Code Playgroud)


gil*_*lyD 7

我也有这个问题.我设法用一个丑陋的黑客修复它,但它的工作原理.我在FBLoginView委托中保留了一个计数器.当调用fetchedUserInfo时,我检查计数器.如果大于零,则返回.否则,做两件事 - 1.增加消息计数器2.触发延迟事件,再次将消息计数器归零.

所以你的fetchedUserInfo方法将如下所示:

- (void)loginViewFetchedUserInfo:(FBLoginView *)loginView
                        user:(id<FBGraphUser>)user {


       if ([self messageCounter] >0)
           return;
       else
           {
    self.messageCounter++;
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 3 * NSEC_PER_SEC), dispatch_get_current_queue(), ^{
        [self setMessageCounter:0];
    });}
// Do whatever you were going to do }
Run Code Online (Sandbox Code Playgroud)


owe*_*nfi 5

在2013年9月18日发布的FB SDK 3.8中已修复.无论重复注销和重新登录多少次,都会在每次登录时调用一次委托方法.


我也能够在FB SDK 3.7.1和他们自己的示例程序"Scrumptious"中重现这一点

如上所述(至少对我来说)这只发生在:

  1. 登录一次
  2. 注销
  3. 重新登录(现在发生)

有趣的是重新登录的调用顺序:

在第一次登录时,我看到的电话是:

- (void)loginViewShowingLoggedInUser:(FBLoginView *)loginView;
- (void)loginViewFetchedUserInfo:(FBLoginView *)loginView user:(id<FBGraphUser>)user;
Run Code Online (Sandbox Code Playgroud)

在第二次(及以后)登录中,我看到:

- (void)loginViewFetchedUserInfo:(FBLoginView *)loginView user:(id<FBGraphUser>)user;
- (void)loginViewShowingLoggedInUser:(FBLoginView *)loginView;
- (void)loginViewFetchedUserInfo:(FBLoginView *)loginView user:(id<FBGraphUser>)user;
Run Code Online (Sandbox Code Playgroud)

这为在中间方法中设置标志提供了一个方便的小解决方法,如下所示:

- (void)loginViewShowingLoggedInUser:(FBLoginView *)loginView {
    // Set flag
    self.isFirstLoginDone = YES;
}

- (void)loginViewFetchedUserInfo:(FBLoginView *)loginView user:(id<FBGraphUser>)user {

    // Check
    if(self.isFirstLoginDone) {
        // Execute code I want to run just once
        NSLog(@"fetched");
    }

    // Don't forget to clear the flag (I guess it shouldn't matter if everything is cleaned up)
    self.isFirstLoginDone = NO;
}
Run Code Online (Sandbox Code Playgroud)