简体   繁体   English

在iOS中唯一标识设备

[英]Uniquely identify the device in iOS

I am developing a corporate application on the iPad for a certain business requirement. 我正在为某些业务需求在iPad上开发公司应用程序。 This app is meant to use in a specific number of devices which is predefined by the admin. 该应用旨在用于由管理员预定义的特定数量的设备。

But I also need the application to reject any login requests even if it is from an authorised user,when he or she is using a device which is not defined by the admin. 但是我也需要该应用程序拒绝任何登录请求,即使来自授权用户的登录请求(当他或她使用管理员未定义的设备时)也是如此。

Edits: 编辑:

Say I have 2 devices and I have my credentials to login to the app, And my need is, to restrict the login from the devices which is not mine. 假设我有2台设备,并且我具有登录该应用程序的凭据,并且我需要限制从不是我的设备的登录。 For that I have to identify whether the login request is comes from my device or not. 为此,我必须确定登录请求是否来自我的设备。

Previously we could use device UDID to do this, but now it is deprecated. 以前我们可以使用设备UDID来执行此操作,但是现在不建议使用。

Can any one please suggest a method to implement this ? 有人可以建议一种实现此方法的方法吗?

try this. 尝试这个。 for more info check UIDevice 有关更多信息,请检查UIDevice

// IOS 6+
    NSString *uniqueIdentifier = [[NSString alloc] initWithString:[[[UIDevice currentDevice] identifierForVendor] UUIDString]];

You can use iCloud over here because UUID has been deprecated and vendorId is uniqe but might be change if you uninstall the app and install it agian, 您可以在此处使用iCloud ,因为UUID已被弃用,而且vendorId是uniqe,但是如果您卸载该应用并将其安装为Agian,则可能会更改,

So I would suggest iCloud will be safer, what you cna do is at the time of application launch you can generate one token which is unique and save it to your iCloud data storage along with user credentials, 因此,我建议iCloud会更安全,您要做的就是在应用程序启动时生成一个唯一的令牌,并将其与用户凭证一起保存到iCloud数据存储中,

So from the next time onwards when user will try to login you can check it with iCloud. 因此,从下一次起,当用户尝试登录时,您可以使用iCloud进行检查。

How about using Apple's enterprise distribution system? 如何使用Apple的企业发行系统? That will allow you to deploy the app to a corporation, and have tight access control. 这样您就可以将应用程序部署到公司,并具有严格的访问控制。

https://developer.apple.com/programs/ios/enterprise/ https://developer.apple.com/programs/ios/enterprise/

Just Trick: Just Trick:

You can Implement APNS code to your Project and Get Device Token. 您可以对项目实施APNS代码并获取设备令牌。

The Device Token is Unique One. 设备令牌是唯一的。 But The user Must Allow the APNS. 但是用户必须允许APNS。

Note: APNS Device Token is changed to Following reasons. 注意:APNS设备令牌更改为以下原因。

  1. Change of Bundle id. 捆绑包ID的更改。
  2. Change of Development Mode(Sandbox/Production) 开发方式变更(沙盒/生产)

I have implemented a solution for exactly your problem, The best solution (and Apples recommended route) is to create a UUID unique to your App like this: 我已经针对您的问题实施了一种解决方案,最好的解决方案(以及Apple推荐的解决方案)是为您的应用创建唯一的UUID,如下所示:

    NSString *uuidString = nil;
    CFUUIDRef uuid = CFUUIDCreate(NULL);
    if (uuid) {
        uuidString = (NSString *)CFBridgingRelease(CFUUIDCreateString(NULL, uuid));
        CFRelease(uuid);
    }

Then, and this is the key, you can store that to the iOS keychain (Handy classes here: https://github.com/lukef/IXKeychain ) and values in the iOS keychain are NOT removed when the user uninstalls the App, so you can persist your own UUID through App installs which is a key part of managing a specific number of devices against a user account. 然后,这就是密钥,您可以将其存储到iOS钥匙串(此处为便捷类: https : //github.com/lukef/IXKeychain ),并且在用户卸载应用程序时不会删除iOS钥匙串中的值,因此您可以通过应用安装来保留自己的UUID,这是根据用户帐户管理特定数量设备的关键部分。

This method will return a string for every device. 此方法将为每个设备返回一个字符串。 Since it is gonna change every time for single device so we are storing it in a keychain and can refer it whenever we need it. 由于单个设备每次都会更改,因此我们将其存储在钥匙串中,并在需要时可以引用它。

+ (NSString *) uniqueDeviceIdentifier
  {

    NSString *deviceUUID = [[SGKeyChain defaultKeyChain] stringForKey:@"uniqueId"];

        if (!deviceUUID) {

            if (!deviceUUID.length) {

        NSString *deviceUUID = @"";

        CFUUIDRef uuidRef = CFUUIDCreate(NULL);

        CFStringRef uuidStringRef = CFUUIDCreateString(NULL, uuidRef);

        CFRelease(uuidRef);

         deviceUUID = [NSString stringWithFormat:@"%@",[NSString stringWithString:(__bridge_transfer NSString *)uuidStringRef]];

    [[SGKeyChain defaultKeyChain] setObject:deviceUUID forKey:@"uniqueId" accessibleAttribute:kSecAttrAccessibleAlways];

        }
    }       
        return deviceUUID;
    }

yo can refer to this repository... https://github.com/sgup77/SGKeyChainWrapper for SGKeyChain implementation 可以参考此存储库... https://github.com/sgup77/SGKeyChainWrapper用于SGKeyChain实现

Ok I will share the approach that we follow for a B2B enterprise app. 好的,我将分享我们对B2B企业应用程序遵循的方法。 .

Every User has login Id and Password. 每个用户都有登录ID和密码。

1. So user register his device with Server using deviceReg API which takes clientDeviceId as param(client generated uuid) along with username and pass. 1.因此,用户使用deviceReg API向服务器注册其设备,该设备将clientDeviceId作为参数(客户端生成的uuid)以及用户名和密码传递给param。

2. Server returns a server generated unique identifier to be used by application on that particular device. 2.服务器返回服务器生成的唯一标识符,供该特定设备上的应用程序使用。

Conclude - in this way you can restrict the user with a certain device. 结论-通过这种方式,您可以限制用户使用特定设备。

You can use below method to generate deviceSpecific client UUID 您可以使用以下方法生成设备特定的客户端UUID

- (NSString *)getUuid
{
    CFUUIDRef uuidRef = CFUUIDCreate(NULL);
    CFStringRef uuidStringRef = CFUUIDCreateString(NULL, uuidRef);
    CFRelease(uuidRef);
    NSString *uuid = [NSString stringWithString:(__bridge NSString *)uuidStringRef];
    CFRelease(uuidStringRef);
    return uuid;
}

Please note: I remove the explanation about using AuthKey, AccessToken and others which we use for security purpose as you do not use any auth server. 请注意:由于您不使用任何身份验证服务器,因此我删除了有关使用AuthKey,AccessToken和我们出于安全目的使用的其他内容的说明。

I hope it helps. 希望对您有所帮助。

Update 1. 更新1。

Since you are having an enterprise application so i am sure you would be having atleast the user e-mail ids. 由于您具有企业应用程序,因此我确定您将至少拥有用户电子邮件ID。

So the account manager should than 1st send a email with one time token to all active accounts. 因此,客户经理应该比1日发送一封带有一次性令牌的电子邮件到所有活动帐户。

This token can be requested by the application while registering the device and send to server for validation. 应用程序可以在注册设备时请求此令牌,然后将其发送到服务器以进行验证。

Also the server invalidates the token once used to avoid misuge. 此外,一旦用于避免误操作,服务器会使令牌无效。

There should be a migrAtion api which uses the server generated device id and a migration token if user migrates the device. 应该有一个迁移API,该API使用服务器生成的设备ID和迁移令牌(如果用户迁移设备)。

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

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