解析匿名用户不持久并导致会话错误
Parse Anonymous users does not persist and causing session errors
我目前正在使用 Parse 的匿名用户,在 didFinishLaunchingWithOptions
使用逻辑创建一个匿名用户,如果没有检测到 [PFUser currentUser]
,那么创建一个:
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);
}
}];
}
但是,我 运行 遇到这样一个问题:当我重新启动应用程序时,有时会 重新创建 匿名用户,或者当我尝试时它会提供会话错误将数据保存到同一个匿名用户。我应该执行其他检查吗?谢谢!
这里是我的解决方法,而不是使用解析匿名用户。它基于设备的 identifierForVendor
,对于每个特定安装的特定设备来说是唯一的。这意味着在正常情况下,用户只安装一次您的应用程序并在之后更新它,该字符串将保留。但是,如果您多次在测试设备上进行调试和 installing/deleting,这不是最佳解决方案。
下面的密码字段有一些非常骇人听闻的东西,您不应该为生产应用程序做这些事情。希望这有帮助。
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);
}
我目前正在使用 Parse 的匿名用户,在 didFinishLaunchingWithOptions
使用逻辑创建一个匿名用户,如果没有检测到 [PFUser currentUser]
,那么创建一个:
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);
}
}];
}
但是,我 运行 遇到这样一个问题:当我重新启动应用程序时,有时会 重新创建 匿名用户,或者当我尝试时它会提供会话错误将数据保存到同一个匿名用户。我应该执行其他检查吗?谢谢!
这里是我的解决方法,而不是使用解析匿名用户。它基于设备的 identifierForVendor
,对于每个特定安装的特定设备来说是唯一的。这意味着在正常情况下,用户只安装一次您的应用程序并在之后更新它,该字符串将保留。但是,如果您多次在测试设备上进行调试和 installing/deleting,这不是最佳解决方案。
下面的密码字段有一些非常骇人听闻的东西,您不应该为生产应用程序做这些事情。希望这有帮助。
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);
}