zoukankan      html  css  js  c++  java
  • oc唯一标时一部设备

    MyUUIDManager.h

    #import <Foundation/Foundation.h>
    
    NS_ASSUME_NONNULL_BEGIN
    
    @interface MyUUIDManager : NSObject
    +(void)saveUUID:(NSString *)uuid;
    
    +(NSString *)getUUID;
    
    +(void)deleteUUID;
    @end
    
    NS_ASSUME_NONNULL_END
    

     MyUUIDManager.m

    #import "MyUUIDManager.h"
    #import "MyKeyChainManager.h"
    @implementation MyUUIDManager
    static NSString * const KEY_IN_KEYCHAIN = @"com.myuuid.uuid";
    
    
    +(void)saveUUID:(NSString *)uuid{
        if (uuid && uuid.length > 0) {
            [MyKeyChainManager save:KEY_IN_KEYCHAIN data:uuid];
        }
    }
    
    
    +(NSString *)getUUID{
        //先获取keychain里面的UUID字段,看是否存在
        NSString *uuid = (NSString *)[MyKeyChainManager load:KEY_IN_KEYCHAIN];
        
        //如果不存在则为首次获取UUID,所以获取保存。
        if (!uuid || uuid.length == 0) {
            CFUUIDRef puuid = CFUUIDCreate( nil );
            
            CFStringRef uuidString = CFUUIDCreateString( nil, puuid );
            
            uuid = [NSString stringWithFormat:@"%@", uuidString];
            
            [self saveUUID:uuid];
            
            CFRelease(puuid);
            
            CFRelease(uuidString);
        }
        
        return uuid;
    }
    
    
    
    +(void)deleteUUID{
        [MyKeyChainManager delete:KEY_IN_KEYCHAIN];
    }
    
    @end
    

    MyKeyChainManager.h

    #import <Foundation/Foundation.h>
    
    NS_ASSUME_NONNULL_BEGIN
    
    @interface MyKeyChainManager : NSObject
    
    + (void)save:(NSString *)service data:(id)data;
    + (id)load:(NSString *)service ;
    + (void)delete:(NSString *)service ;
    @end
    
    NS_ASSUME_NONNULL_END

    MyKeyChainManager.m

    #import "MyKeyChainManager.h"
    
    @implementation MyKeyChainManager
    
    + (NSMutableDictionary *)getKeychainQuery:(NSString *)service {
        return [NSMutableDictionary dictionaryWithObjectsAndKeys:
                (__bridge_transfer id)kSecClassGenericPassword,(__bridge_transfer id)kSecClass,
                service, (__bridge_transfer id)kSecAttrService,
                service, (__bridge_transfer id)kSecAttrAccount,
                (__bridge_transfer id)kSecAttrAccessibleAfterFirstUnlock,(__bridge_transfer 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((__bridge_retained CFDictionaryRef)keychainQuery);
        //Add new object to search dictionary(Attention:the data format)
        [keychainQuery setObject:[NSKeyedArchiver archivedDataWithRootObject:data] forKey:(__bridge_transfer id)kSecValueData];
        //Add item to keychain with the search dictionary
        SecItemAdd((__bridge_retained CFDictionaryRef)keychainQuery, NULL);
    }
    
    + (id)load:(NSString *)service {
        id ret = nil;
        NSMutableDictionary *keychainQuery = [self getKeychainQuery:service];
        //Configure the search setting
        [keychainQuery setObject:(id)kCFBooleanTrue forKey:(__bridge_transfer id)kSecReturnData];
        [keychainQuery setObject:(__bridge_transfer id)kSecMatchLimitOne forKey:(__bridge_transfer id)kSecMatchLimit];
        CFDataRef keyData = NULL;
        if (SecItemCopyMatching((__bridge_retained CFDictionaryRef)keychainQuery, (CFTypeRef *)&keyData) == noErr) {
            @try {
                ret = [NSKeyedUnarchiver unarchiveObjectWithData:(__bridge_transfer NSData *)keyData];
            } @catch (NSException *e) {
               
            } @finally {
            }
        }
        return ret;
    }
    
    + (void)delete:(NSString *)service {
        NSMutableDictionary *keychainQuery = [self getKeychainQuery:service];
        SecItemDelete((__bridge_retained CFDictionaryRef)keychainQuery);
    }
    
    @end

    使用:NSLog(@"%@",[MyUUIDManager getUUID]);

    //如果uuid为空

    if(!uuid){
            // 获取UUID 这个是要引入<AdSupport/AdSupport.h>的
            if ( [[ASIdentifierManager sharedManager] isAdvertisingTrackingEnabled] ) {
                uuid = [[[ASIdentifierManager sharedManager] advertisingIdentifier] UUIDString];
                return uuid;
            }else{
                return @"";
            }
        }
    
  • 相关阅读:
    C# winIO32位,64位的使用(运行时要用管理员身份)
    C#实现的三种方式实现模拟键盘按键
    C#打印日志的小技巧
    write wall ping ifconfig mail last traceroute netstat setup mount
    安装常用工具 zip unzip bzip2 gcc gcc++编译器 cmake编译器
    gzip/gunzip tar -zcf/-zxvf zip /unzip bzip2/bunzip2 tar -cjf/tar -xjf
    help
    Asp.Net 高性能框架 SqlSugar.ORM 2.3
    centos 查看版本(转)
    浅谈OCR之Tesseract
  • 原文地址:https://www.cnblogs.com/hualuoshuijia/p/5377279.html
Copyright © 2011-2022 走看看