123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630 |
- //
- // GlobalMethod.m
- // 米兰港
- //
- // Created by 隋林栋 on 15/3/3.
- // Copyright (c) 2015年 Sl. All rights reserved.
- //
- #import "GlobalMethod.h"
- #import <sys/utsname.h>
- //数学
- #import <math.h>
- //提示框
- #import "NoticeView.h"
- //网络请求
- #import "AFNetworkReachabilityManager.h"
- //yykit
- //#import <YYKit/YYKit.h>
- @implementation GlobalMethod
- #pragma mark //解析错误信息
- + (NSString *)returnErrorMessage:(id)error{
- if (error == nil) {
- return @"错误为空";
- }
- if ([error isKindOfClass:[NSString class]]) {
- NSString * strError = (NSString *)error;
- if (strError.length >0) {
- return strError;
- }
- return @"错误为空";
- }
- if ([error isKindOfClass:[NSDictionary class]]) {
- NSString * strError = [error objectForKey:@"Message"];
- return strError;
- }
- return @"错误为空";
- }
- #pragma mark 角标清零
- + (void)zeroIcon{
- [[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
-
- }
- //处理小数点
- + (NSString *)exchangeNum:(double)num{
- NSString * strReturn =[NSString stringWithFormat:@"%.02f",round(num*100)/100 ];
- return strReturn;
- }
- //去掉空格
- + (NSString *)exchangeEmpty:(NSString *)str{
-
- if (str == nil || ![str isKindOfClass:[NSString class]]) {
- return @"";
- }
- NSString * strReturn = [str stringByReplacingOccurrencesOfString:@" " withString:@""];
- return strReturn;
- }
- //去掉换行
- + (NSString *)exchangeBlack:(NSString *)str{
-
- if (str == nil || ![str isKindOfClass:[NSString class]]) {
- return @"";
- }
- NSString * strReturn = [str stringByReplacingOccurrencesOfString:@"\n" withString:@""];
- return strReturn;
- }
- //转换为jsong
- + (NSString *)exchangeDicToJson:(id)object{
- if (object == nil) {
- NSLog(@"error json转换错误");
- return @"";
- }
- if ([object isKindOfClass:[NSDictionary class]]||[object isKindOfClass:[NSArray class]]) {
- NSData * dataJson = [NSJSONSerialization dataWithJSONObject:object options:0 error:nil];
- NSString * strJson = [[NSString alloc]initWithData:dataJson encoding:NSUTF8StringEncoding];
- return strJson;
- }
- return @"";
- }
- //转换为jsong
- + (NSString *)exchangeModelsToJson:(NSArray *)object{
- if (object == nil) {
- NSLog(@"error json转换错误");
- return @"";
- }
- NSMutableArray * aryMu = [NSMutableArray new];
- if ([object isKindOfClass:[NSArray class]]) {
- for (id model in object) {
- NSDictionary * dic = [GlobalMethod performSelector:@"dictionaryRepresentation" delegate:model object:nil isHasReturn:true];
- if (dic != nil) {
- [aryMu addObject:dic];
- }
- }
- }
-
- return [self exchangeDicToJson:aryMu];
- }
- //转换json
- + (NSString *)exchangeModelToJson:(id)model{
- if (!model) return @"";
- if (![model respondsToSelector:@selector(dictionaryRepresentation)]) return @"";
- NSDictionary * dicJson = [model dictionaryRepresentation];
- NSData * dataJson = [NSJSONSerialization dataWithJSONObject:dicJson options:0 error:nil];
- NSString * strJson = [[NSString alloc]initWithData:dataJson encoding:NSUTF8StringEncoding];
- return strJson!=nil?strJson:@"";
- }
- + (NSMutableArray *)exchangeDic:(id)response toAryWithModelName:(NSString *)modelName{
- if (response == nil || !isStr(modelName)) {
- return [NSMutableArray array];
- }
- id class = NSClassFromString(modelName);
- if (class == nil) {
- return [NSMutableArray array];
- }
- if (response && [response isKindOfClass:[NSDictionary class]]) {
- if ([class respondsToSelector:@selector(modelObjectWithDictionary:)]) {
- id model = [class performSelector:@selector(modelObjectWithDictionary:) withObject:response];
- return [NSMutableArray arrayWithObject:model];
- }
- }
- if (response && [response isKindOfClass:[NSArray class]]) {
- NSMutableArray * aryReturn = [NSMutableArray array];
- for (NSDictionary * dic in (NSArray *)response) {
- if ([class respondsToSelector:@selector(modelObjectWithDictionary:)]) {
- id model = [class performSelector:@selector(modelObjectWithDictionary:) withObject:dic];
- [aryReturn addObject:model];
- }
- }
- return aryReturn;
- }
- return [NSMutableArray array];
- }
- + (NSMutableArray *)exchangeAryModelToAryDic:(NSArray *)response{
- if (!isAry(response)) {
- return [NSMutableArray array];
- }
- NSMutableArray * aryReturn = [NSMutableArray array];
- for (NSObject *subArrayObject in response) {
- if ([subArrayObject respondsToSelector:@selector(dictionaryRepresentation)]) {
- // This class is a model object
- [aryReturn addObject:[subArrayObject performSelector:@selector(dictionaryRepresentation)]];
- } else {
- // Generic object
- [aryReturn addObject:subArrayObject];
- }
- }
- return aryReturn;
- }
- + (id)exchangeDicToModel:(NSDictionary *)dic modelName:(NSString *)strName{
- if (!isStr(strName)) {
- return nil;
- }
- Class class = NSClassFromString(strName);
- if (class == nil) {
- return nil;
- }
- if ([dic isKindOfClass:[NSArray class]]) {
- return [self performSelector:@"modelObjectWithDictionary:" delegate:class object:[(NSArray *)dic lastObject] isHasReturn:true];
- }
- if (dic == nil || ![dic isKindOfClass:[NSDictionary class]]) {
- return [[class alloc]init];
- }
- return [self performSelector:@"modelObjectWithDictionary:" delegate:class object:dic isHasReturn:true];
- }
- //delegate 执行 selector
- + (id)performSelector:(NSString *)selectorName delegate:(id)delegate{
- return [self performSelector:selectorName delegate:delegate object:nil isHasReturn:false];
- }
- + (id)performSelector:(NSString *)selectorName delegate:(id)delegate object:(id)object isHasReturn:(BOOL)isHasReturn{
- SEL selector = NSSelectorFromString(selectorName);
- if (delegate != nil && [delegate respondsToSelector:selector]) {
- if (isHasReturn) {
- return [delegate performSelector:selector withObject:object];
- }else{
- [delegate performSelector:selector withObject:object];
- return nil;
- }
- }
- return nil;
- }
- + (id)performSelector:(NSString *)selectorName delegate:(id)delegate object:(id)object object:(id)object2 isHasReturn:(BOOL)isHasReturn{
- SEL selector = NSSelectorFromString(selectorName);
- if (delegate != nil && [delegate respondsToSelector:selector]) {
- if (isHasReturn) {
- return [delegate performSelector:selector withObject:object withObject:object2];
- }else{
- [delegate performSelector:selector withObject:object withObject:object2];
- return nil;
- }
- }
- return nil;
- }
- //移除空白
- + (void)removeEmpty:(NSMutableArray *)ary key:(NSString *)strKey{
- NSMutableDictionary * dic = [NSMutableDictionary dictionary];
- for (id model in ary) {
- id key = [model valueForKey:strKey];
- if (key != nil && [key isKindOfClass:[NSString class]]) {
- NSString * strKey = key;
- if (isStr(strKey)) {
- [dic setValue:model forKey:key];
- }
- }
- }
- [ary removeAllObjects];
- [ary addObjectsFromArray:dic.allValues];
- }
- //转换data to dic
- + (NSDictionary *)exchangeDataToDic:(NSData *)data{
- // NSString * str = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
- if (data == nil || ![data isKindOfClass:[NSData class]]) {
- return nil;
- }
- return [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
- }
- //转换String to dic
- + (NSDictionary *)exchangeStringToDic:(NSString *)str{
- if (!isStr(str)) {
- return [NSDictionary dictionary];
- }
- NSData * data = [str dataUsingEncoding:NSUTF8StringEncoding];
- NSDictionary * dicReturn = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
- return dicReturn;
- }
- //转换String to ary
- + (NSArray *)exchangeStringToAry:(NSString *)str{
- if (!isStr(str)) {
- return [NSArray array];
- }
- NSData * data = [str dataUsingEncoding:NSUTF8StringEncoding];
- NSArray * aryReturn = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
- return aryReturn;
- }
- //十六进制转颜色
- + (UIColor *)exchangeColorWith16:(NSString*)hexColor
- {
- return [GlobalMethod exchangeColorWith16:hexColor alpha:1.0f];
- }
- //十六进制转颜色 alpha
- + (UIColor *)exchangeColorWith16:(NSString*)hexColor alpha:(CGFloat)alpha
- {
- unsigned int red,green,blue;
- NSRange range;
- range.length = 2;
-
- range.location = 0;
- [[NSScanner scannerWithString:[hexColor substringWithRange:range]]scanHexInt:&red];
-
- range.location = 2;
- [[NSScanner scannerWithString:[hexColor substringWithRange:range]]scanHexInt:&green];
-
- range.location = 4;
- [[NSScanner scannerWithString:[hexColor substringWithRange:range]]scanHexInt:&blue];
-
- return [UIColor colorWithRed:(float)(red/255.0f)green:(float)(green / 255.0f) blue:(float)(blue / 255.0f)alpha:alpha];
- }
- //隐藏显示tag视图
- + (void)showHideViewWithTag:(int)tag inView:(UIView *)viewAll isshow:(BOOL)isShow{
- UIView * viewTag = [viewAll viewWithTag:tag];
- if (viewTag != nil) {
- viewTag.hidden = !isShow;
- }
- }
- //显示提示
- + (void)showAlert:(NSString *)strAlert{
- UIWindow * window = [UIApplication sharedApplication].keyWindow;
- [window endEditing:true];
- [[GlobalData sharedInstance].GB_NoticeView showNotice:strAlert time:1 frame:[UIScreen mainScreen].bounds viewShow:window ];
- }
- //获取版本号
- + (NSString *)getVersion{
- NSDictionary *infoDic = [[NSBundle mainBundle] infoDictionary];
- NSString *appVersion = [infoDic objectForKey:@"CFBundleShortVersionString"];
- return appVersion;
- }
- //获取APP名字
- + (NSString *)getAppName{
- NSDictionary *infoDic = [[NSBundle mainBundle] infoDictionary];
- NSString *appName = [infoDic objectForKey:@"CFBundleDisplayName"];
- return appName;
-
- }
- /**
- *获取设备型号
- */
- + (NSString *)LookDeviceName{
-
- struct utsname systemInfo;
-
- uname(&systemInfo);
-
- return [NSString stringWithCString:systemInfo.machine encoding:NSUTF8StringEncoding];
- }
- //适配label字号
- + (CGFloat)adaptLabelFont:(CGFloat)fontNum{
- if (isIphone5) {
- return fontNum - 1;
- } else if (isIphone6){
- return fontNum;
- } else if (isIphone6p){
- return fontNum + 1;
- }
- return fontNum + 2;
- }
- #pragma mark 根据推送的消息进行跳转
- + (void)jumpWithPushJson{
-
- }
- #pragma mark 验证HTML
- +(NSString *)removeHTML:(NSString *)html {
-
- NSScanner *theScanner;
-
- NSString *text = nil;
-
-
-
- theScanner = [NSScanner scannerWithString:html];
-
-
-
- while ([theScanner isAtEnd] == NO) {
-
- // find start of tag
-
- [theScanner scanUpToString:@"<" intoString:NULL] ;
-
-
-
- // find end of tag
-
- [theScanner scanUpToString:@">" intoString:&text] ;
-
-
-
- // replace the found tag with a space
-
- //(you can filter multi-spaces out later if you wish)
-
- html = [html stringByReplacingOccurrencesOfString:[NSString stringWithFormat:@"%@>", text] withString:@" "];
-
-
-
- }
-
- return html;
-
- }
- #pragma mark 弹出提示
- + (void)showNotiInStatusBar:(NSString *)strMsg msg:(NSString *)msg bageNum:(int)msgCount{
- UILocalNotification* noti = [[UILocalNotification alloc]init];
- NSDate* now = [NSDate date];
- noti.fireDate = [now dateByAddingTimeInterval:1];
- noti.timeZone = [NSTimeZone defaultTimeZone];
- noti.alertBody = strMsg;
- noti.alertTitle = msg;
- noti.soundName = UILocalNotificationDefaultSoundName;
- noti.alertAction = strMsg;
- noti.applicationIconBadgeNumber = msgCount;
- // [noti setUserInfo:[NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithInt:1], @"notikey", nil]];
- //NSLog(@"您有%d条消息需要处理",msgCount);
- [[UIApplication sharedApplication] scheduleLocalNotification:noti];
- }
- #pragma mark 拼写文件名
- + (NSString *)fetchDoumentPath:(NSString *)name{
- NSString *path = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:@"sld"];
-
- [[NSFileManager defaultManager] createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:nil];
-
- NSString *result = [path stringByAppendingPathComponent:name];
-
- return result;
- }
- + (NSString*)fetchDoumentSize{
- NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
- NSString *documentsDirectory = [paths objectAtIndex:0];
- NSString * strPath = [documentsDirectory stringByAppendingPathComponent:@"default"];
- NSFileManager *fileManager = [NSFileManager defaultManager];
- long long num = 0;
- if ([fileManager fileExistsAtPath:strPath]){
- num = [[fileManager attributesOfItemAtPath:strPath error:nil] fileSize];
- }
- return 0;
- // NSLog(@"%@",[NSString stringWithFormat:@"%l",num/(1024.0*1024.0)]);
- // return [NSString stringWithFormat:@"%l",num/(1024.0*1024.0)];
- }
- #pragma mark 判断推送状态
- //+ (BOOL)IsEnablePush{
- // if (isIOS8) {// system is iOS8
- // UIUserNotificationSettings *setting = [[UIApplication sharedApplication] currentUserNotificationSettings];
- // if (UIUserNotificationTypeNone != setting.types) {
- // return YES;
- // }
- // } else {//iOS7
- // UIRemoteNotificationType type = [[UIApplication sharedApplication] enabledRemoteNotificationTypes];
- // if(UIRemoteNotificationTypeNone != type)
- // return YES;
- // }
- // return NO;
- //}
- #pragma mark 判断网络状态
- + (BOOL)IsEnableNetwork{
- if ([[AFNetworkReachabilityManager sharedManager] networkReachabilityStatus] == AFNetworkReachabilityStatusNotReachable) {
- return NO;
- }
- return YES;
- }
- + (BOOL)IsEnableWifi{
- return ([[AFNetworkReachabilityManager sharedManager] networkReachabilityStatus] == AFNetworkReachabilityStatusReachableViaWiFi);
-
- }
- + (BOOL)IsENable3G{
- return ([[AFNetworkReachabilityManager sharedManager] networkReachabilityStatus] == AFNetworkReachabilityStatusReachableViaWWAN);
-
- }
- #pragma mark encode decode
- +(NSString*)encodeString:(NSString*)unencodedString{
-
- // CharactersToBeEscaped = @":/?&=;+!@#$()~',*";
-
- // CharactersToLeaveUnescaped = @"[].";
-
- NSString*encodedString=(NSString*)
-
- CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault,
-
- (CFStringRef)unencodedString,
-
- NULL,
-
- (CFStringRef)@"!*'();:@&=+$,/?%#[]",
-
- kCFStringEncodingUTF8));
-
- return encodedString;
-
- }
- //URLDEcode
- +(NSString*)decodeString:(NSString*)encodedString
- {
-
- //NSString *decodedString = [encodedString stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding ];
-
- NSString*decodedString=(__bridge_transfer NSString*)CFURLCreateStringByReplacingPercentEscapesUsingEncoding(NULL,
-
- (__bridge CFStringRef)encodedString,
-
- CFSTR(""),
-
- CFStringConvertNSStringEncodingToEncoding(NSUTF8StringEncoding));
-
- return decodedString;
-
- }
- #pragma mark获取某个字符串或者汉字的首字母.
- + (NSString *)fetchFirstCharactorWithString:(NSString *)string
- {
- if (!isStr(string)) {
- return @"";
- }
- NSMutableString *str = [NSMutableString stringWithString:string];
- CFStringTransform((CFMutableStringRef) str, NULL, kCFStringTransformMandarinLatin, NO);
- CFStringTransform((CFMutableStringRef)str, NULL, kCFStringTransformStripDiacritics, NO);
- NSString *pinYin = [str capitalizedString];
- return [pinYin substringToIndex:1];
- }
- #pragma mark 收键盘
- + (void)hideKeyboard{
- UIViewController * vc = GB_Nav.lastVC;
- [vc.view endEditing:true];
- }
- #pragma mark - 获取地图Bundle中turnIcon图片
- +(UIImage *)getImageFromAMapNaviBundle:(NSString *)imageName{
- NSURL *bundleURL = [[[NSBundle mainBundle] URLForResource:@"AMapNavi" withExtension:@"bundle"] URLByAppendingPathComponent:@"images"];
- NSBundle *bundle = [NSBundle bundleWithURL:bundleURL];
- UIImage *image = [UIImage imageNamed:imageName inBundle:bundle compatibleWithTraitCollection:nil];
-
- return image;
- }
- #pragma mark - 返回NSAttributedString并且带图片
- + (NSAttributedString *)returnNSAttributedStringWithContentStr:(NSString *)labelStr titleColor:(UIColor *)titleColor withlineSpacing:(CGFloat)lineSpacing withAlignment:(NSInteger)alignment withFont:(UIFont *)font withImageName:(NSString *)imageName withImageRect:(CGRect)rect withAtIndex:(NSUInteger)index
- {
- NSString * detailStr = labelStr;
-
- if (detailStr.length > 0 && ![detailStr isEqualToString:@"<null>"] && ![detailStr isEqualToString:@"(null)"])
- {
- NSMutableAttributedString * detailAttrString = [[NSMutableAttributedString alloc]initWithString:detailStr];
-
-
-
- NSMutableParagraphStyle * detailParagtaphStyle = [[NSMutableParagraphStyle alloc]init];
- detailParagtaphStyle.alignment = alignment; //设置两端对齐(3)
- NSDictionary * detaiDic = @{NSFontAttributeName : font,
- //NSKernAttributeName : [NSNumber numberWithInteger:W(0)],
- NSForegroundColorAttributeName:titleColor,
- NSParagraphStyleAttributeName : detailParagtaphStyle
- };
-
- [detailAttrString setAttributes:detaiDic range:NSMakeRange(0, detailAttrString.length)];
-
- //设置图片
- NSTextAttachment * attch = [[NSTextAttachment alloc] init];
- attch.image = [UIImage imageNamed:imageName];
- attch.bounds = rect;
- NSAttributedString * string1 = [NSAttributedString attributedStringWithAttachment:attch];
- [detailAttrString insertAttributedString:string1 atIndex:index];
-
- // detailAttrString.lineSpacing = lineSpacing;
-
- return detailAttrString;
- }
- else
- {
- return nil;
- }
- }
- //字符串数组转化为字符串
- +(id)parseJSONStringToNSDictionary:(NSString *)JSONString {
- if (JSONString==nil) {
- return nil;
- }
- NSData *JSONData = [JSONString dataUsingEncoding:NSUTF8StringEncoding];
-
- NSError *error = nil;
-
- id jsonObject = [NSJSONSerialization
-
- JSONObjectWithData:JSONData options:NSJSONReadingAllowFragments
-
- error:&error];
-
- if (jsonObject != nil && error == nil){
-
- // NSLog(@"反序列化成功...");
-
- if ([jsonObject isKindOfClass:[NSDictionary class]]){
-
- NSDictionary *deserializedDictionary = (NSDictionary *)jsonObject;
-
- // NSLog(@"反序列化后的dictionary数据 = %@", deserializedDictionary);
-
- return deserializedDictionary;
-
- }
-
- else if ([jsonObject isKindOfClass:[NSArray class]]){
-
- NSArray *deserializedArray = (NSArray *)jsonObject;
-
- // NSLog(@"反序列化json后的数组 = %@", deserializedArray);
-
- return deserializedArray;
-
- }else {
-
- return nil;
-
- }
-
- }else{
-
- NSLog(@"%@", error);
-
- NSLog(@"反序列化时发生一个错误");
-
- return JSONString;
-
- }
-
- }
- //获取所在vc
- + (UIViewController *)fetchVCWithView:(UIView *)view{
- for (UIView* next = view ; next; next = next.superview) {
- UIResponder* nextResponder = [next nextResponder];
- if ([nextResponder isKindOfClass:[UIViewController class]]) {
- return (UIViewController*)nextResponder;
- }
- }
- return nil;
- }
- @end
|