Parse - 每次都重新创建 Facebook 用户

Posted

技术标签:

【中文标题】Parse - 每次都重新创建 Facebook 用户【英文标题】:Parse - Facebook users being recreated everytime 【发布时间】:2015-04-25 18:22:34 【问题描述】:

我正在使用 Parse.com + Facebook 用户,我注意到一些以前没有发生过的不寻常的事情。用户通过 Facebook 创建一个帐户,登录、注销、登录、注销,然后当他们尝试登录时,该用户被删除并创建另一个用户。为什么会这样?

这是我的注册/登录代码:

-(IBAction)facebookSignIn:(id)sender
    CLGeocoder *geo = [[CLGeocoder alloc] init];
    if(![CLLocationManager locationServicesEnabled] || [CLLocationManager authorizationStatus] == kCLAuthorizationStatusDenied )
        UIAlertView *locationAlert = [[UIAlertView alloc]initWithTitle:@"Oops!" message:@"You must have location services enabled for this app to work properly" delegate:self cancelButtonTitle:nil otherButtonTitles:@"Okay", nil];
        [locationAlert show];
    else



        [PFFacebookUtils logInWithPermissions:_permissions block:^(PFUser *aUser, NSError *suError) 
            if(!aUser)
                NSLog(@"not fbook user because %@",[suError description]);

                if([[[suError userInfo] objectForKey:@"com.facebook.sdk:ErrorLoginFailedReason"] isEqualToString:@"com.facebook.sdk:SystemLoginDisallowedWithoutError"])
                 UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Error" message:@"Looks like you have Facebook authentication disabled! Go to Settings > Facebook > mySwapp and turn the switch ON"delegate:nil cancelButtonTitle:nil otherButtonTitles:@"Okay", nil];
                    [alert show];

                
                else
                 UIAlertView  *alert = [[UIAlertView alloc]initWithTitle:@"Error Signing In/Logging In" message:[suError localizedDescription] delegate:nil cancelButtonTitle:nil otherButtonTitles:@"Okay", nil];
                    [alert show];
                

            
            else if(aUser.isNew)

                NSLog(@"User is NEW");
                 [[FBRequest requestForMe] startWithCompletionHandler:
                 ^(FBRequestConnection *connection,
                 NSDictionary<FBGraphUser> *user,
                 NSError *fbError) 
                 if (!fbError) 

                 NSLog(@"Facebook Request succeeded");

                 NSString *email = [user objectForKey:@"email"];
                 [aUser setEmail:email];

                 PFQuery *g = [PFQuery queryWithClassName:@"Counter"];
                 PFObject *cool = [g getObjectWithId:@"gpKDgNhwhw"];
                 [cool incrementKey:@"users"];
                 [cool saveEventually];
                 NSString *username = [NSString stringWithFormat:@"blahblah%d",[[cool objectForKey:@"users"] intValue]];
                 [aUser setUsername:username];
                 PFInstallation *installation = [PFInstallation currentInstallation];
                 [installation setObject:aUser forKey:@"user"];

                 [aUser setObject:@NO forKey:@"text"];
                 [aUser setObject:@YES forKey:@"snew"];
                 [aUser setObject:@"All" forKey:@"prefState"];
                 [aUser setObject:@"All" forKey:@"prefCat"];
                     [aUser setObject:@YES forKey:@"fnew"];
                 _type = @"facebook";

                 NSLog(@"Right before geopoint search....");

                 [PFGeoPoint geoPointForCurrentLocationInBackground:^(PFGeoPoint *geoPoint, NSError *error) 
                 if(!error)
                 NSLog(@"Got current geopoint!");
                 CLLocation *myLocation = [[CLLocation alloc]initWithLatitude:geoPoint.latitude longitude:geoPoint.longitude];
                 [geo reverseGeocodeLocation:myLocation completionHandler:^(NSArray *placemarks, NSError *error) 
                 if(!error)
                 CLPlacemark *pl = placemarks[0];

                 NSString *zip = [pl.addressDictionary objectForKey:(NSString *)kABPersonAddressZIPKey];
                 NSString *city = [pl.addressDictionary objectForKey:(NSString *)kABPersonAddressCityKey];
                 NSString *state = [pl.addressDictionary objectForKey:(NSString *)kABPersonAddressStateKey];
                 if(city == nil ||state ==nil)
                 NSLog(@"city or state is nil");
                 if(city==nil)
                 NSLog(@"city is nil");
                 
                 if(state==nil)
                 NSLog(@"state is nil");
                 
                 
                 [aUser setObject:city forKey:@"city"];
                 [aUser setObject:state forKey:@"state"];
                 [aUser setObject:zip forKey:@"zip"];
                 [aUser setObject:geoPoint forKey:@"geopoint"];
                 [aUser setObject:@NO forKey:@"pref"];
                 [aUser setObject:@20 forKey:@"radius"];
                     [aUser setObject:@0 forKey:@"postCount"];

                 [aUser saveInBackgroundWithBlock:^(BOOL succeeded, NSError *perror) 
                 if(!perror && succeeded)

                 [self performSegueWithIdentifier:@"registerMe" sender:self];                                          
                 else

                     CCAlertView *errorAlert = [[CCAlertView alloc]initWithTitle:@"Oops!" message:[NSString stringWithFormat:@"%@. If you have already registered, please login regularly and go to the settings tab and switch on \"Link to Facebook\".",[[perror userInfo] objectForKey:@"error"]]];
                     [errorAlert addButtonWithTitle:@"Okay" block:^
                         [aUser deleteInBackground];
                     ];
                     [errorAlert show];
                 
                 ];
                 
                 else
                     CCAlertView *errorAlert = [[CCAlertView alloc]initWithTitle:@"Error getting Facebook data" message:[[fbError userInfo] objectForKey:@"error"]];
                     [errorAlert addButtonWithTitle:@"Okay" block:^
                         [aUser deleteInBackground];
                     ];
                     [errorAlert show];

                 
                 ];
                 
                 else
                CCAlertView *errorAlert = [[CCAlertView alloc]initWithTitle:@"Facebook Sign In/Sign Up" message:[[suError userInfo] objectForKey:@"error"]];
                     [errorAlert addButtonWithTitle:@"Okay" block:^

                         [aUser deleteInBackground];

                     ];


                 [errorAlert show];
                     NSString *ciid = [[PFInstallation currentInstallation] objectId];
                     [PFCloud callFunctionInBackground:@"logError" withParameters:@@"installation":ciid,@"message":[suError description],@"place":@"Facebook Sign In/Sign Up" block:^(id object, NSError *error) 
                         if(error)
                             PFObject * errorObj = [PFObject objectWithClassName:@"Error"];
                             [errorObj setObject:ciid forKey:@"installation"];
                             [errorObj setObject:[suError description] forKey:@"message"];
                             [errorObj setObject:@"Facebook Sign In/Sign Up" forKey:@"place"];
                             [errorObj saveEventually];
                         
                     ];
                 
                 ];


                 

                 ];










            
            else
                NSLog(@"User is OLD");
                [self performSegueWithIdentifier:@"showMain" sender:self];                        

        ];





    

  

这是我的注销代码:

- (IBAction)goBackNow:(id)sender 
    NSLog(@"gobacknow called");
    [PFUser logOut];

    [self.navigationController popToRootViewControllerAnimated:YES];


【问题讨论】:

【参考方案1】:

您可以尝试在注销方法中添加更多内容。例如

[FBSession.activeSession closeAndClearTokenInformation];
[FBSession.activeSession close];
[FBSession setActiveSession:nil];

PFInstallation *installation = [PFInstallation currentInstallation];
installation[@"user"] = [NSNull null];
[installation saveInBackground];

还记得在你的例子中[installation saveInBackground];

【讨论】:

以上是关于Parse - 每次都重新创建 Facebook 用户的主要内容,如果未能解决你的问题,请参考以下文章

如何在新数据上更新时间序列模型(例如 facebook_prophet),而无需每次都重新训练数据? [关闭]

Parse / Facebook -> 会话与当前存储的会话不匹配

保存电子邮件地址 Facebook + Parse (Swift)

从 parse 获取 Facebook 分数上的朋友

在 iOS9 和 Swift2 中使用 Parse 登录 Facebook

无法使用 Swift 在 Xcode 中使用 Parse 1.7.2 和 Facebook SDK 4.1 创建新用户