-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathUtils.m
122 lines (105 loc) · 4.21 KB
/
Utils.m
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
//
// Tool.m
// OmniSDKDemo
//
// Created by 程小康 on 2023/3/3.
//
#import "Utils.h"
#include <CommonCrypto/CommonHMAC.h>
#define kOmniSDKRegion @"OmniSDKRegion"
#define kPassportChannelName @"kspassport"
#define kOverseaChannelName @"oversea"
#define kSeayooChannelName @"seayoo"
@implementation Utils
+ (NSString *)convertDictToJsonString:(NSDictionary *)dict{
NSData *data = [NSJSONSerialization dataWithJSONObject:dict options:NSJSONWritingFragmentsAllowed error:nil];
NSString *str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
return str;
}
+ (NSDictionary *)convertJsonStringToDict:(NSString *)json{
NSData *data = [json dataUsingEncoding:NSUTF8StringEncoding];
NSError *error;
return [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&error];
}
+ (void)showAlertWithContrller:(UIViewController *)vc msg:(NSString *)msg{
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:msg preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *ok = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil];
[alert addAction:ok];
dispatch_async(dispatch_get_main_queue(), ^{
[vc presentViewController:alert animated:YES completion:nil];
});
}
+ (NSString*)getCurrentTimes{
NSDate* dat = [NSDate dateWithTimeIntervalSinceNow:0];
NSTimeInterval a=[dat timeIntervalSince1970];
NSString*timeString = [NSString stringWithFormat:@"%0.f", a];//转为字符型
return timeString;
}
+ (NSDictionary *)dictFromJsonFile:(NSString *)fileName{
NSString *urlStr = [[NSBundle mainBundle] pathForResource:fileName ofType:nil];
NSURL *url = [[NSURL alloc] initFileURLWithPath:urlStr];
NSData *data = [NSData dataWithContentsOfURL:url];
NSError *error;
return [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingFragmentsAllowed error:&error];
}
+ (NSString *)signWithParams:(NSDictionary *)params key: (NSString *)key
{
NSMutableString *signStr = [[NSMutableString alloc] init]; //签名串
Boolean isFirst = YES;
NSArray *sortedArray = [[params allKeys] sortedArrayUsingSelector:@selector(compare:)];
for (id key in sortedArray) {
id value = [params objectForKey:key];
if (value == nil) {
continue;
}
if (isFirst) {
[signStr appendFormat:@"%@=%@", key, value];
isFirst = NO;
} else {
[signStr appendFormat:@"&%@=%@", key, value];
}
}
return [self hmacSha1: key text:signStr];
}
// Sign
+ (NSString *)hmacSha1:(NSString *)key text:(NSString *)text
{
const char *cKey = [key cStringUsingEncoding:NSUTF8StringEncoding];
const char *cData = [text cStringUsingEncoding:NSUTF8StringEncoding];
uint8_t cHMAC[CC_SHA1_DIGEST_LENGTH];
CCHmac(kCCHmacAlgSHA1, cKey, strlen(cKey), cData, strlen(cData), cHMAC);
NSString *hash;
NSMutableString *output = [NSMutableString stringWithCapacity:CC_SHA1_DIGEST_LENGTH * 2];
for (int i = 0; i < CC_SHA1_DIGEST_LENGTH; i++) {
[output appendFormat:@"%02x", cHMAC[i]];
}
hash = output;
return hash;
}
+ (void)removeCache {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *cachePath = [paths firstObject];
NSString *filePath = [cachePath stringByAppendingPathComponent:@"com.seayoo.omnisdk"];
NSError *error = nil;
[[NSFileManager defaultManager] removeItemAtPath:filePath error:&error];
if (error) {
NSLog(@"删除缓存失败: %@", error.localizedDescription);
}
}
+ (Boolean)isLandScape{
UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
return UIInterfaceOrientationIsLandscape(orientation);
}
+ (ChannelType)getChannelType{
NSString *channel = [[NSBundle mainBundle].infoDictionary objectForKey:@"OmniSDKChannel"];
if ([channel isEqualToString:kPassportChannelName]) {
return Passport;
} else if ([channel isEqualToString:kOverseaChannelName]) {
return Oversea;
} else if ([channel isEqualToString:kSeayooChannelName]){
return Seayoo;
} else {
return Passport;
}
}
@end