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?
I disabled iCloud Backup on my app's Documents folder by doing this:
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)
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With