CommonFun.m
5.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
//
// CommonFun.m
// B2CMall
//
// Created by 钱鋆 on 2019/4/8.
// Copyright © 2019 Facebook. All rights reserved.
//
#import "CommonFun.h"
// CC_MD5
#import <CommonCrypto/CommonDigest.h>
#import <netdb.h>
#include <arpa/inet.h>
@implementation CommonFun
+ (instancetype)sharedInstance {
static id sharedInstance = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sharedInstance = [[self alloc] init];
});
return sharedInstance;
}
- (NSString *)f_toJSONString:(id)theData
{
NSString *_str_json = nil;
if (theData) {
if ([theData isKindOfClass:[NSDictionary class]] || [theData isKindOfClass:[NSMutableDictionary class]] || [theData isKindOfClass:[NSArray class]] || [theData isKindOfClass:[NSMutableArray class]]) {
NSData *_data_json = [NSJSONSerialization dataWithJSONObject:theData
options:NSJSONWritingPrettyPrinted
error:nil];
if (_data_json) {
_str_json = [[NSString alloc] initWithData:_data_json
encoding:NSUTF8StringEncoding];
}
}
}
return _str_json;
}
- (NSDictionary *)f_jsonStringToDic:(NSString *)jsonString {
if (jsonString == nil) {
return nil;
}
NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
NSError *err;
NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:jsonData
options:NSJSONReadingMutableContainers
error:&err];
if(err) {
NSLog(@"json解析失败:%@",err);
return nil;
}
return dic;
}
- (NSMutableDictionary *)f_jsonDataToDic:(NSData *)jsonData option:(NSJSONReadingOptions)option {
NSError *_err_temp;
if (jsonData) {
NSMutableDictionary *_muDic_json = [NSJSONSerialization
JSONObjectWithData:jsonData
options:option error:&_err_temp];
if (_muDic_json && _muDic_json.count > 0) {
return _muDic_json;
}
}
return nil;
}
#pragma mark - type 1:空白 2:换行 默认 空白+换行
+(NSString *)deleteWhitespaceNewline:(NSString *)str type:(int)type
{
switch (type) {
case 1:
return [str stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
break;
case 2:
return [str stringByTrimmingCharactersInSet:[NSCharacterSet newlineCharacterSet]];
break;
default:
return [str stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
break;
}
}
#pragma mark - string是否为空(null也算)
+ (BOOL) isBlankOrNullString:(NSString *)string {
if (string == nil || string == NULL) {
return YES;
}
if ([string isKindOfClass:[NSNull class]]) {
return YES;
}
if ([[string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] length]==0) {
return YES;
}
return NO;
}
#pragma mark - dic/array转为jsonString
/**
* @author qianjun
*
* @brief dic/array转为jsonString
*
* @param theData dic/array
*
* @return jsonString
*/
- (NSString *)event_toJSONString:(id)theData
{
if(theData)
{
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:theData
options:NSJSONWritingPrettyPrinted
error:nil];
NSString *jsonString = [[NSString alloc] initWithData:jsonData
encoding:NSUTF8StringEncoding];
return jsonString;
}
return nil;
}
#pragma mark - 当前视图
- (UIViewController *)topViewController {
UIViewController *resultVC;
resultVC = [self _topViewController:[[UIApplication sharedApplication].keyWindow rootViewController]];
if ([resultVC isKindOfClass:[UIViewController class]]) {
return resultVC;
}
while (resultVC.presentedViewController) {
// 如果是UIAlertController忽略掉
if([[self _topViewController:resultVC.presentedViewController] isKindOfClass:[UIAlertController class]]){
break;
}else{
resultVC = [self _topViewController:resultVC.presentedViewController];
}
}
return resultVC;
}
- (UIViewController *)_topViewController:(UIViewController *)vc {
if ([vc isKindOfClass:[UINavigationController class]]) {
return [self _topViewController:[(UINavigationController *)vc topViewController]];
} else if ([vc isKindOfClass:[UITabBarController class]]) {
return [self _topViewController:[(UITabBarController *)vc selectedViewController]];
} else {
return vc;
}
return nil;
}
#pragma mark - MD5 32位加密
+(NSString *)md5_32:(NSString *)str bool_isUpper:(BOOL)bool_isUpper
{
const char *cStr = [str UTF8String];
unsigned char result[32];
CC_MD5(cStr, (CC_LONG)strlen(cStr), result);
NSMutableString *hash = [NSMutableString string];
for (int i = 0; i < 16; i++)
{
[hash appendFormat:@"%02X", result[i]];
}
if (bool_isUpper) {
return [hash uppercaseString];
}else{
return [hash lowercaseString];
}
}
#pragma mark -获取字符串形式的当前时间(默认yyyy-MM-dd HH:mm:ss)
+(NSString *)getDateTimeString:(NSDateFormatter *)dateFormatter
{
if (dateFormatter == nil) {
dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss.SSS"];
}
NSDate *dateNow = [NSDate date];
return [dateFormatter stringFromDate:dateNow];
}
@end