// // Tools.m // jiaPeiC // // Created by apple on 16/3/12. // Copyright © 2016年 JCZ. All rights reserved. // #import "Tools.h" #import "SDSoundPlayer.h" #import "sys/utsname.h" @implementation Tools #pragma mark - iphoneX验证 + (BOOL)isIPhoneX { struct utsname systemInfo; uname(&systemInfo); NSString *platform = [NSString stringWithCString:systemInfo.machine encoding:NSASCIIStringEncoding]; if ([platform isEqualToString:@"i386"] || [platform isEqualToString:@"x86_64"]) { // 模拟器下采用屏幕的高度来判断 return [UIScreen mainScreen].bounds.size.height == 812; } // iPhone10,6是美版iPhoneX 感谢hegelsu指出:https://github.com/banchichen/TZImagePickerController/issues/635 BOOL isIPhoneX = [platform isEqualToString:@"iPhone10,3"] || [platform isEqualToString:@"iPhone10,6"]; return isIPhoneX; } #pragma mark - 获取已知名字文件路径 // 记得是文件 不是文件夹路径 + (NSString *)getPathWithFileName:(NSString *)fileName { NSString *document = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject]; NSString *filePath = [document stringByAppendingPathComponent:fileName]; return filePath; } #pragma mark - 验证是否是电话号码 + (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]; } #pragma mark - 播报声音 +(void)playAudioWithString:(NSString *)string { SDSoundPlayer *player = [SDSoundPlayer SDSoundPlayerInit]; [player setDefaultWithVolume:-1.0 rate:0.5 pitchMultiplier:1.2]; [player play:string]; } #pragma mark - 根据图片二进制流获取图片格式 + (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"; } #pragma mark - 结合keychain的存储 //存 + (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); } #pragma mark - 权限验证 +(void)permissionValidationWithID:(NSString *)str view:(UIView *)view result:(GetResult)result { NSMutableDictionary * mdic = [NSMutableDictionary new]; [mdic setValue:defUser.userDict[@"id"] forKey:@"userId"]; [mdic setValue:str forKey:@"id"]; [MBProgressHUD showLoadToView:view]; [NetManager requestAnythingWithURL:@"showMenu" dictionary:mdic dataArray:nil completion:^(NSDictionary *root) { [MBProgressHUD hideHUDForView:view animated:NO]; if (!root) { result(NO,@"请求失败"); return; } if ([root[@"code"] integerValue] == 1) { result(NO,root[@"msg"]); return; } result(YES,nil); }]; } @end