口袋优选

PublicFunction.m 11KB

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