How to exclude file from backup in Swift?

Related searches

I had this working great with Swift 1.2 as I used the filePath as a string. Now Swift 2 wants us all to use URL paths I can't get this to work even though i'm reading through their docs.

I have;

var fileName = "myRespondusCSV.csv"

let fileManager = NSFileManager.defaultManager()

let documentsURL = fileManager.URLsForDirectory(NSSearchPathDirectory.DocumentDirectory, inDomains: NSSearchPathDomainMask.UserDomainMask)


if let documentPath: NSURL = documentsURL.first as NSURL! {

    filePath = documentPath.URLByAppendingPathComponent(fileName)
        print(filePath)        
    } else {
        fileManager.createFileAtPath(filePath!.path!,
                                     contents: ("" as String).dataUsingEncoding(NSUTF8StringEncoding)!,
                                     attributes:nil)
        print("file has been created")
    }
}

func excludeFileFromBackup() {

    var error:NSError?
    //var fileToExcludeh = NSURL.fileReferenceURL(filePath!)

    var fileToExcludeh = fileURLWithPath(filePath)

    let success = fileToExcludeh.setResourceValue(true, forKey: NSURLIsExcludedFromBackupKey, error: &error)
}

I'm getting a 'Use of unresolved identifier 'fileURLWithPath'!

Should I be using an absolute URL path?

This should work

        do {
            try filePath.setResourceValue(true, forKey: NSURLIsExcludedFromBackupKey)
        } catch _{
            print("Failed")
        }

Technical Q&A QA1719: How do I prevent files from being backed , QA1719: describes how an app can prevent files from being backed up to Listing 2 Excluding a File from Backups on iOS 5.1 and later (Swift)� To exclude files or directories from your backups for all user accounts, add those files or directories to the /etc/cpbackup-exclude.conf file. The /etc/cpbackup-exclude.conf file excludes the files that you specify relative to any location from which the backup script runs.

Swift 4 if anyone needs it:

var resourceValues = URLResourceValues()
resourceValues.isExcludedFromBackup = true
try? fileUrl.setResourceValues(resourceValues)

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� The second file affects backups only when the schedule is named fulls. For a given backup, NetBackup uses only one exclude list and that is the one with the most specific name. For example, if there are files named: exclude_list.wkstations and exclude_list.wkstations.fulls NetBackup uses only: exclude_list.wkstations.fulls

This is working for Swift 3. URL must be a var.

let urls:[URL] = FileManager.default.urls(for:FileManager.SearchPathDirectory.documentDirectory, in:FileManager.SearchPathDomainMask.userDomainMask)
let appDirectory:URL = urls.last!
var fileUrl:URL = appDirectory.appendingPathComponent("myFile")

var resourceValues:URLResourceValues = URLResourceValues()
resourceValues.isExcludedFromBackup = true

do
{
    try fileUrl.setResourceValues(resourceValues)
}
catch let error
{
    print(error.localizedDescription)
}

How to exclude file from backup with Swift 2?, dataUsingEncoding(NSUTF8StringEncoding)!, attributes:nil) print("file has been created") } } func excludeFileFromBackup() { var error:NSError? The volume may be configured for backup but may be offline, such as removable USB drives. Volumes that are offline are shown in grey. You can only back up data from a local drive that is formatted as an NTFS file system. Drives formatted as FAT (including FAT32) file systems do not appear in the list of drives to back up.

Here is my resolution for it on Swift 3

func addSkipBackupAttributeToItemAtURL(URL:NSURL) -> Bool {
    var success: Bool

    do {
        try URL.setResourceValue(true, forKey:URLResourceKey.isExcludedFromBackupKey)
        success = true
    } catch let error as NSError {
        success = false
        print("Error excluding \(URL.lastPathComponent!) from backup \(error)");
    }
    return success
}

Exclude Specified file url from iCloud backup � GitHub, Exclude Specified file url from iCloud backup. isExcludedFromBackup.swift. struct FileHelpers {. @discardableResult static func addSkipBackupAttribute(url:� Under Category select Backup, and under Type select Exclude: Under File or Pattern, click Browse, and a window headed File or Pattern will appear in which you can browse to any file on the drive that you want to exclude. Browse to and select any file on the drive that you want to exclude, and click OK.

The code that runs for me in swift 5 is...

var file: URL = <# your URL file #>
do {
   var res = URLResourceValues()
   res.isExcludedFromBackup = true
   try file.setResourceValues(res)
} catch {
   print(error)
}

Of course you can put this in a function or an extension.

iOS storage best practices. This article talks about the common , Code to exclude a file from backup. Be sure to store file only at one location i.e. either locally or on iCloud drive. If you put files in your local� The ability to exclude files from backup is very helpful. Adding new files or folders to the exclude list in CB is kind of painful; one has to go through a Windows "open" box, navigate to the file or folder, and click on "open". There are two problems with this: 1. It is slow. 2.

To check how much data your app is storing: - Install and launch your app - Go to Settings > iCloud > Storage & Backup > Manage Storage - If necessary, tap "Show all apps" - Check your app's storage The iOS Data Storage Guidelines indicate that only content that the user creates using your app, e.g., documents, new files, edits, etc., should be

Is there any method for determining whether a specific file actually is being backed up to iCloud? I don't mean whether it is supposed to be backed up or not, I mean whether there is a way to see

In the Backup Settings window, in the Backup Set drop-down list, select the backup set that you want to configure. On the What tab, click Add or exclude files or folders. In the window that appears, do the following: To add a file to the backup set, click Include File and navigate to the file you want to add.

Comments
  • fileURLWithPath is not a (global) function, but a method of NSURL. There are many examples on SO how to use that method, a recent one is stackoverflow.com/questions/32665508/….
  • The commented code ` //var fileToExcludeh = NSURL.fileReferenceURL(filePath!)` seems correct, then why did you comment that ?
  • The example here stackoverflow.com/questions/32665508 cannot-invoke-initializer-for-type-nsurl-with-an-argument-list-of-type-fileudoes not (as yet) have an answer. Swift 2 has removed the stringByAppendingPathComponent which means I need to create the path as a URL instead. To answer Midhun MP; the commented code threw another error 'function value was used as a property; add () to call'. There is no function with this name in my code 'fileToExcludeh'.
  • Thanks! just came back to this; your solution was better!
  • It can be written in a single line: try? filePath.setResourceValue(true, forKey: NSURLIsExcludedFromBackupKey)
  • Works fine for me in Swift 4
  • Is the given fileUrl modified or are there just other technical reasons for it to be a var? I looked at the source (github.com/apple/swift-corelibs-foundation/blob/master/…), but could not figure it out for sure.
  • @Klaas URL has to be mutable as you are modifying its resource values.
  • ah, makes sense. I did assume that those were not cached and directly written to the file system.