123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470 |
- //
- // PublicFunction.m
- // CommerceManage
- //
- // Created by 小花 on 2016/12/28.
- // Copyright © 2016年 vaic. All rights reserved.
- //
- #import "PublicFunction.h"
- #import <CommonCrypto/CommonDigest.h>
- //#import "Reachability.h"
- @implementation PublicFunction
- +(BOOL)isAvailableNetworkType{
- struct sockaddr zeroAddress;
- bzero(&zeroAddress, sizeof(zeroAddress));
- zeroAddress.sa_len = sizeof(zeroAddress);
- zeroAddress.sa_family = AF_INET;
-
- SCNetworkReachabilityRef defaultRouteReachability =
- SCNetworkReachabilityCreateWithAddress(NULL, (struct sockaddr *)&zeroAddress);
- SCNetworkReachabilityFlags flags;
-
- BOOL didRetrieveFlags =
- SCNetworkReachabilityGetFlags(defaultRouteReachability, &flags);
- CFRelease(defaultRouteReachability);
-
- if (!didRetrieveFlags) {
- printf("Error. Count not recover network reachability flags\n");
- return NO;
- }
-
- BOOL isReachable = flags & kSCNetworkFlagsReachable;
- BOOL needsConnection = flags & kSCNetworkFlagsConnectionRequired;
- return (isReachable && !needsConnection) ? YES : NO;
- }
- + (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 *)getYestDay
- {
- NSDate * date = [NSDate date];//当前时间
-
- NSDate *lastDay = [NSDate dateWithTimeInterval:-24*60*60 sinceDate:date];//前一天
-
- NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
- [dateFormatter setDateFormat:@"yyyy-MM-dd"];
- NSString *dateString = [dateFormatter stringFromDate:lastDay];
- return dateString;
- }
- + (NSString *)getCurrentDay{
-
- NSDate *currentDate = [NSDate date];//获取当前时间,日期
- NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
- [dateFormatter setDateFormat:@"yyyy-MM-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;
-
- }
- + (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;
-
- }
- #pragma mark---计算时间
- +(NSString *)DifferencebetweenCurrentDate:(NSString *)dateStr
- {
- //转date
- NSDateFormatter *formatter=[[NSDateFormatter alloc]init];
- [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
- NSDate *betweenDate=[formatter dateFromString:dateStr];
-
- // 时区相差8个小时 加上这个时区即是北京时间
- NSTimeInterval inter=[[NSDate date] timeIntervalSinceDate:betweenDate];
- if (inter<60) {//60秒内
- return [NSString stringWithFormat:@"%.0f秒",inter];
- }else if (inter<3600)
- {
- return [NSString stringWithFormat:@"%.0f分",inter/60];//返回分
- }else
- {
- return [NSString stringWithFormat:@"%.0f小时",inter/3600];//返回小时
- }
-
- return @"刚刚";
- }
- +(void)saveAccountWithPush:(NSString *)push
- {
- NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
- NSString *name =push;
- [defaults setObject: name forKey:@"push"];
- [defaults synchronize];
- }
- +(NSString*)getPush
- {
- NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
-
- return [defaults objectForKey:@"push"];
- }
- #pragma mark---修改头像
- +(void)saveAccountChangeInfoWithPush:(NSString *)push
- {
- NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
- NSString *name =push;
- [defaults setObject: name forKey:@"head"];
- [defaults synchronize];
- }
- +(NSString*)getPushHead
- {
- NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
-
- return [defaults objectForKey:@"head"];
- }
- +(void) jumpKuaishouWithstr:(NSString *)str{
-
-
-
- NSString *encodedString = [str stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
- NSString *encodeUrl = (NSString *)[encodedString stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet lowercaseLetterCharacterSet]];
-
- NSString *schemeUrlString = [NSString stringWithFormat:@"%@%@", @"kwai://webview?url=", encodeUrl];
- NSURL *schemeUrl = [NSURL URLWithString:schemeUrlString];
- if ([self canOpenURL:schemeUrlString]) {
- dispatch_async(dispatch_get_main_queue(), ^{
- [[UIApplication sharedApplication] openURL:schemeUrl];
- });
- }else{
- [MBProgressHUD showTip:KWaiTipe];
- }
-
-
-
- }
- /**
- 判断是否安装淘宝app
- */
- + (BOOL)isInstallTaobaoAPP {
- if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"taobao://"]]) {
- return YES;
- }else {
- return NO;
- }
- }
- + (BOOL)canOpenURL:(NSString *)url {
- return [[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:url]];
- }
- //保存本地金额显示
- +(void)saveMoneyHidden:(NSString *)isHidden
- {
- NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
- //yes为显示金额,no为隐藏
- [defaults setObject: isHidden forKey:@"moneyHidden"];
- [defaults synchronize];
-
- }
- +(NSString *)GetMoneyStatus
- {
- NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
-
- return [defaults objectForKey:@"moneyHidden"];
- }
- //本月ps number 0 为当前月份 依次类推
- +(NSString *)backToPassedTimeWithMonthNumber:(NSInteger)number
- {
- NSCalendar *cal = [NSCalendar currentCalendar];
- NSDateComponents *components = [cal components:NSCalendarUnitMonth | NSCalendarUnitYear | NSCalendarUnitDay | NSCalendarUnitHour fromDate:[[NSDate alloc] init]];
- [components setMonth:[components month] - number];
-
- NSDate *monthDate = [cal dateFromComponents:components];
-
- NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
- [formatter setDateFormat:@"yyyy-MM"];
- [formatter setLocale:[NSLocale localeWithLocaleIdentifier:@"zn_CN"]];
- return [formatter stringFromDate:monthDate];
- }
- #pragma mark---b保存聊天记录
- +(void)saveMessage:(NSDictionary *)message
- {
- NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
- //yes为显示金额,no为隐藏
- [defaults setObject: message forKey:@"message"];
- [defaults synchronize];
- }
- +(NSDictionary *)getMessage
- {
- NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
-
- return [defaults objectForKey:@"message"];
- }
- /// 添加单边阴影效果
- +(void)addShadowToView:(UIView *)theView withColor:(UIColor *)theColor {
- theView.layer.shadowColor = theColor.CGColor;
- theView.layer.shadowOffset = CGSizeMake(0,0);
- theView.layer.shadowOpacity = 0.5;
- theView.layer.shadowRadius = 2;
- theView.clipsToBounds=NO;
- theView.layer.masksToBounds=NO;
- // 单边阴影 顶边
- float shadowPathWidth = theView.layer.shadowRadius;
- CGRect shadowRect = CGRectMake(0, 0-shadowPathWidth/2.0, theView.bounds.size.width, shadowPathWidth);
- UIBezierPath *path = [UIBezierPath bezierPathWithRect:shadowRect];
- theView.layer.shadowPath = path.CGPath;
- }
- + (CGSize)sizeWithString:(NSString *)str font:(UIFont *)font maxSize:(CGSize)maxSize
- {
- NSDictionary *dict = @{NSFontAttributeName: font};
- CGSize textSize = [str boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:dict context:nil].size;
- return textSize;
- }
- @end
|