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);
}