説明なし

PublicFunction.m 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. //
  2. // PublicFunction.m
  3. // CommerceManage
  4. //
  5. // Created by 小花 on 2016/12/28.
  6. // Copyright © 2016年 vaic. All rights reserved.
  7. //
  8. #import "PublicFunction.h"
  9. #import <CommonCrypto/CommonDigest.h>
  10. #import "Reachability.h"
  11. @implementation PublicFunction
  12. +(BOOL)isAvailableNetworkType{
  13. Reachability *reachability = [Reachability reachabilityWithHostName:@"www.baidu.com"];
  14. NetworkStatus internetStatus = [reachability currentReachabilityStatus];
  15. BOOL net = NO;
  16. switch (internetStatus) {
  17. case ReachableViaWiFi:
  18. net = YES;
  19. break;
  20. case ReachableViaWWAN:
  21. net =YES;break;
  22. case NotReachable:
  23. net = NO;
  24. default:net=NO;break;
  25. }
  26. if (net) {
  27. NSLog(@"isAvailableNetworkType-可以上网");
  28. }else{
  29. NSLog(@"isAvailableNetworkType-不可以上网");
  30. }
  31. return net;
  32. }
  33. + (BOOL)isOuttimeDate:(NSString *)dateStr {
  34. NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
  35. [dateFormatter setDateFormat:@"yyyy-MM-dd"];
  36. NSDate *date = [dateFormatter dateFromString:dateStr];
  37. NSString *current = [dateFormatter stringFromDate:[NSDate date]];
  38. NSDate *currentDate = [dateFormatter dateFromString:current];
  39. NSComparisonResult result = [date compare:currentDate];
  40. if (result == NSOrderedDescending) {
  41. //未过期
  42. return NO;
  43. }
  44. else if (result == NSOrderedAscending){
  45. //过期
  46. return YES;
  47. }
  48. //刚好时间一样.未过期
  49. return NO;
  50. }
  51. + (NSString *)getDateWith:(NSString *)ctime {
  52. NSTimeInterval time=[ctime doubleValue]/1000;
  53. NSDate * detaildate=[NSDate dateWithTimeIntervalSince1970:time];
  54. //实例化一个NSDateFormatter对象
  55. NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
  56. [dateFormatter setDateFormat:@"yyyy-MM-dd"];
  57. NSString *currentDateStr = [dateFormatter stringFromDate:detaildate];
  58. return currentDateStr;
  59. }
  60. + (NSString *)getDateWithReturnHourWith:(NSString *)ctime{
  61. NSTimeInterval time=[ctime doubleValue]/1000;
  62. NSDate * detaildate=[NSDate dateWithTimeIntervalSince1970:time];
  63. //实例化一个NSDateFormatter对象
  64. NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
  65. [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm"];
  66. NSString *currentDateStr = [dateFormatter stringFromDate:detaildate];
  67. return currentDateStr;
  68. }
  69. + (NSString *)addSeparatorPointForPriceString:(NSString *)str {
  70. NSMutableString *priceStr = str.mutableCopy;
  71. NSRange range = [priceStr rangeOfString:@"."];
  72. NSInteger index = 0;
  73. if (range.length > 0 ) {
  74. index = range.location;
  75. }else {
  76. index = str.length;
  77. }
  78. while ((index - 3) > 0) {
  79. index -= 3;
  80. [priceStr insertString:@"," atIndex:index];
  81. }
  82. priceStr = [priceStr stringByReplacingOccurrencesOfString:@"." withString:@","].mutableCopy;
  83. return priceStr;
  84. }
  85. + (void)isOpenMessageNotificationServiceWithBlock:(void(^)(BOOL))returnBlock
  86. {
  87. BOOL isOpen = NO;
  88. #if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_8_0
  89. UIUserNotificationSettings *setting = [[UIApplication sharedApplication] currentUserNotificationSettings];
  90. if (setting.types != UIUserNotificationTypeNone) {
  91. isOpen = YES;
  92. }
  93. #else
  94. UIRemoteNotificationType type = [[UIApplication sharedApplication] enabledRemoteNotificationTypes];
  95. if (type != UIRemoteNotificationTypeNone) {
  96. isOpen = YES;
  97. }
  98. #endif
  99. if (returnBlock) {
  100. returnBlock(isOpen);
  101. }
  102. }
  103. + (CGSize)getAutoWidthWith:(NSString *)text andSize:(CGSize)size andFont:(NSInteger)font {
  104. NSDictionary *attributes = @{NSFontAttributeName:[UIFont systemFontOfSize:font]};
  105. CGSize textSize = [text boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:attributes context:nil].size;
  106. return textSize;
  107. }
  108. + (NSString *)getChineseCalendarWithDate:(NSString*)date{
  109. NSArray *chineseYears = [NSArray arrayWithObjects:
  110. @"甲子", @"乙丑", @"丙寅", @"丁卯", @"戊辰", @"己巳", @"庚午", @"辛未", @"壬申", @"癸酉",
  111. @"甲戌", @"乙亥", @"丙子", @"丁丑", @"戊寅", @"己卯", @"庚辰", @"辛己", @"壬午", @"癸未",
  112. @"甲申", @"乙酉", @"丙戌", @"丁亥", @"戊子", @"己丑", @"庚寅", @"辛卯", @"壬辰", @"癸巳",
  113. @"甲午", @"乙未", @"丙申", @"丁酉", @"戊戌", @"己亥", @"庚子", @"辛丑", @"壬寅", @"癸丑",
  114. @"甲辰", @"乙巳", @"丙午", @"丁未", @"戊申", @"己酉", @"庚戌", @"辛亥", @"壬子", @"癸丑",
  115. @"甲寅", @"乙卯", @"丙辰", @"丁巳", @"戊午", @"己未", @"庚申", @"辛酉", @"壬戌", @"癸亥", nil];
  116. NSArray *chineseMonths=[NSArray arrayWithObjects:
  117. @"正月", @"二月", @"三月", @"四月", @"五月", @"六月", @"七月", @"八月",
  118. @"九月", @"十月", @"冬月", @"腊月", nil];
  119. NSArray *chineseDays=[NSArray arrayWithObjects:
  120. @"初一", @"初二", @"初三", @"初四", @"初五", @"初六", @"初七", @"初八", @"初九", @"初十",
  121. @"十一", @"十二", @"十三", @"十四", @"十五", @"十六", @"十七", @"十八", @"十九", @"二十",
  122. @"廿一", @"廿二", @"廿三", @"廿四", @"廿五", @"廿六", @"廿七", @"廿八", @"廿九", @"三十", nil];
  123. NSArray *chineseWeekDays = [NSArray arrayWithObjects:
  124. @"星期日",@"星期一",@"星期二",@"星期三",@"星期四",@"星期五",@"星期六"
  125. , nil];
  126. // [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
  127. NSDate *dateTemp = nil;
  128. NSDateFormatter *dateFormater = [[NSDateFormatter alloc]init];
  129. [dateFormater setDateFormat:@"yyyy-MM-dd"];
  130. dateTemp = [dateFormater dateFromString:date];
  131. NSCalendar *localeCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierChinese];
  132. unsigned unitFlags = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitWeekday;
  133. NSDateComponents *localeComp = [localeCalendar components:unitFlags fromDate:dateTemp];
  134. NSLog(@"%ld_%ld_%ld %@",(long)localeComp.year,(long)localeComp.month,(long)localeComp.day, localeComp.date);
  135. // NSString *y_str = [chineseYears objectAtIndex:localeComp.year-1];
  136. NSString *m_str = [chineseMonths objectAtIndex:localeComp.month-1];
  137. NSString *d_str = [chineseDays objectAtIndex:localeComp.day-1];
  138. NSString *week_day = [chineseWeekDays objectAtIndex:localeComp.weekday-1];
  139. NSString *chineseCal_str =[NSString stringWithFormat: @"%@%@ %@",m_str,d_str,week_day];
  140. return chineseCal_str;
  141. }
  142. + (NSString *)getCurrentDate {
  143. NSDate *currentDate = [NSDate date];//获取当前时间,日期
  144. NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
  145. [dateFormatter setDateFormat:@"MM-dd"];
  146. NSString *dateString = [dateFormatter stringFromDate:currentDate];
  147. return dateString;
  148. }
  149. + (NSString *)getCurrentDay{
  150. NSDate *currentDate = [NSDate date];//获取当前时间,日期
  151. NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
  152. [dateFormatter setDateFormat:@"dd"];
  153. NSString *dateString = [dateFormatter stringFromDate:currentDate];
  154. return dateString;
  155. }
  156. + (BOOL)isValiMobile:(NSString *)mobile{
  157. if (mobile.length < 11){
  158. return NO;
  159. }else{
  160. /**
  161. * 移动号段正则表达式
  162. */
  163. NSString *CM_NUM = @"^((13[4-9])|(147)|(15[0-2,7-9])|(178)|(18[2-4,7-8]))\\d{8}|(1705)\\d{7}$";
  164. /**
  165. * 联通号段正则表达式
  166. */
  167. NSString *CU_NUM = @"^((13[0-2])|(145)|(15[5-6])|(176)|(18[5,6]))\\d{8}|(1709)\\d{7}$";
  168. /**
  169. * 电信号段正则表达式
  170. */
  171. NSString *CT_NUM = @"^((133)|(153)|(177)|(18[0,1,9]))\\d{8}$";
  172. NSPredicate *pred1 = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CM_NUM];
  173. BOOL isMatch1 = [pred1 evaluateWithObject:mobile];
  174. NSPredicate *pred2 = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CU_NUM];
  175. BOOL isMatch2 = [pred2 evaluateWithObject:mobile];
  176. NSPredicate *pred3 = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CT_NUM];
  177. BOOL isMatch3 = [pred3 evaluateWithObject:mobile];
  178. if (isMatch1 || isMatch2 || isMatch3) {
  179. return YES;
  180. }else{
  181. return NO;
  182. }
  183. }
  184. }
  185. +(NSString *)getNowTimeTimestamp{
  186. NSDate* dat = [NSDate dateWithTimeIntervalSinceNow:0];
  187. NSTimeInterval a=[dat timeIntervalSince1970];
  188. NSString*timeString = [NSString stringWithFormat:@"%0.f", a];//转为字符型
  189. ;
  190. return timeString;
  191. }
  192. /**
  193. 判断是否安装淘宝app
  194. */
  195. + (BOOL)isInstallTaobaoAPP {
  196. if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"taobao://"]]) {
  197. return YES;
  198. }else {
  199. return NO;
  200. }
  201. }
  202. + (NSString *)md5:(NSString *)str
  203. {
  204. const char *cStr = [str UTF8String];
  205. unsigned char result[16];
  206. CC_MD5(cStr, (CC_LONG)strlen(cStr), result);
  207. return [NSString stringWithFormat:
  208. @"%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
  209. result[0], result[1], result[2], result[3],
  210. result[4], result[5], result[6], result[7],
  211. result[8], result[9], result[10], result[11],
  212. result[12], result[13], result[14], result[15]
  213. ];
  214. }
  215. + (NSString *)convertToJsonData:(NSDictionary *)dict
  216. {
  217. NSError *error;
  218. NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dict options:NSJSONWritingPrettyPrinted error:&error];
  219. NSString *jsonString;
  220. if (!jsonData) {
  221. NSLog(@"%@",error);
  222. }else{
  223. jsonString = [[NSString alloc]initWithData:jsonData encoding:NSUTF8StringEncoding];
  224. }
  225. NSMutableString *mutStr = [NSMutableString stringWithString:jsonString];
  226. NSRange range = {0,jsonString.length};
  227. //去掉字符串中的空格
  228. [mutStr replaceOccurrencesOfString:@" " withString:@"" options:NSLiteralSearch range:range];
  229. NSRange range2 = {0,mutStr.length};
  230. //去掉字符串中的换行符
  231. [mutStr replaceOccurrencesOfString:@"\n" withString:@"" options:NSLiteralSearch range:range2];
  232. return mutStr;
  233. }
  234. @end