2022-11-23 19:17:29 +08:00

88 lines
1.9 KiB
Objective-C

//
// DataManager.m
// Unity-iPhone
//
// Created by zhl on 2022/11/23.
//
#import "DataManager.h"
#import "UICKeyChainStore.h"
@interface DataManager () <NSCopying>
@end
static NSString * const cebgWalletService = @"com.cebg.wallet";
//global var
static id _instance;
UICKeyChainStore *keychain;
InitType _instanceInitType;
@implementation DataManager
+ (instancetype)sharedInstanceWith:(InitType)type {
_instanceInitType = type;
if (type == SynLock) {
@synchronized (self) {
if (_instance == nil) {
_instance = [[self alloc] init];
}
}
}
else {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_instance = [[self alloc] init];
});
}
return _instance;
}
+ (instancetype)allocWithZone:(struct _NSZone *)zone {
if (_instanceInitType == SynLock) {
@synchronized (self) {
if (_instance == nil) {
_instance = [super allocWithZone:zone];
}
}
}
else {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_instance = [super allocWithZone:zone];
});
}
return _instance;
}
- (id)copyWithZone:(nullable NSZone *)zone {
return _instance;
}
-(void)saveKey:(NSString *) account key:(NSString *) key {
if (keychain == nil ){
keychain = [UICKeyChainStore keyChainStoreWithService:cebgWalletService];
keychain.synchronizable = YES;
}
keychain[account] = key;
}
-(NSString *)loadKey:(NSString *) account {
if (keychain == nil ){
keychain = [UICKeyChainStore keyChainStoreWithService:cebgWalletService];
keychain.synchronizable = YES;
}
NSError *error;
NSString * result = [keychain stringForKey:account error:&error];
if (error) {
NSLog(@"%@", error.localizedDescription);
}
return result;
}
@end