Swift excluding files from iCloud backup

Having your device automatically back-up to iCloud is one of the better services Apple provides.

Although not new, iCloud (introduced in June of 2011) back-up is one of the better services Apple has rolled out. If used right this removes a whole class of end user data lose fears.

However as an app developer you need to take this into consideration. Where it is privacy, size constraints, or other you need to be aware what you might be sending to iCloud.

You might wish to not back-up specific files. To do this you must set the file’s isExcludedFromBackup attribute to true. Below is a helper method to help get you started.

struct FileHelpers {
@discardableResult static func addSkipBackupAttribute(url: URL) throws -> Bool {
var fileUrl = url
do {
if FileManager.default.fileExists(atPath: fileUrl.path) {
var resourceValues = URLResourceValues()
resourceValues.isExcludedFromBackup = true
try fileUrl.setResourceValues(resourceValues)
}
return true
} catch {
print("failed setting isExcludedFromBackup \(error)")
return false
}
}
}