解析匿名用户不会持久存在并导致会话错误

时间:2015-08-21 20:03:09

标签: ios parse-platform pfuser

我目前正在使用Parse的匿名用户,使用如果没有检测到[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); } }]; } 创建一个匿名用户,然后创建一个:

<h1 class="title">Some Value
    <a href="#">link 1</a>
    <a href="#">link 2</a>
    <a href="#">link 3</a>
    <a href="#">link 4</a>
</h1>

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

1 个答案:

答案 0 :(得分:0)

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

下面有一些非常hacky的东西,密码字段,你不应该为生产应用程序做。希望这会有所帮助。

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