-
Notifications
You must be signed in to change notification settings - Fork 0
/
iClodeManager.swift
271 lines (231 loc) · 12 KB
/
iClodeManager.swift
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
//
// iClodeManager.swift
// iClodeDemo
//
// Created by mac-00021 on 15/06/21.
// Developer Name: Parth Gohel
import Foundation
class CloudDataManager {
static let shared = CloudDataManager() // Singleton
let fileManager = FileManager.default
struct DocumentsDirectory {
static var localDocumentsURL: URL? {
return FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
}
static var iCloudDocumentsURL: URL? {
return FileManager.default.url(forUbiquityContainerIdentifier: nil)?.appendingPathComponent("Documents")
}
}
// Return the Document directory (Cloud OR Local)
// To do in a background thread
func getDocumentDiretoryURL() -> URL {
debugPrint("======================= Document paths =======================")
debugPrint("iCloud Directory: \(String(describing: DocumentsDirectory.iCloudDocumentsURL?.path))")
debugPrint("Local Directory: \(String(describing: DocumentsDirectory.localDocumentsURL?.path))")
if self.isCloudEnabled() {
return DocumentsDirectory.iCloudDocumentsURL!
} else {
return DocumentsDirectory.localDocumentsURL!
}
}
//// Return true if iCloud is enabled
func isCloudEnabled() -> Bool {
if DocumentsDirectory.iCloudDocumentsURL != nil { return true }
else {
debugPrint("iCloud is not enabled")
return false
}
}
func isFileExist(at path: String) -> Bool{
return FileManager.default.fileExists(atPath: path, isDirectory: nil)
}
//create subdirectory in iCloud
func createSubdirectory(folderName: String){
guard isCloudEnabled() else {return}
let nestedFolderURL = DocumentsDirectory.iCloudDocumentsURL!.appendingPathComponent(folderName)
if self.isFileExist(at: nestedFolderURL.path){
debugPrint("!!!!!! Folder already exist !!!!!!")
return
}else{
do{
let _ = try FileManager.default.createDirectory(
at: nestedFolderURL,
withIntermediateDirectories: false,
attributes: nil
)
debugPrint("\(folderName) is created sussceefully")
} catch {
debugPrint("Can't create a folder")
}
}
}
func createTextFileInDirectry(folderName: String,
filename: String){
guard self.isCloudEnabled() else {return}
let nestedFolderURL = DocumentsDirectory.iCloudDocumentsURL!.appendingPathComponent(folderName)
if !self.isFileExist(at: nestedFolderURL.path){
debugPrint("\(folderName) is not found at \(DocumentsDirectory.iCloudDocumentsURL!)")
}
let fullname = filename + ".txt"
let fileUrl = nestedFolderURL.appendingPathComponent(fullname)
do {
try "Hello iCloud!".write(to: fileUrl, atomically: true, encoding: .utf8)
debugPrint("Write successfully in the file")
}
catch {
print(error.localizedDescription)
}
}
//// Delete all files at URL
//
func deleteFilesInDirectory(url: URL,filenameWithExtension: String = "") {
if filenameWithExtension.isEmpty{
let enumerator = self.fileManager.enumerator(atPath: url.path)
while let file = enumerator?.nextObject() as? String {
do {
if !self.isFileExist(at: url.appendingPathComponent(file).path){
debugPrint("!!!!! File is not exist !!!!")
return
}
try fileManager.removeItem(at: url.appendingPathComponent(file))
print("!!! \(file) deleted !!!")
} catch let error as NSError {
print("!!! Failed deleting files : \(error) !!!")
}
}
debugPrint("All files deleted")
}else{
if !self.isFileExist(at: url.appendingPathComponent(filenameWithExtension).path){
debugPrint("file is not exist")
return
}
debugPrint("Deleting........")
do {
try fileManager.removeItem(at: url.appendingPathComponent(filenameWithExtension))
print("\(filenameWithExtension) deleted")
} catch let error as NSError {
print("Failed deleting files : \(error)")
}
}
}
func fetchListOfFiles(at urlPath: String = DocumentsDirectory.iCloudDocumentsURL?.path ?? ""){
guard self.isCloudEnabled(),
!urlPath.isEmpty else {
debugPrint("!!! Path is empty !!!")
return
}
let enumerator = self.fileManager.enumerator(atPath: urlPath)
debugPrint("!!!!================== All Files ===================!!!!")
while let file = enumerator?.nextObject() as? String {
debugPrint(file)
}
}
// Move iCloud files to local directory
// Local dir will be cleared
// No data merging
func moveFileToLocal(fileName: String = "",
withExtension: String = "",
copyToLocal: Bool = false) {
guard self.isCloudEnabled() else {return}
if fileName.isEmpty && withExtension.isEmpty{
let enumerator = fileManager.enumerator(atPath: DocumentsDirectory.iCloudDocumentsURL!.path)
while let file = enumerator?.nextObject() as? String {
debugPrint(file)
do {
//Note: When you try to send the file to iCloud you should set the flag to true. you where using false which is to remove the file from iCloud.
if copyToLocal{
try self.fileManager.copyItem(at: DocumentsDirectory.iCloudDocumentsURL!.appendingPathComponent(file), to: DocumentsDirectory.localDocumentsURL!.appendingPathComponent(file))
debugPrint("Copy to local dir \(DocumentsDirectory.localDocumentsURL!.appendingPathComponent(file))")
}else{
try fileManager.setUbiquitous(false,
itemAt: DocumentsDirectory.iCloudDocumentsURL!.appendingPathComponent(file),
destinationURL: DocumentsDirectory.localDocumentsURL!.appendingPathComponent(file))
debugPrint("Moved to local dir \(DocumentsDirectory.localDocumentsURL!.appendingPathComponent(file))")
}
} catch let error as NSError {
debugPrint("Failed to move file to local dir : \(error)")
}
}
}else{
var fullName: String = ""
if withExtension.isEmpty{
fullName = fileName
}else{
fullName = fileName+withExtension
}
if !self.isFileExist(at: DocumentsDirectory.iCloudDocumentsURL!.appendingPathComponent(fullName).path){
debugPrint("\(fullName) not exist")
return
}
do {
//Note: When you try to send the file to iCloud you should set the flag to true. you where using false which is to remove the file from iCloud.
if copyToLocal{
try fileManager.copyItem(at: DocumentsDirectory.iCloudDocumentsURL!.appendingPathComponent(fullName), to: DocumentsDirectory.localDocumentsURL!.appendingPathComponent(fullName))
debugPrint("Copy to local dir \(DocumentsDirectory.localDocumentsURL!.appendingPathComponent(fullName))")
}else{
try fileManager.setUbiquitous(false,
itemAt: DocumentsDirectory.iCloudDocumentsURL!.appendingPathComponent(fullName),
destinationURL: DocumentsDirectory.localDocumentsURL!.appendingPathComponent(fullName))
debugPrint("Moved to local dir \(DocumentsDirectory.localDocumentsURL!.appendingPathComponent(fullName))")
}
} catch let error as NSError {
debugPrint("Failed to move file to local dir : \(error)")
}
}
}
//// Move/copy iCloud files to local directory
//// Local dir will be cleared
//// No data merging
func moveFileToCloud(fileName: String,
withExtension: String,
copyToCloud:Bool = false) {
guard self.isCloudEnabled() else {return}
if fileName.isEmpty && withExtension.isEmpty{
let enumerator = fileManager.enumerator(atPath: DocumentsDirectory.localDocumentsURL!.path)
while let file = enumerator?.nextObject() as? String {
debugPrint(file)
do {
//Note: When you try to send the file to iCloud you should set the flag to true. you where using false which is to remove the file from iCloud.
if copyToCloud{
try fileManager.copyItem(at: DocumentsDirectory.localDocumentsURL!.appendingPathComponent(file), to: DocumentsDirectory.iCloudDocumentsURL!.appendingPathComponent(file))
debugPrint("Copy to icloud dir \(DocumentsDirectory.iCloudDocumentsURL!.appendingPathComponent(file))")
}else{
try fileManager.setUbiquitous(true,
itemAt:DocumentsDirectory.localDocumentsURL!.appendingPathComponent(file) ,
destinationURL:DocumentsDirectory.iCloudDocumentsURL!.appendingPathComponent(file))
debugPrint("Moved to icloud dir \(DocumentsDirectory.iCloudDocumentsURL!.appendingPathComponent(file))")
}
} catch let error as NSError {
print("Failed to move file to Cloud : \(error)")
}
}
}else{
var fullName: String = ""
if withExtension.isEmpty{
fullName = fileName
}else{
fullName = fileName+withExtension
}
if !self.isFileExist(at: DocumentsDirectory.localDocumentsURL!.appendingPathComponent(fullName).path){
debugPrint("\(fullName) not exist")
return
}
do {
//Note: When you try to send the file to iCloud you should set the flag to true. you where using false which is to remove the file from iCloud.
debugPrint(fullName)
if copyToCloud{
try self.fileManager.copyItem(at: DocumentsDirectory.localDocumentsURL!.appendingPathComponent(fullName),
to: DocumentsDirectory.iCloudDocumentsURL!.appendingPathComponent(fullName))
debugPrint("Copy to icloud dir \(DocumentsDirectory.iCloudDocumentsURL!.appendingPathComponent(fullName))")
}else{
try self.fileManager.setUbiquitous(true,
itemAt:DocumentsDirectory.localDocumentsURL!.appendingPathComponent(fullName) ,
destinationURL:DocumentsDirectory.iCloudDocumentsURL!.appendingPathComponent(fullName))
debugPrint("Moved to icloud dir \(DocumentsDirectory.iCloudDocumentsURL!.appendingPathComponent(fullName))")
}
} catch let error as NSError {
print("Failed to move file to Cloud : \(error)")
}
}
}
}