dkahgld

PublicFunction.m 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  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 *)getYestDay
  148. {
  149. NSDate * date = [NSDate date];//当前时间
  150. NSDate *lastDay = [NSDate dateWithTimeInterval:-24*60*60 sinceDate:date];//前一天
  151. NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
  152. [dateFormatter setDateFormat:@"yyyy-MM-dd"];
  153. NSString *dateString = [dateFormatter stringFromDate:lastDay];
  154. return dateString;
  155. }
  156. + (NSString *)getCurrentDay{
  157. NSDate *currentDate = [NSDate date];//获取当前时间,日期
  158. NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
  159. [dateFormatter setDateFormat:@"yyyy-MM-dd"];
  160. NSString *dateString = [dateFormatter stringFromDate:currentDate];
  161. return dateString;
  162. }
  163. + (BOOL)isValiMobile:(NSString *)mobile{
  164. if (mobile.length < 11){
  165. return NO;
  166. }else{
  167. /**
  168. * 移动号段正则表达式
  169. */
  170. NSString *CM_NUM = @"^((13[4-9])|(147)|(15[0-2,7-9])|(178)|(18[2-4,7-8]))\\d{8}|(1705)\\d{7}$";
  171. /**
  172. * 联通号段正则表达式
  173. */
  174. NSString *CU_NUM = @"^((13[0-2])|(145)|(15[5-6])|(176)|(18[5,6]))\\d{8}|(1709)\\d{7}$";
  175. /**
  176. * 电信号段正则表达式
  177. */
  178. NSString *CT_NUM = @"^((133)|(153)|(177)|(18[0,1,9]))\\d{8}$";
  179. NSPredicate *pred1 = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CM_NUM];
  180. BOOL isMatch1 = [pred1 evaluateWithObject:mobile];
  181. NSPredicate *pred2 = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CU_NUM];
  182. BOOL isMatch2 = [pred2 evaluateWithObject:mobile];
  183. NSPredicate *pred3 = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CT_NUM];
  184. BOOL isMatch3 = [pred3 evaluateWithObject:mobile];
  185. if (isMatch1 || isMatch2 || isMatch3) {
  186. return YES;
  187. }else{
  188. return NO;
  189. }
  190. }
  191. }
  192. +(NSString *)getNowTimeTimestamp{
  193. NSDate* dat = [NSDate dateWithTimeIntervalSinceNow:0];
  194. NSTimeInterval a=[dat timeIntervalSince1970];
  195. NSString*timeString = [NSString stringWithFormat:@"%0.f", a];//转为字符型
  196. ;
  197. return timeString;
  198. }
  199. + (NSString *)md5:(NSString *)str
  200. {
  201. const char *cStr = [str UTF8String];
  202. unsigned char result[16];
  203. CC_MD5(cStr, (CC_LONG)strlen(cStr), result);
  204. return [NSString stringWithFormat:
  205. @"%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
  206. result[0], result[1], result[2], result[3],
  207. result[4], result[5], result[6], result[7],
  208. result[8], result[9], result[10], result[11],
  209. result[12], result[13], result[14], result[15]
  210. ];
  211. }
  212. + (NSString *)convertToJsonData:(NSDictionary *)dict
  213. {
  214. NSError *error;
  215. NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dict options:NSJSONWritingPrettyPrinted error:&error];
  216. NSString *jsonString;
  217. if (!jsonData) {
  218. NSLog(@"%@",error);
  219. }else{
  220. jsonString = [[NSString alloc]initWithData:jsonData encoding:NSUTF8StringEncoding];
  221. }
  222. NSMutableString *mutStr = [NSMutableString stringWithString:jsonString];
  223. NSRange range = {0,jsonString.length};
  224. //去掉字符串中的空格
  225. [mutStr replaceOccurrencesOfString:@" " withString:@"" options:NSLiteralSearch range:range];
  226. NSRange range2 = {0,mutStr.length};
  227. //去掉字符串中的换行符
  228. [mutStr replaceOccurrencesOfString:@"\n" withString:@"" options:NSLiteralSearch range:range2];
  229. return mutStr;
  230. }
  231. #pragma mark---计算时间
  232. +(NSString *)DifferencebetweenCurrentDate:(NSString *)dateStr
  233. {
  234. //转date
  235. NSDateFormatter *formatter=[[NSDateFormatter alloc]init];
  236. [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
  237. NSDate *betweenDate=[formatter dateFromString:dateStr];
  238. // 时区相差8个小时 加上这个时区即是北京时间
  239. NSTimeInterval inter=[[NSDate date] timeIntervalSinceDate:betweenDate];
  240. if (inter<60) {//60秒内
  241. return [NSString stringWithFormat:@"%.0f秒",inter];
  242. }else if (inter<3600)
  243. {
  244. return [NSString stringWithFormat:@"%.0f分",inter/60];//返回分
  245. }else
  246. {
  247. return [NSString stringWithFormat:@"%.0f小时",inter/3600];//返回小时
  248. }
  249. return @"刚刚";
  250. }
  251. +(void)saveAccountWithPush:(NSString *)push
  252. {
  253. NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
  254. NSString *name =push;
  255. [defaults setObject: name forKey:@"push"];
  256. [defaults synchronize];
  257. }
  258. +(NSString*)getPush
  259. {
  260. NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
  261. return [defaults objectForKey:@"push"];
  262. }
  263. #pragma mark---修改头像
  264. +(void)saveAccountChangeInfoWithPush:(NSString *)push
  265. {
  266. NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
  267. NSString *name =push;
  268. [defaults setObject: name forKey:@"head"];
  269. [defaults synchronize];
  270. }
  271. +(NSString*)getPushHead
  272. {
  273. NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
  274. return [defaults objectForKey:@"head"];
  275. }
  276. +(void) jumpKuaishouWithstr:(NSString *)str{
  277. NSString *encodedString = [str stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
  278. NSString *encodeUrl = (NSString *)[encodedString stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet lowercaseLetterCharacterSet]];
  279. NSString *schemeUrlString = [NSString stringWithFormat:@"%@%@", @"kwai://webview?url=", encodeUrl];
  280. NSURL *schemeUrl = [NSURL URLWithString:schemeUrlString];
  281. if ([self canOpenURL:schemeUrlString]) {
  282. dispatch_async(dispatch_get_main_queue(), ^{
  283. [[UIApplication sharedApplication] openURL:schemeUrl];
  284. });
  285. }else{
  286. [MBProgressHUD showTip:KWaiTipe];
  287. }
  288. }
  289. /**
  290. 判断是否安装淘宝app
  291. */
  292. + (BOOL)isInstallTaobaoAPP {
  293. if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"taobao://"]]) {
  294. return YES;
  295. }else {
  296. return NO;
  297. }
  298. }
  299. + (BOOL)canOpenURL:(NSString *)url {
  300. return [[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:url]];
  301. }
  302. //保存本地金额显示
  303. +(void)saveMoneyHidden:(NSString *)isHidden
  304. {
  305. NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
  306. //yes为显示金额,no为隐藏
  307. [defaults setObject: isHidden forKey:@"moneyHidden"];
  308. [defaults synchronize];
  309. }
  310. +(NSString *)GetMoneyStatus
  311. {
  312. NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
  313. return [defaults objectForKey:@"moneyHidden"];
  314. }
  315. //本月ps number 0 为当前月份 依次类推
  316. +(NSString *)backToPassedTimeWithMonthNumber:(NSInteger)number
  317. {
  318. NSCalendar *cal = [NSCalendar currentCalendar];
  319. NSDateComponents *components = [cal components:NSCalendarUnitMonth | NSCalendarUnitYear | NSCalendarUnitDay | NSCalendarUnitHour fromDate:[[NSDate alloc] init]];
  320. [components setMonth:[components month] - number];
  321. NSDate *monthDate = [cal dateFromComponents:components];
  322. NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
  323. [formatter setDateFormat:@"yyyy-MM"];
  324. [formatter setLocale:[NSLocale localeWithLocaleIdentifier:@"zn_CN"]];
  325. return [formatter stringFromDate:monthDate];
  326. }
  327. #pragma mark---b保存聊天记录
  328. +(void)saveMessage:(NSDictionary *)message
  329. {
  330. NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
  331. //yes为显示金额,no为隐藏
  332. [defaults setObject: message forKey:@"message"];
  333. [defaults synchronize];
  334. }
  335. +(NSDictionary *)getMessage
  336. {
  337. NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
  338. return [defaults objectForKey:@"message"];
  339. }
  340. /// 添加单边阴影效果
  341. +(void)addShadowToView:(UIView *)theView withColor:(UIColor *)theColor {
  342. theView.layer.shadowColor = theColor.CGColor;
  343. theView.layer.shadowOffset = CGSizeMake(0,0);
  344. theView.layer.shadowOpacity = 0.5;
  345. theView.layer.shadowRadius = 2;
  346. theView.clipsToBounds=NO;
  347. theView.layer.masksToBounds=NO;
  348. // 单边阴影 顶边
  349. float shadowPathWidth = theView.layer.shadowRadius;
  350. CGRect shadowRect = CGRectMake(0, 0-shadowPathWidth/2.0, theView.bounds.size.width, shadowPathWidth);
  351. UIBezierPath *path = [UIBezierPath bezierPathWithRect:shadowRect];
  352. theView.layer.shadowPath = path.CGPath;
  353. }
  354. + (CGSize)sizeWithString:(NSString *)str font:(UIFont *)font maxSize:(CGSize)maxSize
  355. {
  356. NSDictionary *dict = @{NSFontAttributeName: font};
  357. CGSize textSize = [str boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:dict context:nil].size;
  358. return textSize;
  359. }
  360. @end