-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathFileDelete.m
More file actions
163 lines (120 loc) · 5.14 KB
/
FileDelete.m
File metadata and controls
163 lines (120 loc) · 5.14 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
//
// FileDelete.m
// TableView
//
// Created by Anthony Levings on 11/03/2014.
// Copyright (c) 2014 Gylphi. All rights reserved. You are free to use this code but please acknowledge copyright owner.
//
#import "FileDelete.h"
@implementation FileDelete
+ (NSURL *)applicationDocumentsDirectory {
return [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory
inDomains:NSUserDomainMask] lastObject];
}
+ (NSURL *)applicationLibraryDirectory {
return [[[NSFileManager defaultManager] URLsForDirectory:NSLibraryDirectory
inDomains:NSUserDomainMask] lastObject];
}
+(NSString *)stripSlashIfNeeded:(NSString *)stringWithPossibleSlash {
// If the file name contains a slash at the beginning then we remove so that we don't end up with two
if ([stringWithPossibleSlash compare:@"/" options:NSLiteralSearch range:NSMakeRange(0, 1)]==NSOrderedSame) {
stringWithPossibleSlash = [stringWithPossibleSlash substringFromIndex:1];
}
// Return the string with no slash at the beginning
return stringWithPossibleSlash;
}
+(BOOL)deleteFileFromDocumentsDirectory:(NSString *)path inSubDirectory:(NSString *)subdirectory
{
// Remove unnecessary slash if need
path = [self stripSlashIfNeeded:path];
subdirectory = [self stripSlashIfNeeded:subdirectory];
// Create generic beginning to file save path
NSMutableString *deletePath = [[NSMutableString alloc] initWithFormat:@"%@/",[self applicationDocumentsDirectory].path];
if (subdirectory){
[deletePath appendString:subdirectory];
// Check whether the specified subdirectory exists
BOOL dir;
[[NSFileManager defaultManager] fileExistsAtPath:deletePath isDirectory:&dir];
if (dir==NO) {
return dir;
}
[deletePath appendString:@"/"];
}
// Add requested save path
[deletePath appendString:path];
// Save the file and see if it was successful
NSError *error;
BOOL ok = [[NSFileManager defaultManager] removeItemAtPath:[deletePath copy] error:&error];
if (error) NSLog(@"%@", error);
// Return status of file save
return ok;
}
+(BOOL)deleteFileFromLibraryDirectory:(NSString *)path inSubDirectory:(NSString *)subdirectory
{
// Remove unnecessary slash if need
path = [self stripSlashIfNeeded:path];
subdirectory = [self stripSlashIfNeeded:subdirectory];
// Create generic beginning to file save path
NSMutableString *deletePath = [[NSMutableString alloc] initWithFormat:@"%@/",[self applicationLibraryDirectory].path];
if (subdirectory){
[deletePath appendString:subdirectory];
// Check whether the specified subdirectory exists
BOOL dir;
[[NSFileManager defaultManager] fileExistsAtPath:deletePath isDirectory:&dir];
if (dir==NO) {
return dir;
}
[deletePath appendString:@"/"];
}
// Add requested save path
[deletePath appendString:path];
// Save the file and see if it was successful
NSError *error;
BOOL ok = [[NSFileManager defaultManager] removeItemAtPath:[deletePath copy] error:&error];
if (error) NSLog(@"%@", error);
// Return status of file save
return ok;
}
+(BOOL)deleteSubDirectoryFromDocumentsDirectory:(NSString *)subdirectory {
// Remove unnecessary slash if need
subdirectory = [self stripSlashIfNeeded:subdirectory];
// Create generic beginning to file save path
NSMutableString *deletePath = [[NSMutableString alloc] initWithFormat:@"%@/",[self applicationDocumentsDirectory].path];
if (subdirectory){
[deletePath appendString:subdirectory];
// Check whether the specified subdirectory exists
BOOL dir;
[[NSFileManager defaultManager] fileExistsAtPath:deletePath isDirectory:&dir];
if (dir==NO) {
return dir;
}
}
NSError *error;
// Save the file and see if it was successful
BOOL ok = [[NSFileManager defaultManager] removeItemAtPath:deletePath error:&error];
if (error) NSLog(@"%@", error);
// Return status of file save
return ok;
}
+(BOOL)deleteSubDirectoryFromLibraryDirectory:(NSString *)subdirectory {
// Remove unnecessary slash if need
subdirectory = [self stripSlashIfNeeded:subdirectory];
// Create generic beginning to file save path
NSMutableString *deletePath = [[NSMutableString alloc] initWithFormat:@"%@/",[self applicationLibraryDirectory].path];
if (subdirectory){
[deletePath appendString:subdirectory];
// Check whether the specified subdirectory exists
BOOL dir;
[[NSFileManager defaultManager] fileExistsAtPath:deletePath isDirectory:&dir];
if (dir==NO) {
return dir;
}
}
NSError *error;
// Save the file and see if it was successful
BOOL ok = [[NSFileManager defaultManager] removeItemAtPath:deletePath error:&error];
if (error) NSLog(@"%@", error);
// Return status of file save
return ok;
}
@end