2015-04-25 54 views
0

我使用的是Parse.com + Facebook用戶,我注意到以前沒有發生的異常事情。用戶通過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]; 

} 

回答

0

你可以嘗試添加更多的東西註銷方法。例如

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

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

還記得[installation saveInBackground];在你的榜樣

相關問題