唯一识别 iOS 用户 [英] Uniquely identifying an iOS user

查看:26
本文介绍了唯一识别 iOS 用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在服务器上为应用程序的新用户创建一个用户帐户,但我也不希望用户输入任何内容.理想情况下,我希望这是自动的,就像 Game Center 一样.

I'd like to create a user account on the server for new users of an app, but I'd also like to not ask the user to type in anything. Ideally, I'd like this to be automatic, like with Game Center.

但我想知道这是否可能.有什么我可以用来唯一标识用户的吗?我极不可能找到用户的 Apple ID.另外,设备 ID 唯一标识的是设备,而不是用户,所以如果用户有更多的设备,这将毫无用处...

But I'm wondering if it's possible. Is there anything I can use to uniquely identify the user? It's highly unlikely that I can find out the user's Apple ID. Also, the device ID uniquely identifies the device, not the user, so it would be useless if the user has more devices...

还有什么我可以用的吗?

Is there anything else I can use?

关于隐私 - 我不想在用户背后发现任何事情.我完全没有问题要求用户访问他们的信息(如果有一个 API 可以授予我这些信息,如果 API 本身询问这个会很棒).正如史蒂夫·乔布斯本人所说,这就是隐私的全部意义所在 - 强制应用在对用户的私人数据进行任何操作之前征求用户的许可.

About privacy - I don't want to find out anything behind the user's back. I have absolutely no problem with asking the user for access to their information (and if there is an API that grants me this information, it would be great if the API asks this itself). As Steve Jobs himself said, this is what privacy is all about - forcing apps to ask the user for permission before doing anything with their private data.

推荐答案

正确的解决方案是使用iCloud 键值存储,您可以在其中存储唯一的用户 ID,而无需任何类型的身份验证或用户信息(例如电子邮件地址).

The correct solution is to use the iCloud Key-Value Store, where you can store a unique user ID without requiring any kind of authentication or user information such as an email address.

最终结果是一个随机的 UUID(实际上并不能识别用户),每个用户都不同,但会在注册到同一 iCloud 帐户的多台设备上持续存在.

我们在 iCloud KV Store 中定义了一个字段,我们称之为 userID.当应用程序启动时,我们首先检查用户 ID.如果它在那里,那么我们都设置了我们用户的唯一 ID.如果没有,那么这是我们第一次为该用户运行.我们生成一个随机的 UUID 并将其存储在 userID 下的 KV Store 中.这就是全部.

We define a field in our iCloud KV Store, let's call it userID. When the app is launched, we first check the userID. If it's there, then we're all set with our user's unique ID. If not, then this is the first time we're running for this user. We generate a random UUID and store it in the KV Store under userID. That's all there is to it.

我们的经验表明,此 UUID 对于每个 iTunes 帐户都是唯一的.如果您的最终用户正在使用家庭共享,那么这些帐户将被分配不同的 UUID(这可能是也可能不是可取的,但您无能为力).在同一 iTunes 帐户下启动的任意数量的设备都会看到相同的 UUID.

Our experience shows that this UUID is unique per iTunes account. If your end-users are using family sharing, those accounts will be assigned different UUIDs (which may or may not be desirable but there's nothing you can do about it). Any number of devices launching under the same iTunes account will see the same UUID.

这种方法是完全合法的,应该得到 Apple 的批准,没有问题.

This approach is totally legit and should be approved by Apple with no issues.

显然您必须在 Xcode 上的 Capabilities 下启用 iCloud Key-Value store,只需打开 iCloud Switch.

这是一个在 Objective-C 中实现这个概念的简单类:

Here's a simple class that implements this concept in Objective-C:

@implementation EEUserID

+ (NSUUID *) getUUID
{
    NSUUID *uuid = nil;
    NSString *uuidString = [[NSUbiquitousKeyValueStore defaultStore] stringForKey: @"EEUserID"];
    if (uuidString == nil)
    {
        // This is our first launch for this iTunes account, so we generate random UUID and store it in iCloud:
        uuid = [NSUUID UUID];
        [[NSUbiquitousKeyValueStore defaultStore] setString: uuid.UUIDString forKey: @"EEUserID"];
        [[NSUbiquitousKeyValueStore defaultStore] synchronize];
    }
    else
    {
        uuid = [[NSUUID alloc] initWithUUIDString: uuidString];
    }
    
    return uuid;
}

+ (NSString *) getUUIDString
{
    NSUUID *uuid = [self getUUID];
    if (uuid != nil)
        return uuid.UUIDString;
    else
        return nil;
}

+ (void) load
{
    // get changes that might have happened while this
    // instance of your app wasn't running
    [[NSUbiquitousKeyValueStore defaultStore] synchronize];
}

@end

对于头文件:

#import <Foundation/Foundation.h>

@interface EEUserID : NSObject

+ (NSUUID *) getUUID;
+ (NSString *) getUUIDString;

@end

要使用,您只需调用:

NSString *uniqueIDForiTunesAccount = [EEUserID getUUIDString];

享受.

这篇关于唯一识别 iOS 用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆