// // PublicFunction.m // CommerceManage // // Created by 小花 on 2016/12/28. // Copyright © 2016年 vaic. All rights reserved. // #import "PublicFunction.h" #import #import "Reachability.h" @implementation PublicFunction +(BOOL)isAvailableNetworkType{ Reachability *reachability = [Reachability reachabilityWithHostName:@"www.baidu.com"]; NetworkStatus internetStatus = [reachability currentReachabilityStatus]; BOOL net = NO; switch (internetStatus) { case ReachableViaWiFi: net = YES; break; case ReachableViaWWAN: net =YES;break; case NotReachable: net = NO; default:net=NO;break; } if (net) { NSLog(@"isAvailableNetworkType-可以上网"); }else{ NSLog(@"isAvailableNetworkType-不可以上网"); } return net; } + (BOOL)isOuttimeDate:(NSString *)dateStr { NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateFormat:@"yyyy-MM-dd"]; NSDate *date = [dateFormatter dateFromString:dateStr]; NSString *current = [dateFormatter stringFromDate:[NSDate date]]; NSDate *currentDate = [dateFormatter dateFromString:current]; NSComparisonResult result = [date compare:currentDate]; if (result == NSOrderedDescending) { //未过期 return NO; } else if (result == NSOrderedAscending){ //过期 return YES; } //刚好时间一样.未过期 return NO; } + (NSString *)getDateWith:(NSString *)ctime { NSTimeInterval time=[ctime doubleValue]/1000; NSDate * detaildate=[NSDate dateWithTimeIntervalSince1970:time]; //实例化一个NSDateFormatter对象 NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateFormat:@"yyyy-MM-dd"]; NSString *currentDateStr = [dateFormatter stringFromDate:detaildate]; return currentDateStr; } + (NSString *)getDateWithReturnHourWith:(NSString *)ctime{ NSTimeInterval time=[ctime doubleValue]/1000; NSDate * detaildate=[NSDate dateWithTimeIntervalSince1970:time]; //实例化一个NSDateFormatter对象 NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm"]; NSString *currentDateStr = [dateFormatter stringFromDate:detaildate]; return currentDateStr; } + (NSString *)addSeparatorPointForPriceString:(NSString *)str { NSMutableString *priceStr = str.mutableCopy; NSRange range = [priceStr rangeOfString:@"."]; NSInteger index = 0; if (range.length > 0 ) { index = range.location; }else { index = str.length; } while ((index - 3) > 0) { index -= 3; [priceStr insertString:@"," atIndex:index]; } priceStr = [priceStr stringByReplacingOccurrencesOfString:@"." withString:@","].mutableCopy; return priceStr; } + (void)isOpenMessageNotificationServiceWithBlock:(void(^)(BOOL))returnBlock { BOOL isOpen = NO; #if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_8_0 UIUserNotificationSettings *setting = [[UIApplication sharedApplication] currentUserNotificationSettings]; if (setting.types != UIUserNotificationTypeNone) { isOpen = YES; } #else UIRemoteNotificationType type = [[UIApplication sharedApplication] enabledRemoteNotificationTypes]; if (type != UIRemoteNotificationTypeNone) { isOpen = YES; } #endif if (returnBlock) { returnBlock(isOpen); } } + (CGSize)getAutoWidthWith:(NSString *)text andSize:(CGSize)size andFont:(NSInteger)font { NSDictionary *attributes = @{NSFontAttributeName:[UIFont systemFontOfSize:font]}; CGSize textSize = [text boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:attributes context:nil].size; return textSize; } + (NSString *)getChineseCalendarWithDate:(NSString*)date{ NSArray *chineseYears = [NSArray arrayWithObjects: @"甲子", @"乙丑", @"丙寅", @"丁卯", @"戊辰", @"己巳", @"庚午", @"辛未", @"壬申", @"癸酉", @"甲戌", @"乙亥", @"丙子", @"丁丑", @"戊寅", @"己卯", @"庚辰", @"辛己", @"壬午", @"癸未", @"甲申", @"乙酉", @"丙戌", @"丁亥", @"戊子", @"己丑", @"庚寅", @"辛卯", @"壬辰", @"癸巳", @"甲午", @"乙未", @"丙申", @"丁酉", @"戊戌", @"己亥", @"庚子", @"辛丑", @"壬寅", @"癸丑", @"甲辰", @"乙巳", @"丙午", @"丁未", @"戊申", @"己酉", @"庚戌", @"辛亥", @"壬子", @"癸丑", @"甲寅", @"乙卯", @"丙辰", @"丁巳", @"戊午", @"己未", @"庚申", @"辛酉", @"壬戌", @"癸亥", nil]; NSArray *chineseMonths=[NSArray arrayWithObjects: @"正月", @"二月", @"三月", @"四月", @"五月", @"六月", @"七月", @"八月", @"九月", @"十月", @"冬月", @"腊月", nil]; NSArray *chineseDays=[NSArray arrayWithObjects: @"初一", @"初二", @"初三", @"初四", @"初五", @"初六", @"初七", @"初八", @"初九", @"初十", @"十一", @"十二", @"十三", @"十四", @"十五", @"十六", @"十七", @"十八", @"十九", @"二十", @"廿一", @"廿二", @"廿三", @"廿四", @"廿五", @"廿六", @"廿七", @"廿八", @"廿九", @"三十", nil]; NSArray *chineseWeekDays = [NSArray arrayWithObjects: @"星期日",@"星期一",@"星期二",@"星期三",@"星期四",@"星期五",@"星期六" , nil]; // [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"]; NSDate *dateTemp = nil; NSDateFormatter *dateFormater = [[NSDateFormatter alloc]init]; [dateFormater setDateFormat:@"yyyy-MM-dd"]; dateTemp = [dateFormater dateFromString:date]; NSCalendar *localeCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierChinese]; unsigned unitFlags = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitWeekday; NSDateComponents *localeComp = [localeCalendar components:unitFlags fromDate:dateTemp]; NSLog(@"%ld_%ld_%ld %@",(long)localeComp.year,(long)localeComp.month,(long)localeComp.day, localeComp.date); // NSString *y_str = [chineseYears objectAtIndex:localeComp.year-1]; NSString *m_str = [chineseMonths objectAtIndex:localeComp.month-1]; NSString *d_str = [chineseDays objectAtIndex:localeComp.day-1]; NSString *week_day = [chineseWeekDays objectAtIndex:localeComp.weekday-1]; NSString *chineseCal_str =[NSString stringWithFormat: @"%@%@ %@",m_str,d_str,week_day]; return chineseCal_str; } + (NSString *)getCurrentDate { NSDate *currentDate = [NSDate date];//获取当前时间,日期 NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateFormat:@"MM-dd"]; NSString *dateString = [dateFormatter stringFromDate:currentDate]; return dateString; } + (NSString *)getCurrentDay{ NSDate *currentDate = [NSDate date];//获取当前时间,日期 NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateFormat:@"dd"]; NSString *dateString = [dateFormatter stringFromDate:currentDate]; return dateString; } + (BOOL)isValiMobile:(NSString *)mobile{ if (mobile.length < 11){ return NO; }else{ /** * 移动号段正则表达式 */ NSString *CM_NUM = @"^((13[4-9])|(147)|(15[0-2,7-9])|(178)|(18[2-4,7-8]))\\d{8}|(1705)\\d{7}$"; /** * 联通号段正则表达式 */ NSString *CU_NUM = @"^((13[0-2])|(145)|(15[5-6])|(176)|(18[5,6]))\\d{8}|(1709)\\d{7}$"; /** * 电信号段正则表达式 */ NSString *CT_NUM = @"^((133)|(153)|(177)|(18[0,1,9]))\\d{8}$"; NSPredicate *pred1 = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CM_NUM]; BOOL isMatch1 = [pred1 evaluateWithObject:mobile]; NSPredicate *pred2 = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CU_NUM]; BOOL isMatch2 = [pred2 evaluateWithObject:mobile]; NSPredicate *pred3 = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CT_NUM]; BOOL isMatch3 = [pred3 evaluateWithObject:mobile]; if (isMatch1 || isMatch2 || isMatch3) { return YES; }else{ return NO; } } } +(NSString *)getNowTimeTimestamp{ NSDate* dat = [NSDate dateWithTimeIntervalSinceNow:0]; NSTimeInterval a=[dat timeIntervalSince1970]; NSString*timeString = [NSString stringWithFormat:@"%0.f", a];//转为字符型 ; return timeString; } /** 判断是否安装淘宝app */ + (BOOL)isInstallTaobaoAPP { if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"taobao://"]]) { return YES; }else { return NO; } } + (NSString *)md5:(NSString *)str { const char *cStr = [str UTF8String]; unsigned char result[16]; CC_MD5(cStr, (CC_LONG)strlen(cStr), result); return [NSString stringWithFormat: @"%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x", result[0], result[1], result[2], result[3], result[4], result[5], result[6], result[7], result[8], result[9], result[10], result[11], result[12], result[13], result[14], result[15] ]; } + (NSString *)convertToJsonData:(NSDictionary *)dict { NSError *error; NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dict options:NSJSONWritingPrettyPrinted error:&error]; NSString *jsonString; if (!jsonData) { NSLog(@"%@",error); }else{ jsonString = [[NSString alloc]initWithData:jsonData encoding:NSUTF8StringEncoding]; } NSMutableString *mutStr = [NSMutableString stringWithString:jsonString]; NSRange range = {0,jsonString.length}; //去掉字符串中的空格 [mutStr replaceOccurrencesOfString:@" " withString:@"" options:NSLiteralSearch range:range]; NSRange range2 = {0,mutStr.length}; //去掉字符串中的换行符 [mutStr replaceOccurrencesOfString:@"\n" withString:@"" options:NSLiteralSearch range:range2]; return mutStr; } @end