【问题标题】:Parse Anonymous users does not persist and causing session errorsParse Anonymous 用户不会持续存在并导致会话错误
【发布时间】:2015-08-21 20:03:09
【问题描述】:

我目前正在使用 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);
            }
        }];
}

但是,我遇到了一个问题,当我重新启动应用程序时,有时会重新创建匿名用户,或者当我尝试将数据保存给同一匿名用户时,它会提供会话错误。我应该执行其他检查吗?谢谢!

【问题讨论】:

  • 你找到解决这个问题的方法了吗?我现在的应用也遇到了同样的问题...
  • 感谢@Seishin 的提醒。让我回答我自己的问题。

标签: ios parse-platform pfuser


【解决方案1】:

这里是我的解决方法,而不是使用 Parse Anonymous User。它基于设备的identifierForVendor,对于每个特定安装的特定设备都是唯一的。这意味着在正常情况下,用户只安装一次您的应用程序并在之后更新它,该字符串将持续存在。但是,如果您在测试设备上进行多次调试和安装/删除,这不是最佳解决方案。

下面有一些关于密码字段的非常骇人听闻的东西,您不应该为生产应用程序这样做。希望这会有所帮助。

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

【讨论】:

  • daspianist,感谢您的快速回复和建议的解决方法。我只是想问你这对苹果来说好吗?我的意思是获取和存储设备的 UUID?据我所知,在Android中它是被禁止的。谢谢! :)
  • 好问题。该应用程序已获得 Apple 的批准,所以我认为它没问题。我相信这是因为 UUID 在新安装时确实会发生变化,这就是为什么这不是跟踪用户的好方法。但是,似乎有比我使用钥匙串的解决方案更好的解决方法 - 也许试一试? blog.onliquid.com/…
猜你喜欢
  • 1970-01-01
  • 2015-08-26
  • 2023-04-03
  • 2023-04-09
  • 1970-01-01
  • 2018-06-25
  • 1970-01-01
  • 1970-01-01
  • 2012-01-10
相关资源
最近更新 更多