Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter: Exclude directory from iCloud-Backup

I'm writing a flutter app. This app will contain image, audio and video files. These files should be available even when the app is offline. The files can be downloaded from a server, or manually placed in the application's document directory (ios) or external directory (Android) via a USB connection. Additionally, a Json file will provide information to the app on how to process these media files. The directory structure will look like the following:

AppContent
-> info.json
-> Audio folder
->-> Files...
-> Video folder
->-> Files...
-> Pictures folder
->-> Files...

According to apple.developers such files should not be backed up by icloud. This is also not wanted in this application. The JSON file only contains information about the media files, no user data. https://developer.apple.com/icloud/documentation/data-storage/index.html

This tutorial shows how to exclude a folder from the iCloud backup: https://developer.apple.com/library/archive/qa/qa1719/_index.html

Is there an easy way to achieve this in the Flutter? Or do I have to call the native ios function?

like image 695
Shraxy Avatar asked Sep 07 '25 19:09

Shraxy


1 Answers

I disabled iCloud Backup on my app's Documents folder by doing this:

  1. Add the following function to ios/Runner/AppDelegate.swift:
private func setExcludeFromiCloudBackup(isExcluded: Bool) throws {
    var fileOrDirectoryURL = try! FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: true)
    var values = URLResourceValues()
    values.isExcludedFromBackup = isExcluded
    try fileOrDirectoryURL.setResourceValues(values)
}
  1. Call it in the application() function:
    GeneratedPluginRegistrant.register(with: self)
      
    // Exclude the documents folder from iCloud backup.
    try! setExcludeFromiCloudBackup(isExcluded: true)
    
    return super.application(application, didFinishLaunchingWithOptions: launchOptions)

Your AppDelegate.swift should look something like this:

import UIKit
import Flutter

@UIApplicationMain
@objc class AppDelegate: FlutterAppDelegate {
  override func application(
    _ application: UIApplication,
    didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
  ) -> Bool {
    GeneratedPluginRegistrant.register(with: self)
      
    // Exclude the documents folder from iCloud backup.
    try! setExcludeFromiCloudBackup(isExcluded: true)
    
    return super.application(application, didFinishLaunchingWithOptions: launchOptions)
  }
}

private func setExcludeFromiCloudBackup(isExcluded: Bool) throws {
    var fileOrDirectoryURL = try! FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: true)
    var values = URLResourceValues()
    values.isExcludedFromBackup = isExcluded
    try fileOrDirectoryURL.setResourceValues(values)
}

Note that no error checking is done, but I can't think of any reason why it would fail. I haven't noticed any additional startup time caused by running the function, which makes sense as it is just setting an attribute on a directory.

like image 148
UnicornsOnLSD Avatar answered Sep 09 '25 15:09

UnicornsOnLSD