// // Tools.m // jiaPeiC // // Created by apple on 16/3/12. // Copyright © 2016年 JCZ. All rights reserved. // #import "Tools.h" #import "SDSoundPlayer.h" //广告标识 #import @implementation Tools + (NSString *)getPathWithFileName:(NSString *)fileName { NSString *document = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject]; NSString *filePath = [document stringByAppendingPathComponent:fileName]; return filePath; } + (void)playAudioWithString:(NSString *)string { SDSoundPlayer *player = [SDSoundPlayer SDSoundPlayerInit]; [player setDefaultWithVolume:-1.0 rate:0.5 pitchMultiplier:1.2]; [player play:string]; } //验证是否是电话号码 + (BOOL)isMobileNumber:(NSString *)mobileNum { // 电信号段:133/153/180/181/189/177 // 联通号段:130/131/132/155/156/185/186/145/176 // 移动号段:134/135/136/137/138/139/150/151/152/157/158/159/182/183/184/187/188/147/178 // 虚拟运营商:170 NSString *MOBILE = @"^1(3[0-9]|4[57]|5[0-35-9]|8[0-9]|7[06-8])\\d{8}$"; NSPredicate *regextestmobile = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", MOBILE]; return [regextestmobile evaluateWithObject:mobileNum]; } //根据图片二进制流获取图片格式 + (NSString *)typeForImageData:(NSData *)data { uint8_t c; [data getBytes:&c length:1]; switch (c) { case 0xFF: return @"jpeg"; case 0x89: return @"png"; case 0x47: return @"gif"; case 0x49: case 0x4D: return @"tiff"; } return @"jpg"; } + (NSString *)getIDFV { // if (!isOfficial) { // return @"0282DC86-95FD-4000-AC65-0F69CF9DBE7E"; // } NSString * const KEY_USERNAME_PASSWORD = @"com.danson.jiaPeiCo.usernamepassword"; NSString * const KEY_PASSWORD = @"com.danson.jiaPeiCo.password"; //测试用 清除keychain中的内容 //[Tools delete:KEY_USERNAME_PASSWORD]; NSMutableDictionary *readUserPwd = (NSMutableDictionary *)[Tools load:KEY_USERNAME_PASSWORD]; //NSLog(@"keychain------><>%@",readUserPwd); if (!readUserPwd) { //如果为空 说明是第一次安装 做存储操作 //IDFV // NSString *identifierStr = [[[UIDevice currentDevice] identifierForVendor] UUIDString]; // NSMutableDictionary *usernamepasswordKVPairs = [NSMutableDictionary dictionaryWithObject:identifierStr forKey:KEY_PASSWORD]; //IDFA NSString *adId = [[[ASIdentifierManager sharedManager] advertisingIdentifier] UUIDString]; if ([adId isEqualToString:@"00000000-0000-0000-0000-000000000000"] || [adId isEqualToString:@"00000000000000000000000000000000"]) { ShowMsg(@"获取广告标识失败,请在“设置-隐私-广告”中,关闭“限制广告跟踪”"); }else{ NSMutableDictionary *usernamepasswordKVPairs = [NSMutableDictionary dictionaryWithObject:adId forKey:KEY_PASSWORD]; [Tools save:KEY_USERNAME_PASSWORD data:usernamepasswordKVPairs]; } return adId; }else{ return [readUserPwd objectForKey:KEY_PASSWORD]; } } //存 + (void)save:(NSString *)service data:(id)data { //Get search dictionary NSMutableDictionary *keychainQuery = [self getKeychainQuery:service]; //Delete old item before add new item SecItemDelete((__bridge CFDictionaryRef)keychainQuery); //Add new object to search dictionary(Attention:the data format) [keychainQuery setObject:[NSKeyedArchiver archivedDataWithRootObject:data] forKey:(__bridge id)kSecValueData]; //Add item to keychain with the search dictionary SecItemAdd((__bridge CFDictionaryRef)keychainQuery, NULL); } + (NSMutableDictionary *)getKeychainQuery:(NSString *)service { return [NSMutableDictionary dictionaryWithObjectsAndKeys: (__bridge id)kSecClassGenericPassword,(__bridge id)kSecClass, service, (__bridge id)kSecAttrService, service, (__bridge id)kSecAttrAccount, (__bridge id)kSecAttrAccessibleAfterFirstUnlock,(__bridge id)kSecAttrAccessible, nil]; } //取 + (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:(__bridge id)kCFBooleanTrue forKey:(__bridge id)kSecReturnData]; [keychainQuery setObject:(__bridge id)kSecMatchLimitOne forKey:(__bridge id)kSecMatchLimit]; CFDataRef keyData = NULL; if (SecItemCopyMatching((__bridge 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((__bridge CFDictionaryRef)keychainQuery); } + (NSString *)dateWithTimestamp:(NSString *)timestamp { // iOS 生成的时间戳是10位 NSTimeInterval interval = [timestamp doubleValue] / 1000.0; NSDate *date = [NSDate dateWithTimeIntervalSince1970:interval]; NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"]; return [formatter stringFromDate: date]; } @end