Tools.m 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. //
  2. // Tools.m
  3. // jiaPeiC
  4. //
  5. // Created by apple on 16/3/12.
  6. // Copyright © 2016年 JCZ. All rights reserved.
  7. //
  8. #import "Tools.h"
  9. #import "SDSoundPlayer.h"
  10. #import "sys/utsname.h"
  11. @implementation Tools
  12. #pragma mark - iphoneX验证
  13. + (BOOL)isIPhoneX {
  14. struct utsname systemInfo;
  15. uname(&systemInfo);
  16. NSString *platform = [NSString stringWithCString:systemInfo.machine encoding:NSASCIIStringEncoding];
  17. if ([platform isEqualToString:@"i386"] || [platform isEqualToString:@"x86_64"]) {
  18. // 模拟器下采用屏幕的高度来判断
  19. return [UIScreen mainScreen].bounds.size.height == 812;
  20. }
  21. // iPhone10,6是美版iPhoneX 感谢hegelsu指出:https://github.com/banchichen/TZImagePickerController/issues/635
  22. BOOL isIPhoneX = [platform isEqualToString:@"iPhone10,3"] || [platform isEqualToString:@"iPhone10,6"];
  23. return isIPhoneX;
  24. }
  25. #pragma mark - 获取已知名字文件路径
  26. // 记得是文件 不是文件夹路径
  27. + (NSString *)getPathWithFileName:(NSString *)fileName
  28. {
  29. NSString *document = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
  30. NSString *filePath = [document stringByAppendingPathComponent:fileName];
  31. return filePath;
  32. }
  33. #pragma mark - 验证是否是电话号码
  34. + (BOOL)isMobileNumber:(NSString *)mobileNum
  35. {
  36. // 电信号段:133/153/180/181/189/177
  37. // 联通号段:130/131/132/155/156/185/186/145/176
  38. // 移动号段:134/135/136/137/138/139/150/151/152/157/158/159/182/183/184/187/188/147/178
  39. // 虚拟运营商:170
  40. NSString *MOBILE = @"^1(3[0-9]|4[57]|5[0-35-9]|8[0-9]|7[06-8])\\d{8}$";
  41. NSPredicate *regextestmobile = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", MOBILE];
  42. return [regextestmobile evaluateWithObject:mobileNum];
  43. }
  44. #pragma mark - 播报声音
  45. +(void)playAudioWithString:(NSString *)string
  46. {
  47. SDSoundPlayer *player = [SDSoundPlayer SDSoundPlayerInit];
  48. [player setDefaultWithVolume:-1.0 rate:0.5 pitchMultiplier:1.2];
  49. [player play:string];
  50. }
  51. #pragma mark - 根据图片二进制流获取图片格式
  52. + (NSString *)typeForImageData:(NSData *)data {
  53. uint8_t c;
  54. [data getBytes:&c length:1];
  55. switch (c) {
  56. case 0xFF:
  57. return @"jpeg";
  58. case 0x89:
  59. return @"png";
  60. case 0x47:
  61. return @"gif";
  62. case 0x49:
  63. case 0x4D:
  64. return @"tiff";
  65. }
  66. return @"jpg";
  67. }
  68. #pragma mark - 结合keychain的存储
  69. //存
  70. + (void)save:(NSString *)service data:(id)data {
  71. //Get search dictionary
  72. NSMutableDictionary *keychainQuery = [self getKeychainQuery:service];
  73. //Delete old item before add new item
  74. SecItemDelete((__bridge CFDictionaryRef)keychainQuery);
  75. //Add new object to search dictionary(Attention:the data format)
  76. [keychainQuery setObject:[NSKeyedArchiver archivedDataWithRootObject:data] forKey:(__bridge id)kSecValueData];
  77. //Add item to keychain with the search dictionary
  78. SecItemAdd((__bridge CFDictionaryRef)keychainQuery, NULL);
  79. }
  80. + (NSMutableDictionary *)getKeychainQuery:(NSString *)service {
  81. return [NSMutableDictionary dictionaryWithObjectsAndKeys:
  82. (__bridge id)kSecClassGenericPassword,(__bridge id)kSecClass,
  83. service, (__bridge id)kSecAttrService,
  84. service, (__bridge id)kSecAttrAccount,
  85. (__bridge id)kSecAttrAccessibleAfterFirstUnlock,(__bridge id)kSecAttrAccessible,
  86. nil];
  87. }
  88. //取
  89. + (id)load:(NSString *)service {
  90. id ret = nil;
  91. NSMutableDictionary *keychainQuery = [self getKeychainQuery:service];
  92. //Configure the search setting
  93. //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
  94. [keychainQuery setObject:(__bridge id)kCFBooleanTrue forKey:(__bridge id)kSecReturnData];
  95. [keychainQuery setObject:(__bridge id)kSecMatchLimitOne forKey:(__bridge id)kSecMatchLimit];
  96. CFDataRef keyData = NULL;
  97. if (SecItemCopyMatching((__bridge CFDictionaryRef)keychainQuery, (CFTypeRef *)&keyData) == noErr) {
  98. @try {
  99. ret = [NSKeyedUnarchiver unarchiveObjectWithData:(__bridge NSData *)keyData];
  100. } @catch (NSException *e) {
  101. NSLog(@"Unarchive of %@ failed: %@", service, e);
  102. } @finally {
  103. }
  104. }
  105. if (keyData)
  106. CFRelease(keyData);
  107. return ret;
  108. }
  109. //删除
  110. + (void)delete:(NSString *)service {
  111. NSMutableDictionary *keychainQuery = [self getKeychainQuery:service];
  112. SecItemDelete((__bridge CFDictionaryRef)keychainQuery);
  113. }
  114. #pragma mark - 权限验证
  115. +(void)permissionValidationWithID:(NSString *)str
  116. view:(UIView *)view
  117. result:(GetResult)result
  118. {
  119. NSMutableDictionary * mdic = [NSMutableDictionary new];
  120. [mdic setValue:defUser.userDict[@"id"] forKey:@"userId"];
  121. [mdic setValue:str forKey:@"id"];
  122. [MBProgressHUD showLoadToView:view];
  123. [NetManager requestAnythingWithURL:@"showMenu" dictionary:mdic dataArray:nil completion:^(NSDictionary *root) {
  124. [MBProgressHUD hideHUDForView:view animated:NO];
  125. if (!root) {
  126. result(NO,@"请求失败");
  127. return;
  128. }
  129. if ([root[@"code"] integerValue] == 1) {
  130. result(NO,root[@"msg"]);
  131. return;
  132. }
  133. result(YES,nil);
  134. }];
  135. }
  136. @end