1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495 |
- //
- // JXUUIDModel.m
- // shiku_im
- //
- // Created by os on 2020/6/24.
- // Copyright © 2020 Reese. All rights reserved.
- //
- #import "JXUUIDModel.h"
- #import <Security/Security.h>
- #import "KeychainUUID.h"
- NSString * const KEY_UDID_INSTEAD = @"app.jixincc.vip";
- @implementation JXUUIDModel
- -(NSString *)keyUDid{
-
- KeychainUUID *keychain = [[KeychainUUID alloc] init];
- id data = [keychain readUDID];
- return data;
- }
- +(NSString *)getDeviceIDInKeychain
- {
- NSString *getUDIDInKeychain = (NSString *)[JXUUIDModel load:KEY_UDID_INSTEAD];
- // NSLog(@"从keychain中获取到的 UDID_INSTEAD %@",getUDIDInKeychain);
-
- if (!getUDIDInKeychain ||[getUDIDInKeychain isEqualToString:@""]||[getUDIDInKeychain isKindOfClass:[NSNull class]]) {
- CFUUIDRef puuid = CFUUIDCreate( nil );
- CFStringRef uuidString = CFUUIDCreateString( nil, puuid );
- NSString * result = (NSString *)CFBridgingRelease(CFStringCreateCopy( NULL, uuidString));
- CFRelease(puuid);
- CFRelease(uuidString);
- NSLog(@"\n \n \n _____重新存储 UUID _____\n \n \n %@",result);
- [JXUUIDModel save:KEY_UDID_INSTEAD data:result];
- getUDIDInKeychain = (NSString *)[JXUUIDModel load:KEY_UDID_INSTEAD];
- }
- // NSLog(@"最终 ———— UDID_INSTEAD %@",getUDIDInKeychain);
- return getUDIDInKeychain;
- }
- #pragma mark - private
- + (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)delete:(NSString *)service {
- NSMutableDictionary *keychainQuery = [self getKeychainQuery:service];
- SecItemDelete((CFDictionaryRef)keychainQuery);
- }
- @end
|