Search code examples
iphoneiosfacebook-graph-apifacebook-ios-sdk

Facebook loginViewFetchedUserInfo is called twice


I am using facebook SDK 3.0 in my app. The delegate method is called twice when after logging to 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];

}

Solution

  • I tried 'HelloFacebookSample' project and the method is called only once.

    So I guess the best solution for such case is to keep a reference to the last user object and compare it to the new object you get the next call, and if they're equal you can just ignore that call.

    - (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] &&
            ...
    }