CNLiveBusinessTools.m
21.1 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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
//
// CNLiveBusinessTools.m
// CNLiveBusinessTools
//
// Created by open on 2019/5/8.
//
#import "CNLiveBusinessTools.h"
#import <Photos/Photos.h>
#import "CNLiveEnvironment.h"
#import <CNLiveUserManagement/CNUserInfoManager.h>
#import "CommonCrypto/CommonDigest.h"
#import <CNLiveBaseKit/CNLiveBaseKit.h>
@implementation CNLiveBusinessTools
+ (NSString *)cnStatWithType:(CNStatType)statType type:(NSString *)type userId:(NSString *)userId programId:(NSString *)programId title:(NSString *)title {
NSString *statStr = @"";
switch (statType) {
case CNStatTypeContentCount:
statStr = @"count";
break;
case CNStatTypeContentDuration:
statStr = @"duration";
break;
case CNStatTypeShareCount:
statStr = @"share";
break;
default:
break;
}
NSString *channelTypeStr = type?type:@"";
NSString *uIdStr = userId?userId:@"";
NSString *proIdStr = programId?programId:@"";
NSString *titleStr = title?title:@"";
NSString *eventStr = [NSString stringWithFormat:@"type__%@-channel__%@-sid__%@-uid__%@-pid__%@-title__%@-data_",statStr,channelTypeStr,uIdStr,[self getUidForStat:[NSDate date]],proIdStr,titleStr];
NSLog(@"event:%@",eventStr);
return eventStr;
}
+ (NSString *)getUidForStat:(NSDate *)date {
NSString *uid = @"";
NSString *uidKey = @"kCNLiveUserDefaultsUIDKey";
NSUserDefaults *defauts = [NSUserDefaults standardUserDefaults];
if ([defauts objectForKey:uidKey]) {
uid = [[defauts objectForKey:uidKey] copy];
return uid;
}
else {
NSInteger time = [[NSDate date] timeIntervalSince1970];
NSString *timeString = [NSString stringWithFormat:@"%ld", (long)time];
NSString *stamp = timeString;
stamp = [stamp stringByReplacingOccurrencesOfString:@"." withString:@""];
NSString *idfv = [[[[UIDevice currentDevice] identifierForVendor] UUIDString] stringByReplacingOccurrencesOfString:@"-" withString:@""];
uid = [NSString stringWithFormat:@"%@_%@", idfv, stamp];
[defauts setObject:uid forKey:uidKey];
[defauts synchronize];
return uid;
}
}
+ (NSString *)uuid{
NSString *uid = @"";
NSString *uidKey = @"kCNLiveUserDefaultsUIDKey";
NSUserDefaults *defauts = [NSUserDefaults standardUserDefaults];
if ([defauts objectForKey:uidKey]) {
uid = [[defauts objectForKey:uidKey] copy];
return uid;
} else {
NSString *idfv = [[[[UIDevice currentDevice] identifierForVendor] UUIDString] stringByReplacingOccurrencesOfString:@"-" withString:@""];
[defauts setObject:idfv forKey:uidKey];
[defauts synchronize];
return uid;
}
}
+ (NSString *)tingyunUUID{
NSString *uid = @"";
NSUserDefaults *defauts = [NSUserDefaults standardUserDefaults];
if ([defauts objectForKey:@"kCNLiveUserDefaultsTingYunUIDKey"]) {
uid = [[defauts objectForKey:@"kCNLiveUserDefaultsTingYunUIDKey"] copy];
return uid;
} else {
NSString *idfv = [[[[UIDevice currentDevice] identifierForVendor] UUIDString] stringByReplacingOccurrencesOfString:@"-" withString:@""];
[defauts setObject:idfv forKey:@"kCNLiveUserDefaultsTingYunUIDKey"];
[defauts synchronize];
}
return uid;
}
+ (NSString *)cnStatWithType:(CNStatType)statType type:(NSString *)type userId:(NSString *)userId programId:(NSString *)programId title:(NSString *)title shareToType:(NSString *)shareToType
{
NSString *statStr = @"";
switch (statType) {
case CNStatTypeContentCount:
statStr = @"count";
break;
case CNStatTypeContentDuration:
statStr = @"duration";
break;
case CNStatTypeShareCount:
statStr = @"share";
break;
default:
break;
}
NSString *channelTypeStr = type?type:@"";
NSString *uIdStr = userId?userId:@"";
NSString *proIdStr = programId?programId:@"";
NSString *titleStr = title?title:@"";
NSString *shareToTypeStr = shareToType?shareToType:@"";
NSString *eventStr = [NSString stringWithFormat:@"type__%@-channel__%@-sid__%@-uid__%@-pid__%@-title__%@-to__%@-data_",statStr,channelTypeStr,uIdStr,[self getUidForStat:[NSDate date]],proIdStr,titleStr,shareToTypeStr];
NSLog(@"event:%@",eventStr);
return eventStr;
}
+ (BOOL)isGif:(id)asset {
if ([asset isKindOfClass:[PHAsset class]]) {
PHAsset *phAsset = (PHAsset *)asset;
if (phAsset.mediaType == PHAssetMediaTypeImage) {
// Gif
if ([[phAsset valueForKey:@"filename"] hasSuffix:@"GIF"]) {
return YES;
}
}
}
return NO;
}
+ (BOOL)isGifWithImageData: (NSData *)data {
if ([[self contentTypeWithImageData:data] isEqualToString:@"gif"]) {
return YES;
}
return NO;
}
+ (NSString *)contentTypeWithImageData: (NSData *)data {
uint8_t c;
[data getBytes:&c length:1];
switch (c) {
case 0xFF:
return @"jpeg";
case 0x89:
return @"png";
case 0x47:
return @"gif";
case 0x49:
case 0x4D:
return @"tiff";
case 0x52:
if ([data length] < 12) {
return nil;
}
NSString *testString = [[NSString alloc] initWithData:[data subdataWithRange:NSMakeRange(0, 12)] encoding:NSASCIIStringEncoding];
if ([testString hasPrefix:@"RIFF"] && [testString hasSuffix:@"WEBP"]) {
return @"webp";
}
return nil;
}
return nil;
}
+ (NSDictionary *)fileTypeDic
{
NSDictionary *dic =
@{
@".3gp" : @"video/3gpp",
@".apk" : @"application/vnd.android.package-archive",
@".asf" : @"video/x-ms-asf",
@".avi" : @"video/x-msvideo",
@".bin" : @"application/octet-stream",
@".bmp" : @"image/bmp",
@".c" : @"text/plain",
@".class" : @"application/octet-stream",
@".conf" : @"text/plain",
@".cpp" : @"text/plain",
@".doc" : @"application/msword",
@".docx" : @"application/vnd.openxmlformats-officedocument.wordprocessingml.document",
@".xls" : @"application/vnd.ms-excel",
@".xlsx" : @"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
@".exe" : @"application/octet-stream",
@".gif" : @"image/gif",
@".gtar" : @"application/x-gtar",
@".gz" : @"application/x-gzip",
@".h" : @"text/plain",
@".htm" : @"text/html",
@".html" : @"text/html",
@".jar" : @"application/java-archive",
@".java" : @"text/plain",
@".jpeg" : @"image/jpeg",
@".jpg" : @"image/jpeg",
@".js" : @"application/x-javascript",
@".log" : @"text/plain",
@".m3u" : @"audio/x-mpegurl",
@".m4a" : @"audio/mp4a-latm",
@".m4b" : @"audio/mp4a-latm",
@".m4p" : @"audio/mp4a-latm",
@".m4u" : @"video/vnd.mpegurl",
@".m4v" : @"video/x-m4v",
@".mov" : @"video/quicktime",
@".mp2" : @"audio/x-mpeg",
@".mp3" : @"audio/x-mpeg",
@".mp4" : @"video/mp4",
@".mpc" : @"application/vnd.mpohun.certificate",
@".mpe" : @"video/mpeg",
@".mpeg" : @"video/mpeg",
@".mpg" : @"video/mpeg",
@".mpg4" : @"video/mp4",
@".mpga" : @"audio/mpeg",
@".msg" : @"application/vnd.ms-outlook",
@".ogg" : @"audio/ogg",
@".pdf" : @"application/pdf",
@".png" : @"image/png",
@".pps" : @"application/vnd.ms-powerpoint",
@".ppt" : @"application/vnd.ms-powerpoint",
@".pptx" : @"application/vnd.openxmlformats-officedocument.presentationml.presentation",
@".prop" : @"text/plain",
@".rc" : @"text/plain",
@".rmvb" : @"audio/x-pn-realaudio",
@".rtf" : @"application/rtf",
@".sh" : @"text/plain",
@".tar" : @"application/x-tar",
@".tgz" : @"application/x-compressed",
@".txt" : @"text/plain",
@".wav" : @"audio/x-wav",
@".wma" : @"audio/x-ms-wma",
@".wmv" : @"audio/x-ms-wmv",
@".wps" : @"application/vnd.ms-works",
@".xml" : @"text/plain",
@".z" : @"application/x-compress",
@".h" : @"text/h-text",
@".m" : @"text/m-text",
@".json": @"text/json-text",
@".key" : @"application/vnd.ms-powerpoint",
@".numbers" : @"application/vnd.ms-excel",
@".pages" : @"application/vnd.openxmlformats-officedocument.wordprocessingml.document"
};
return dic;
}
+ (CNFileType)isReadedFileByType:(NSString *)suffixType
{
if ([[CNLiveBusinessTools fileTypeDic] objectForKey:[NSString stringWithFormat:@".%@",suffixType]]) {
return CNFileTypeReadAndLook;
}
else if ([[CNLiveBusinessTools onlyReadFileType] objectForKey:[NSString stringWithFormat:@".%@",suffixType]])
{
return CNFileTypeOnlyRead;
}
else
return CNFileTypeUnkown;
}
// 文件知道其类型,但是无法预览
+ (NSDictionary *)onlyReadFileType
{
NSDictionary *dic = @{
@".zip" : @"application/x-zip-compressed",
};
return dic;
}
+ (NSString *)getSpid
{
NSString *spId = @"";
NSArray *array = [AppId componentsSeparatedByString:@"_"];
spId = [array firstObject];
return spId;
}
+ (NSString *)getSidIdfv
{
NSString *idfv = [self tingyunUUID];
NSString *uid = @"";
if ([CNUserShareManager isLogin]) {
uid = CNUserShareModel.uid;
}
NSString *sidIdfv = [NSString stringWithFormat:@"%@%@",uid,idfv];
// NSLog(@"sidIdfv:%@",sidIdfv);
return sidIdfv;
}
+ (NSMutableDictionary *)encryptionSignWithParameter:(NSMutableDictionary *)parameter encryptionKey:(NSString *)encryptionKey
{
NSMutableDictionary *muDic = [NSMutableDictionary dictionaryWithDictionary:parameter];
NSArray *allKeys = [muDic allKeys];
BOOL isContainAppId = NO;
BOOL isContainPlat = NO;
BOOL isPlatform_id = NO;
BOOL isTimestamp = NO;
BOOL isVer = NO;
BOOL isUUID = NO;
BOOL isSign = NO;
BOOL isLoginId = NO;
for (NSString *key in allKeys) {
if ([key isEqualToString:@"appId"]){
isContainAppId = YES;
}else if ([key isEqualToString:@"plat"]) {
isContainPlat = YES;
}else if ([key isEqualToString:@"platform_id"]) {
isPlatform_id = YES;
}else if ([key isEqualToString:@"timestamp"]) {
isTimestamp = YES;
}else if ([key isEqualToString:@"ver"]) {
isVer = YES;
}else if ([key isEqualToString:@"uuid"]) {
isUUID = YES;
}else if ([key isEqualToString:@"sign"]){
isSign = YES;
}else if ([key isEqualToString:@"loginSid"]) {
isLoginId = YES;
}
if (isContainAppId && isContainPlat && isPlatform_id && isTimestamp && isVer && isUUID && isLoginId) {
break;
}
}
if (!isContainAppId) {
[muDic setObject:AppId forKey:@"appId"];
}
if (!isContainPlat) {
[muDic setObject:@"i" forKey:@"plat"];
}
if (!isPlatform_id) {
[muDic setObject:[self getBundleID] forKey:@"platform_id"];
}
if (!isTimestamp) {
[muDic setObject:[NSString stringWithFormat:@"%zd",[self getNowTimestamp]] forKey:@"timestamp"];
}
if (!isVer) {
[muDic setObject:AppVersion forKey:@"ver"];
}
if (!isUUID) {
[muDic setObject:[CNLiveBusinessTools uuid] forKey:@"uuid"];
}
if (!isLoginId) {
[muDic setObject:CNUserShareModel.uid.length == 0 ? @"" : CNUserShareModel.uid forKey:@"loginSid"];
}
NSMutableDictionary *startPara = [NSMutableDictionary dictionaryWithDictionary:muDic];
if (!isSign) {
NSString *string = [NSString stringWithFormat:@"%@&key=%@",[self signvalue:muDic],encryptionKey];
NSString *signString = [[self sha1:string] uppercaseString];
[startPara setObject:signString forKey:@"sign"];
}
return startPara;
}
+ (NSInteger)getNowTimestamp{
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateStyle:NSDateFormatterMediumStyle];
[formatter setTimeStyle:NSDateFormatterShortStyle];
[formatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"]; // ----------设置你想要的格式,hh与HH的区别:分别表示12小时制,24小时制
//设置时区,这个对于时间的处理有时很重要
NSTimeZone* timeZone = [NSTimeZone timeZoneWithName:@"Asia/Beijing"];
[formatter setTimeZone:timeZone];
NSDate *datenow = [NSDate date];//现在时间
// NSLog(@"设备当前的时间:%@",[formatter stringFromDate:datenow]);
//时间转时间戳的方法:
NSInteger timeSp = [[NSNumber numberWithDouble:[datenow timeIntervalSince1970]] integerValue];
// NSLog(@"设备当前的10位时间戳:%ld",(long)timeSp); //时间戳的值
return timeSp;
}
+ (NSString *)signvalue:(NSDictionary*)parameter
{
//对所有传入参数按照字段名的 ASCII 码从小到大排序
NSArray *keyArr=[parameter allKeys];
NSArray *arr = [keyArr sortedArrayUsingSelector:@selector(compare:)];
NSMutableString *string1 = [[NSMutableString alloc]init];
for (int i=0; i<arr.count; i++) {
NSString *parameterString = parameter[[arr objectAtIndex:i]];
if (parameterString.length > 0) {
[string1 appendString:[NSString stringWithFormat:@"%@=%@&",[arr objectAtIndex:i],parameter[[arr objectAtIndex:i]]]];
}
}
if (string1.length > 0) {
[string1 deleteCharactersInRange:NSMakeRange(string1.length-1, 1)];
}
return string1;
}
+ (NSString*)sha1:(NSString *)string
{
NSData *data = [string dataUsingEncoding:NSUTF8StringEncoding];
uint8_t digest[CC_SHA1_DIGEST_LENGTH];
CC_SHA1(data.bytes, (int)data.length, digest);
NSMutableString* output = [NSMutableString stringWithCapacity:CC_SHA1_DIGEST_LENGTH * 2];
for(int i = 0; i < CC_SHA1_DIGEST_LENGTH; i++)
[output appendFormat:@"%02x", digest[i]];
while ([[output substringToIndex:1] isEqualToString:@"0"]) {
output = [[NSMutableString alloc] initWithString:[output substringFromIndex:1]];
}
return output;
}
//获取BundleID
+ (NSString*)getBundleID
{
return [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleIdentifier"];
}
/**
GIF表情存放位置
@return 地址
*/
+ (NSString *)newChatGifFacePath{
// NSHomeDirectory():应用程序目录, 在Documents文件夹下创建
NSString *tempName = [NSString stringWithFormat:@"Documents/%@/GifFace/Cache", CNUserShareModel.uid];
NSString *filePath = [NSHomeDirectory() stringByAppendingPathComponent:tempName];
NSFileManager *fileManager = [NSFileManager defaultManager];
BOOL isDir = NO;
// fileExistsAtPath 判断一个文件或目录是否有效,isDirectory判断是否一个目录
BOOL existed = [fileManager fileExistsAtPath:filePath isDirectory:&isDir];
if ( !(isDir == YES && existed == YES) ) {
// 在 tmp 目录下创建一个 temp 目录
[fileManager createDirectoryAtPath:filePath withIntermediateDirectories:YES attributes:nil error:nil];
}
return filePath;
}
/**
@param userId 单聊为对方ID,群聊为群聊 ID,图片和视频存放位置
@return 地址
*/
+ (NSString *)newChatFilePathByUserId:(NSString *)userId{
// NSHomeDirectory():应用程序目录, 在Documents文件夹下创建
NSString *tempName = [NSString stringWithFormat:@"Documents/%@/%@/imageorvideo", CNUserShareModel.uid, userId];
NSString *filePath = [NSHomeDirectory() stringByAppendingPathComponent:tempName];
NSFileManager *fileManager = [NSFileManager defaultManager];
BOOL isDir = NO;
// fileExistsAtPath 判断一个文件或目录是否有效,isDirectory判断是否一个目录
BOOL existed = [fileManager fileExistsAtPath:filePath isDirectory:&isDir];
if ( !(isDir == YES && existed == YES) ) {
// 在 tmp 目录下创建一个 temp 目录
[fileManager createDirectoryAtPath:filePath withIntermediateDirectories:YES attributes:nil error:nil];
}
return filePath;
}
+ (nullable NSString *)chatFileWithCachePathByUserId:(NSString *_Nonnull)userId{
// NSHomeDirectory():应用程序目录, @"tmp/temp":在tmp文件夹下创建temp 文件夹
// NSString *tempName = [NSString stringWithFormat:@"cache/%@/%@", CNUserShareModel.uid, userId];
NSString *filePath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory,NSUserDomainMask, YES) objectAtIndex:0]; //stringByAppendingPathComponent:tempName];
NSFileManager *fileManager = [NSFileManager defaultManager];
BOOL isDir = NO;
// fileExistsAtPath 判断一个文件或目录是否有效,isDirectory判断是否一个目录
BOOL existed = [fileManager fileExistsAtPath:filePath isDirectory:&isDir];
if ( !(isDir == YES && existed == YES) ) {
// 在 tmp 目录下创建一个 temp 目录
[fileManager createDirectoryAtPath:filePath withIntermediateDirectories:YES attributes:nil error:nil];
}
return filePath;
}
//用户ID
+ (nullable NSString *)chatFilePathByUserId:(NSString *_Nonnull)userId{
// NSHomeDirectory():应用程序目录, @"tmp/temp":在tmp文件夹下创建temp 文件夹
NSString *tempName = [NSString stringWithFormat:@"%@/",userId];
NSString *filePath = [NSTemporaryDirectory() stringByAppendingPathComponent:tempName];
NSFileManager *fileManager = [NSFileManager defaultManager];
BOOL isDir = NO;
// fileExistsAtPath 判断一个文件或目录是否有效,isDirectory判断是否一个目录
BOOL existed = [fileManager fileExistsAtPath:filePath isDirectory:&isDir];
if ( !(isDir == YES && existed == YES) ) {
// 在 temp 目录下创建一临时文件
[fileManager createDirectoryAtPath:filePath withIntermediateDirectories:YES attributes:nil error:nil];
}
return filePath;
}
/**
@param userId 单聊为对方ID,群聊为群聊 ID
@return 返回聊天发送的文件 所保存的文件夹地址
*/
+ (NSString *)getChatFilesPathByUserId:(NSString *)userId{
NSString *tempName = [NSString stringWithFormat:@"Documents/%@/%@/file", CNUserShareModel.uid, userId];
NSString *filePath = [NSHomeDirectory() stringByAppendingPathComponent:tempName];
NSFileManager *fileManager = [NSFileManager defaultManager];
BOOL isDir = NO;
// fileExistsAtPath 判断一个文件或目录是否有效,isDirectory判断是否一个目录
BOOL existed = [fileManager fileExistsAtPath:filePath isDirectory:&isDir];
if ( !(isDir == YES && existed == YES) ) {
// 在 Documents 目录下创建
if ([fileManager createDirectoryAtPath:filePath withIntermediateDirectories:YES attributes:nil error:nil]) {
NSLog(@"创建聊天文件路径成功");
}
else
{
NSLog(@"聊天文件路径已经创建过");
}
}
return filePath;
}
/**
0
@param userId 单聊为对方ID,群聊为群聊 ID ,语音文件保存路径
*/
+ (NSString *)audioChatFilePathByUserId:(NSString *)userId{
NSString *tempName = [NSString stringWithFormat:@"Documents/%@/%@/audio", CNUserShareModel.uid, userId];
NSString *filePath = [NSHomeDirectory() stringByAppendingPathComponent:tempName];
NSFileManager *fileManager = [NSFileManager defaultManager];
BOOL isDir = NO;
// fileExistsAtPath 判断一个文件或目录是否有效,isDirectory判断是否一个目录
BOOL existed = [fileManager fileExistsAtPath:filePath isDirectory:&isDir];
if ( !(isDir == YES && existed == YES) ) {
// 在 Documents 目录下创建
if ([fileManager createDirectoryAtPath:filePath withIntermediateDirectories:YES attributes:nil error:nil]) {
NSLog(@"创建语音文件成功");
}
else
{
NSLog(@"语音文件已经存在");
}
}
return filePath;
}
/**
聊天背景图图片地址
@return file
*/
+ (NSString *)chatBackgroupImagefilePath{
// NSHomeDirectory():应用程序目录, 在Documents文件夹下创建
NSString *tempName = [NSString stringWithFormat:@"Documents/%@/chatBackgroupImage", CNUserShareModel.uid];
NSString *filePath = [NSHomeDirectory() stringByAppendingPathComponent:tempName];
NSFileManager *fileManager = [NSFileManager defaultManager];
BOOL isDir = NO;
// fileExistsAtPath 判断一个文件或目录是否有效,isDirectory判断是否一个目录
BOOL existed = [fileManager fileExistsAtPath:filePath isDirectory:&isDir];
if ( !(isDir == YES && existed == YES) ) {
// 在 tmp 目录下创建一个 temp 目录
[fileManager createDirectoryAtPath:filePath withIntermediateDirectories:YES attributes:nil error:nil];
}
return filePath;
}
@end