简体   繁体   English

如何唯一地识别ios设备

[英]how to identify ios device uniquely

In my current application,i have to let user to login from different iOS devices to their account. 在我目前的应用程序中,我必须让用户从不同的iOS设备登录到他们的帐户。 Currently i'm doing user authentication from a token value. 目前我正在从令牌值进行用户身份验证。 but in order to support multiple device login i have to find another way for doing this. 但为了支持多设备登录,我必须找到另一种方法来做到这一点。

Thus, I thought of saving devices uuid along with token for authentication + security. 因此,我想到将设备uuid与用于身份验证+安全性的令牌一起保存。 Then, I come to know I can't use device's uuid , instead I have to use identifierForVendor which may or may not provide user or device information always. 然后,我开始知道我不能使用设备的uuid ,而是我必须使用identifierForVendor ,它可能会或可能不会始终提供用户或设备信息。

So, can anybody suggest the better and proper way of achieving this multiple device login feature for same user account in ios ? 那么,任何人都可以建议在ios中为同一个用户帐户实现这种多设备登录功能的更好和正确的方法吗?

As you already know this using the device's UUID isn't allowed, however, you can generate your own UUID and store it on the devices' UserDefaults. 如您所知,不允许使用设备的UUID,您可以生成自己的UUID并将其存储在设备的UserDefaults上。

using the identifierForVendor isn't 100% reliable, as it only works on iOS6 and above, and users have the ability to opt-out of giving it to you, which makes it a bad choice. 使用identifierForVendor不是100%可靠,因为它只适用于iOS6及更高版本,用户可以选择不给你,这使它成为一个糟糕的选择。

Here's some code I copied of the internets sometime ago and still use it till today, will try to find the source and update my answer in a bit. 这是我之前复制的互联网的一些代码,直到今天仍然使用它,将尝试找到源并稍微更新我的答案。 EDIT: Source 编辑: 来源

This will generate and store a UUID for you in UserDefaults: 这将在UserDefaults中为您生成并存储UUID:

- (NSString *)createUUID
{
  CFUUIDRef theUUID = CFUUIDCreate(NULL);
  CFStringRef string = CFUUIDCreateString(NULL, theUUID);
  CFRelease(theUUID);
  [[NSUserDefaults standardUserDefaults] setObject:(__bridge NSString *)string forKey:@"UUID"];
  [[NSUSerDefaults standardUserDefaults] synchronize];
  return (__bridge NSString *)string;
}

And whenever you need to read the generated UUID: 无论什么时候需要读取生成的UUID:

- (NSString*)UUID
{
    return [[NSUserDefaults standardUserDefaults] ObjectForKey:@"UUID"];
}

Now you have the choice to append your own user's ID to that too so you'll be able to know what UUID is linked to which user.. 现在您可以选择将自己的用户ID附加到该用户身上,这样您就可以知道UUID与哪个用户相关联。

This is just a rough sketch of how it should work 这只是它应该如何工作的粗略草图

First of all, Apple developer guidelines prohibit/ discourage use of IDFA for tracking the user for the purpose of displaying targeted advertisements (and a few other things). 首先,Apple开发人员指南禁止/阻止使用IDFA跟踪用户以显示目标广告(以及其他一些内容)。 The guidelines clearly allow the developer to use the IDFA for identifying the device for security purposes. 该指南明确允许开发人员出于安全目的使用IDFA来识别设备。 Quoting the apple guidelines 引用苹果指南

advertisingTrackingEnabled advertisingTrackingEnabled

A Boolean value that indicates whether the user has limited ad tracking. 一个布尔值,指示用户是否具有有限的广告跟踪。 (read-only) (只读)

@ property (nonatomic, readonly, getter=isAdvertisingTrackingEnabled) BOOL advertisingTrackingEnabled @ property (nonatomic,readonly,getter = isAdvertisingTrackingEnabled)BOOL advertisingTrackingEnabled

Discussion 讨论

Check the value of this property before performing any advertising tracking. 在执行任何广告跟踪之前,请检查此媒体资源的价值。 If the value is NO, use the advertising identifier only for the following purposes: frequency capping, conversion events, estimating the number of unique users, security and fraud detection, and debugging. 如果值为“否”,则仅将广告标识符用于以下目的:频次上限,转换事件,估计唯一用户的数量,安全性和欺诈检测以及调试。

You can use IDFA of the device for the purpose of multiple device logins. 您可以使用设备的IDFA进行多个设备登录。 The flow would be somewhat like this: 流程有点像这样:

  1. User logs in to the server using device A, Server sends back a token which is stored on the device in NSUserDefaults . 用户使用设备A登录服务器,服务器发回一个存储在NSUserDefaults设备上的NSUserDefaults The app also stores the IDFA on the device in NSUserDefaults 该应用程序还将IDFA存储在NSUserDefaults的设备上

  2. This token will be used for creating an encrypted string which would contain the IDFA. 此令牌将用于创建包含IDFA的加密字符串。 (encrypt the IDFA using the token) The encrypted value would be passed to the server in each request along with the original IDFA. (使用令牌加密IDFA)加密值将与原始IDFA一起传递到每个请求中的服务器。

  3. The server would then use the IDFA and the token associated with it (the server would of course be storing the IDFA's corresponding to each token) to get the encrypted value of the IDFA and match it with the encrypted value received in the request. 然后,服务器将使用IDFA和与其关联的令牌(服务器当然会存储与每个令牌对应的IDFA)以获取IDFA的加密值,并将其与请求中接收的加密值进行匹配。 The purpose of doing this is to ensure that no one can hack into your server as the token would not be visible to anyone but the app (You can even store the token in an encrypted format so as to increase the level of security). 这样做的目的是确保没有人可以入侵您的服务器,因为除了应用程序之外的任何人都看不到令牌(您甚至可以以加密格式存储令牌以提高安全级别)。

  4. Whenever a request is sent to the server, the value of IDFA stored on the device in NSUserDefaults would be compared with the current IDFA. 每当向服务器发送请求时, NSUserDefaults存储在设备上的IDFA值将与当前IDFA进行比较。

  5. In case there is a mismatch, the current IDFA would be first updated to the server and then after getting the confirmation of successful update the app would replace the IDFA stored on the device in NSUserDefaults with the current one (and business then runs as usual). 如果不匹配,当前IDFA将首先更新到服务器,然后在获得成功更新确认后,应用程序将使用当前NSUserDefaults替换存储在NSUserDefaults的设备上的IDFA(然后业务照常运行) 。

Alternatively you can avoid step 3,4 and storing IDFA on the device in NSUserDefaults but in that can the user would have to re-login on to the server on resetting the IDFA. 或者,您可以避免步骤3,4并在NSUserDefaults IDFA存储在设备上,但是在这种情况下,用户必须在重置IDFA时重新登录到服务器。

Just confirming ,the mapping of token to IDFA would be many to one. 只需确认,令牌与IDFA的映射将是多对一的。

Hope this helps, comment in case anything not clear/ not satisfying the use case. 希望这有帮助,评论以防任何不清楚/不满足用例。

you should use the standard ways of creating a UUID. 您应该使用创建UUID的标准方法。 Apple does not want you tracking devices. Apple不希望您跟踪设备。

 To create a unique identifier specific to your app, you can call the CFUUIDCreate function to
 create a UUID, and write it to the defaults database using the NSUserDefaults class. (Source)

If you want to use a library for this instead of rolling your own, you should use this excellent library like this : 如果你想使用一个库而不是自己编辑,你应该像这样使用这个优秀的库:

CFUUIDRef uuidRef = CFUUIDCreate(kCFAllocatorDefault);
NSString *uuidString = (NSString *)CFUUIDCreateString(NULL,uuidRef);
CFRelease(uuidRef);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM