Search code examples
iosparse-platformpfuser

Parse Anonymous users does not persist and causing session errors


I am currently using Parse's Anonymous user, creating an anonymous user at didFinishLaunchingWithOptions using the logic that if no [PFUser currentUser] is detected, then make one:

if ([PFUser currentUser]){
        NSLog(@"there is a current user");   
} else {
        [PFUser enableAutomaticUser];
        PFACL *defaultACL = [PFACL ACL];
        [PFACL setDefaultACL:defaultACL withAccessForCurrentUser:YES];
        NSLog(@"make an Automatic user");
        [[PFUser currentUser] saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error){
            if (error){
                NSLog(@"Error: %@", error);
            }
        }];
}

However, I am running into an issue where anonymous users are sometimes re-created when I restart the app, or it will provide session errors when I try to save data to the same anonymous user. Are there additional checks that I should be performing? Thanks!


Solution

  • Instead of using Parse Anonymous User, here is my workaround. It's based on the device's identifierForVendor, which is unique to the specific device per the particular installation. This means that in normal situations, where the user only install your app once and update it afterwards, the string will persist. However, if you're doing debugging and installing/deleting on your test device multiple times, this isn't the optimal solution.

    There are some pretty hacky stuff below with the password field, which you shouldn't do for a production app. Hope this helps.

    if (![PFUser currentUser]){
            NSString *uuid = [[UIDevice currentDevice] identifierForVendor].UUIDString;
    
            PFQuery *userQuery = [PFUser query];
            [userQuery whereKey:@"username" equalTo:uuid];
            [userQuery findObjectsInBackgroundWithBlock:^(NSArray * _Nullable objects, NSError * _Nullable error) {
                if (error){
                    NSLog(@"Error: %@", error);
                } else {
                    if (objects.count == 0){
    
                        //Make new user
                        PFUser *newUser = [PFUser user];
                        newUser.username = uuid;
                        newUser.password = @"123456";
    
                        NSString *deviceType = [UIDeviceHardware platformString];
                        if (deviceType){
                            [newUser setObject:deviceType forKey:@"deviceName"];
                        }
    
                        [newUser signUpInBackgroundWithBlock:^(BOOL succeeded, NSError * _Nullable error) {
                            if (error){
                                NSLog(@"Error: %@", error);
                            } else {
                                NSLog(@"New user signed up: %@", newUser.username);
                                NSLog(@"done");
                            }
                        }];
                    } else {
    
                        NSString *newUUID = [NSString stringWithFormat:@"%@-%@", uuid, [self generate4Alphanumeric]];
                        PFUser *newUser = [PFUser user];
                        newUser.username = newUUID;
                        newUser.password = @"123456";
    
                        NSString *deviceType = [UIDeviceHardware platformString];
                        if (deviceType){
                            [newUser setObject:deviceType forKey:@"deviceName"];
                        }
    
                        [newUser signUpInBackgroundWithBlock:^(BOOL succeeded, NSError * _Nullable error) {
                            if (error){
                                NSLog(@"Error: %@", error);
                            } else {
                                NSLog(@"Modified user signed up: %@", newUser.username);
                                NSLog(@"done");
                            }
                        }];
    
                    }
                }
            }];
    
        } else {
            NSLog(@"currentUser: %@", [PFUser currentUser].username);
        }