• iOS开发之获取设备唯一标识UUID


    首先我们知道获取设备标识的三种方法

    /**  卸载应用重新安装后会不一致*/
    + (NSString *)getUUID{
        CFUUIDRef uuid = CFUUIDCreate(NULL);
        NSString *UUID = (__bridge_transfer NSString *)CFUUIDCreateString(NULL, uuid);
        CFRelease(uuid);
        return UUID;
    }
     
    /**  卸载应用重新安装后会不一致*/
    + (NSString *)getUUID{
        return [UIDevice currentDevice].identifierForVendor.UUIDString;;
    }
     
    /** 不会因为应用卸载改变 
      * 但是用户在设置-隐私-广告里面限制广告跟踪后会变成@"00000000-0000-0000-0000-000000000000"
      * 重新打开后会变成另一个,还原广告标识符也会变
      */
    + (NSString *)getUUID{
        return [[[ASIdentifierManager sharedManager] advertisingIdentifier] UUIDString];
    }

    我们看这三个方法是不是都不稳妥,可能都会改变,那我们得想个办法把值存好了,存到钥匙串

    首先打开钥匙串权限

     然后是核心存储代码

    #import <Foundation/Foundation.h>
     
    @interface KeyChainStore : NSObject
    + (void)save:(NSString*)service data:(id)data;
    + (id)load:(NSString*)service;
    + (void)deleteKeyData:(NSString*)service;
    @end
    #import "KeyChainStore.h"
     
    @implementation KeyChainStore
     
    + (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 searchdictionary(Attention:the data format)
        [keychainQuery setObject:[NSKeyedArchiver archivedDataWithRootObject:data]forKey:(id)kSecValueData];
        //Add item to keychain with the searchdictionary
        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 areexpecting only a single attribute to be returned (the password) wecan 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(@"Unarchiveof %@ failed: %@",service, e);
            }@finally{
            }
        }
        if(keyData)
            CFRelease(keyData);
        return ret;
    }
     
    + (void)deleteKeyData:(NSString*)service {
        NSMutableDictionary*keychainQuery = [self getKeychainQuery:service];
        SecItemDelete((CFDictionaryRef)keychainQuery);
    }
     
    @end

    调用代码

    /**  获取UUID*/
    + (NSString *)getUUIDByKeyChain{
        // 这个key的前缀最好是你的BundleID
        NSString*strUUID = (NSString*)[KeyChainStore load:@"com.mycompany.myapp.usernamepassword"];
        //首次执行该方法时,uuid为空
        if([strUUID isEqualToString:@""]|| !strUUID)
        {
            // 获取UUID 这个是要引入<AdSupport/AdSupport.h>的
            strUUID = [[[ASIdentifierManager sharedManager] advertisingIdentifier] UUIDString];
            
            if(strUUID.length ==0 || [strUUID isEqualToString:@"00000000-0000-0000-0000-000000000000"])
            {
                //生成一个uuid的方法
                CFUUIDRef uuidRef= CFUUIDCreate(kCFAllocatorDefault);
                strUUID = (NSString*)CFBridgingRelease(CFUUIDCreateString(kCFAllocatorDefault,uuidRef));
                CFRelease(uuidRef);
            }
            
            //将该uuid保存到keychain
            [KeyChainStore save:@"com.mycompany.myapp.usernamepassword" data:strUUID];
        }
        return strUUID;
    }

    借鉴链接 https://blog.csdn.net/lg767201403/article/details/80568798

  • 相关阅读:
    为什么cmd拖拽文件进去时有时候带引号,有时候不带?
    Android开发学习笔记:Spinner和AutoCompleteTextView浅析
    使用Genymotion调试出现错误INSTALL_FAILED_CPU_ABI_INCOMPATIBLE解决办法
    Android Fragment完全解析,关于碎片你所需知道的一切
    国外程序员推荐:每个程序员都应该读的非编程书
    百度地图添加覆盖物与给定两点路线规划
    Android 百度地图 SDK v3.0.0 (三) 添加覆盖物Marker与InfoWindow的使用
    Unable to execute dex: Multiple dex files define 解决方法
    Android 百度地图 SDK v3.0.0 (二) 定位与结合方向传感器
    poppupwindow android
  • 原文地址:https://www.cnblogs.com/hecanlin/p/11555184.html
Copyright © 2020-2023  润新知