iOSが取得した一意の識別子

3913 ワード

参考文献
iOSデバイス固有ID(UUID)を取得し、キー列に保存
Keychainの簡単な理解と使用
1. UIDevice
UUIDが生成されますが、アプリの削除に伴って変更されます.

NSString *identifierForVendor = [[UIDevice currentDevice].identifierForVendor UUIDString];


2.KeyChainの使用
キーホルダーは独立しており、アップグレードも削除も削除されません.携帯電話に存在するキーホルダーなので(iPhoneではここを操作する人は少ないのではないでしょうか?)暗号化されています.
KyeChainスイッチをオンにします:project -> targets -> Capabilities -> Keychain Sharing次の2つのクラスをコピー
  • GetUUID
  • KeyChainStore

  • KeyChainStore
    
    #import 
    
    @interface YKKeyChainStore : NSObject
    + (void)save:(NSString *)service data:(id)data;
    + (id)load:(NSString *)service;
    + (void)deleteKeyData:(NSString *)service;
    @end
    
    
    
    #import "YKKeyChainStore.h"
    
    @implementation YKKeyChainStore
    + (NSMutableDictionary *)getKeychainQuery:(NSString *)service {
        return [NSMutableDictionary dictionaryWithObjectsAndKeys:
                (id)kSecClassGenericPassword,(id)kSecClass,
                service, (id)kSecAttrService,
                service, (id)kSecAttrAccount,
                (id)kSecAttrAccessibleAfterFirstUnlock,(id)kSecAttrAccessible,
                nil];
    }
    
    + (void)save:(NSString *)service data:(id)data {
        //Get search dictionary
        NSMutableDictionary *keychainQuery = [self getKeychainQuery:service];
        //Delete old item before add new item
        SecItemDelete((CFDictionaryRef)keychainQuery);
        //Add new object to search dictionary(Attention:the data format)
        [keychainQuery setObject:[NSKeyedArchiver archivedDataWithRootObject:data] forKey:(id)kSecValueData];
        //Add item to keychain with the search dictionary
        SecItemAdd((CFDictionaryRef)keychainQuery, NULL);
    }
    
    + (id)load:(NSString *)service {
        id ret = nil;
        NSMutableDictionary *keychainQuery = [self getKeychainQuery:service];
        //Configure the search setting
        //Since in our simple case we are expecting only a single attribute to be returned (the password) we can set the attribute kSecReturnData to kCFBooleanTrue
        [keychainQuery setObject:(id)kCFBooleanTrue forKey:(id)kSecReturnData];
        [keychainQuery setObject:(id)kSecMatchLimitOne forKey:(id)kSecMatchLimit];
        CFDataRef keyData = NULL;
        if (SecItemCopyMatching((CFDictionaryRef)keychainQuery, (CFTypeRef *)&keyData) == noErr) {
            @try {
                ret = [NSKeyedUnarchiver unarchiveObjectWithData:(__bridge NSData *)keyData];
            } @catch (NSException *e) {
                NSLog(@"Unarchive of %@ failed: %@", service, e);
            } @finally {
            }
        }
        if (keyData)
            CFRelease(keyData);
        return ret;
    }
    
    + (void)deleteKeyData:(NSString *)service {
        NSMutableDictionary *keychainQuery = [self getKeychainQuery:service];
        SecItemDelete((CFDictionaryRef)keychainQuery);
    }
    
    

    GetUUID
    
    #import 
    
    @interface YKGetUUID : NSObject
    +(NSString *)getUUID;
    @end
    
    
    
    #import "GetUUID.h"
    #import "KeyChainStore.h"
    #define  KEY_USERNAME_PASSWORD @"com.company.app.usernamepassword" //   
    #define  KEY_USERNAME @"com.company.app.username" //   
    #define  KEY_PASSWORD @"com.company.app.password" //   
    
    @implementation YKGetUUID
    +(NSString *)getUUID
    {
        NSString * strUUID = (NSString *)[KeyChainStore load:@"com.company.app.usernamepassword"];
        
        //        ,uuid  
        if ([strUUID isEqualToString:@""] || !strUUID)
        {
            //    uuid   
            CFUUIDRef uuidRef = CFUUIDCreate(kCFAllocatorDefault);
            
            strUUID = (NSString *)CFBridgingRelease(CFUUIDCreateString (kCFAllocatorDefault,uuidRef));
            
            //  uuid   keychain
            [KeyChainStore save:KEY_USERNAME_PASSWORD data:strUUID];
            
        }
        return strUUID;
    }
    
    @end
    
    ```
    
    ###   
    ```
    
     NSLog(@"----%@",[YKGetUUID getUUID]);
    
    ```