Benefits:

  • A class that encapsulates the unique identity of an operating device is available directly on GitHub: github.com/Hext123/HDe…

Ideas:

  • Save toKeychainThe data in the keystring will not be deleted when the application is uninstalled, unless factory Settings are restored. Therefore, a string of characters can be generated and saved in the key string as a unique device identifier.

Implementation:

  1. Generates a string of unique characters as a unique device identifier. The only thing that is unique to a large extent is the UUID. IOS provides methods to obtain UUID:

    • [[NSUUID UUID] UUIDString]: The [NSUUID UUID] method generates a different UUID each time it is called.

    • [[[UIDevice currentDevice] identifierForVendor] UUIDString]: identifierForVendor can obtain the unique identity of this app on this phone, but it will change after uninstalling and reinstalling the app.

  2. Save the UUID to the Keychain: The code for directly operating the Keychain is tedious and error-prone. Therefore, use an encapsulated operation class SFHFKeychainUtils to operate the Keychain.

Code:


//
// HDeviceIdentifier.m
// HDeviceIdentifier
//
// Created by hext on 16/4/5.
// Copyright © 2016 Hext. All rights reserved.
//

#import "HDeviceIdentifier.h"
#import "SFHFKeychainUtils.h"

// Obtain the package name, such as com.hext.uuidDemo
#define bundleIdentifier [[NSBundle mainBundle]bundleIdentifier]

@implementation HDeviceIdentifier

/** * Synchronize unique device id (generate and save the unique device ID, if it already exists, no processing) ** @return whether successfully */+ (BOOL)syncDeviceIdentifier{
    
    /** * Obtain the application UUID identifier * (* identifierForVendor Returns the application UUID, which will change after uninstallation. [[NSUUID UUID]UUIDString] The [NSUUID UUID] method generates a different UUID each time it is called * but identifierForVendor can be used to verify if it is the first installation *) */
    NSString *myUUIDStr = [[[UIDevice currentDevice] identifierForVendor] UUIDString];
    
    StoreUsername: key * Password: value * ServiceName: group name * updateExisting: Updates an existing */
    BOOL f = [SFHFKeychainUtils storeUsername:@"deviceIdentifier" andPassword:myUUIDStr forServiceName:bundleIdentifier updateExisting:NO error:nil];
    
    return f;
}

/** * returns a unique device ID ** @return Device ID */+ (NSString*)deviceIdentifier{
    // The device identifier does not already exist
    [self syncDeviceIdentifier];
    
    // Get the unique device id from the keystring
    NSString * deviceIdentifier = [SFHFKeychainUtils getPasswordForUsername:@"deviceIdentifier" andServiceName:bundleIdentifier error:nil];
    
    return deviceIdentifier;
}

/** * This application is installed for the first time ** @return is installed for the first time */+ (BOOL)isFirstInstall{
    
    NSString * deviceIdentifier = [HDeviceIdentifier deviceIdentifier];
    NSString * identifierForVendor = [[[UIDevice currentDevice] identifierForVendor] UUIDString];
    
    /** * if the deviceIdentifier(deviceIdentifier) stored in the keystring does not exist or is equal to the deviceIdentifier(UUID of the application), this is the first installation */
    if ( !deviceIdentifier || [deviceIdentifier isEqualToString:identifierForVendor]) {
        return YES;
    }else{
        return NO; }}@end


Copy the code