Tools.m 5.6 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. //广告标识
  11. #import <AdSupport/ASIdentifierManager.h>
  12. @implementation Tools
  13. + (NSString *)getPathWithFileName:(NSString *)fileName
  14. {
  15. NSString *document = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
  16. NSString *filePath = [document stringByAppendingPathComponent:fileName];
  17. return filePath;
  18. }
  19. + (void)playAudioWithString:(NSString *)string
  20. {
  21. dispatch_async(dispatch_get_main_queue(), ^{
  22. SDSoundPlayer *player = [SDSoundPlayer SDSoundPlayerInit];
  23. [player setDefaultWithVolume:-1.0 rate:0.5 pitchMultiplier:1.2];
  24. [player play:string];
  25. });
  26. }
  27. //验证是否是电话号码
  28. + (BOOL)isMobileNumber:(NSString *)mobileNum
  29. {
  30. // 电信号段:133/153/180/181/189/177
  31. // 联通号段:130/131/132/155/156/185/186/145/176
  32. // 移动号段:134/135/136/137/138/139/150/151/152/157/158/159/182/183/184/187/188/147/178
  33. // 虚拟运营商:170
  34. NSString *MOBILE = @"^1(3[0-9]|4[57]|5[0-35-9]|8[0-9]|7[06-8])\\d{8}$";
  35. NSPredicate *regextestmobile = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", MOBILE];
  36. return [regextestmobile evaluateWithObject:mobileNum];
  37. }
  38. //根据图片二进制流获取图片格式
  39. + (NSString *)typeForImageData:(NSData *)data {
  40. uint8_t c;
  41. [data getBytes:&c length:1];
  42. switch (c) {
  43. case 0xFF:
  44. return @"jpeg";
  45. case 0x89:
  46. return @"png";
  47. case 0x47:
  48. return @"gif";
  49. case 0x49:
  50. case 0x4D:
  51. return @"tiff";
  52. }
  53. return @"jpg";
  54. }
  55. + (NSString *)getIDFV
  56. {
  57. // if (!isOfficial) {
  58. // return @"0282DC86-95FD-4000-AC65-0F69CF9DBE7E";
  59. // }
  60. NSString * const KEY_USERNAME_PASSWORD = @"com.danson.jiaPeiCo.usernamepassword";
  61. NSString * const KEY_PASSWORD = @"com.danson.jiaPeiCo.password";
  62. //测试用 清除keychain中的内容
  63. //[Tools delete:KEY_USERNAME_PASSWORD];
  64. NSMutableDictionary *readUserPwd = (NSMutableDictionary *)[Tools load:KEY_USERNAME_PASSWORD];
  65. //NSLog(@"keychain------><>%@",readUserPwd);
  66. if (!readUserPwd) {
  67. //如果为空 说明是第一次安装 做存储操作
  68. //IDFV
  69. // NSString *identifierStr = [[[UIDevice currentDevice] identifierForVendor] UUIDString];
  70. // NSMutableDictionary *usernamepasswordKVPairs = [NSMutableDictionary dictionaryWithObject:identifierStr forKey:KEY_PASSWORD];
  71. //IDFA
  72. NSString *adId = [[[ASIdentifierManager sharedManager] advertisingIdentifier] UUIDString];
  73. if ([adId isEqualToString:@"00000000-0000-0000-0000-000000000000"] || [adId isEqualToString:@"00000000000000000000000000000000"]) {
  74. ShowMsg(@"获取广告标识失败,请在“设置-隐私-广告”中,关闭“限制广告跟踪”");
  75. }else{
  76. NSMutableDictionary *usernamepasswordKVPairs = [NSMutableDictionary dictionaryWithObject:adId forKey:KEY_PASSWORD];
  77. [Tools save:KEY_USERNAME_PASSWORD data:usernamepasswordKVPairs];
  78. }
  79. return adId;
  80. }else{
  81. return [readUserPwd objectForKey:KEY_PASSWORD];
  82. // return defUser.userDict[@"imei"];
  83. }
  84. }
  85. //存
  86. + (void)save:(NSString *)service data:(id)data {
  87. //Get search dictionary
  88. NSMutableDictionary *keychainQuery = [self getKeychainQuery:service];
  89. //Delete old item before add new item
  90. SecItemDelete((__bridge CFDictionaryRef)keychainQuery);
  91. //Add new object to search dictionary(Attention:the data format)
  92. [keychainQuery setObject:[NSKeyedArchiver archivedDataWithRootObject:data] forKey:(__bridge id)kSecValueData];
  93. //Add item to keychain with the search dictionary
  94. SecItemAdd((__bridge CFDictionaryRef)keychainQuery, NULL);
  95. }
  96. + (NSMutableDictionary *)getKeychainQuery:(NSString *)service {
  97. return [NSMutableDictionary dictionaryWithObjectsAndKeys:
  98. (__bridge id)kSecClassGenericPassword,(__bridge id)kSecClass,
  99. service, (__bridge id)kSecAttrService,
  100. service, (__bridge id)kSecAttrAccount,
  101. (__bridge id)kSecAttrAccessibleAfterFirstUnlock,(__bridge id)kSecAttrAccessible,
  102. nil];
  103. }
  104. //取
  105. + (id)load:(NSString *)service {
  106. id ret = nil;
  107. NSMutableDictionary *keychainQuery = [self getKeychainQuery:service];
  108. //Configure the search setting
  109. //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
  110. [keychainQuery setObject:(__bridge id)kCFBooleanTrue forKey:(__bridge id)kSecReturnData];
  111. [keychainQuery setObject:(__bridge id)kSecMatchLimitOne forKey:(__bridge id)kSecMatchLimit];
  112. CFDataRef keyData = NULL;
  113. if (SecItemCopyMatching((__bridge CFDictionaryRef)keychainQuery, (CFTypeRef *)&keyData) == noErr) {
  114. @try {
  115. ret = [NSKeyedUnarchiver unarchiveObjectWithData:(__bridge NSData *)keyData];
  116. } @catch (NSException *e) {
  117. NSLog(@"Unarchive of %@ failed: %@", service, e);
  118. } @finally {
  119. }
  120. }
  121. if (keyData)
  122. CFRelease(keyData);
  123. return ret;
  124. }
  125. //删除
  126. + (void)delete:(NSString *)service {
  127. NSMutableDictionary *keychainQuery = [self getKeychainQuery:service];
  128. SecItemDelete((__bridge CFDictionaryRef)keychainQuery);
  129. }
  130. @end