小说绘上架版本
This commit is contained in:
@@ -0,0 +1,14 @@
|
||||
//
|
||||
// KeyChainStore.h
|
||||
//
|
||||
// Created by Andrew on 2017/8/5.
|
||||
// Copyright © 2017年 Andrew. All rights reserved.
|
||||
//
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
|
||||
@interface KeyChainStore : NSObject
|
||||
+ (void)save:(NSString *)service data:(id)data;
|
||||
+ (id)load:(NSString *)service;
|
||||
+ (void)deleteKeyData:(NSString *)service;
|
||||
@end
|
||||
@@ -0,0 +1,57 @@
|
||||
//
|
||||
// KeyChainStore.m
|
||||
//
|
||||
// Created by Andrew on 2017/8/5.
|
||||
// Copyright © 2017年 Andrew. All rights reserved.
|
||||
//
|
||||
|
||||
#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 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) {
|
||||
// TILog(@"Unarchive of %@ failed: %@", service, e);
|
||||
} @finally {
|
||||
}
|
||||
}
|
||||
if (keyData)
|
||||
CFRelease(keyData);
|
||||
return ret;
|
||||
}
|
||||
|
||||
+ (void)deleteKeyData:(NSString *)service {
|
||||
NSMutableDictionary *keychainQuery = [self getKeychainQuery:service];
|
||||
SecItemDelete((CFDictionaryRef)keychainQuery);
|
||||
}
|
||||
|
||||
@end
|
||||
@@ -0,0 +1,14 @@
|
||||
//
|
||||
// UUID.h
|
||||
//
|
||||
// Created by Andrew on 2017/8/5.
|
||||
// Copyright © 2017年 Andrew. All rights reserved.
|
||||
//
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
|
||||
@interface UUID : NSObject
|
||||
|
||||
+ (NSString *)getUUID;
|
||||
|
||||
@end
|
||||
@@ -0,0 +1,58 @@
|
||||
//
|
||||
// UUID.m
|
||||
//
|
||||
// Created by Andrew on 2017/8/5.
|
||||
// Copyright © 2017年 Andrew. All rights reserved.
|
||||
//
|
||||
|
||||
#import "UUID.h"
|
||||
#import "KeyChainStore.h"
|
||||
|
||||
#define UUID_KEY(bunid) [NSString stringWithFormat:@"cn.property.uuidkey%@",bunid]
|
||||
#define Pasteboard(bunid) [NSString stringWithFormat:@"cn.property.PublicPasteBord%@",bunid]
|
||||
|
||||
@implementation UUID
|
||||
+ (NSString *)getUUID
|
||||
{
|
||||
NSString *bunid = [[NSBundle mainBundle]bundleIdentifier];
|
||||
NSString *strUUID;
|
||||
NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
|
||||
strUUID = [userDefault objectForKey:UUID_KEY(bunid)];
|
||||
if (!strUUID) {
|
||||
UIPasteboard *pp = [UIPasteboard generalPasteboard];
|
||||
NSData *data = [pp valueForPasteboardType:Pasteboard(bunid)];
|
||||
strUUID = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
|
||||
if (!strUUID || [strUUID isEqualToString:@""]) {
|
||||
strUUID = (NSString *)[KeyChainStore load:UUID_KEY(bunid)];
|
||||
if (strUUID) {
|
||||
[userDefault setObject:strUUID forKey:UUID_KEY(bunid)];
|
||||
[userDefault synchronize];
|
||||
}
|
||||
} else {
|
||||
[userDefault setObject:strUUID forKey:UUID_KEY(bunid)];
|
||||
[userDefault synchronize];
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
//首次执行该方法时,uuid为空
|
||||
if ([strUUID isEqualToString:@""] || !strUUID) {
|
||||
|
||||
//生成一个uuid的方法
|
||||
CFUUIDRef uuidRef = CFUUIDCreate(kCFAllocatorDefault);
|
||||
strUUID = (NSString *)CFBridgingRelease(CFUUIDCreateString(kCFAllocatorDefault,uuidRef));
|
||||
|
||||
//将该uuid保存到keychain
|
||||
[KeyChainStore save:UUID_KEY(bunid) data:strUUID];
|
||||
[userDefault setObject:strUUID forKey:UUID_KEY(bunid)];
|
||||
[userDefault synchronize];
|
||||
UIPasteboard *p = [UIPasteboard generalPasteboard];
|
||||
[p setValue:strUUID forPasteboardType:Pasteboard(bunid)];
|
||||
|
||||
CFRelease(uuidRef);
|
||||
|
||||
}
|
||||
return strUUID;
|
||||
}
|
||||
|
||||
@end
|
||||
Reference in New Issue
Block a user