gpt4 book ai didi

ios - Flutter:从 iCloud-Backup 中排除目录

转载 作者:行者123 更新时间:2023-12-05 06:19:29 24 4
gpt4 key购买 nike

我正在编写一个 flutter 应用程序。此应用程序将包含图像、音频和视频文件。即使应用程序处于离线状态,这些文件也应该可用。这些文件可以从服务器下载,也可以通过 USB 连接手动放置在应用程序的文档目录 (ios) 或外部目录 (Android) 中。此外,Json 文件将向应用程序提供有关如何处理这些媒体文件的信息。目录结构如下所示:

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

根据 apple.developers 的说法,此类文件不应由 icloud 备份。这在这个应用程序中也是不需要的。 JSON 文件仅包含有关媒体文件的信息,不包含用户数据。 https://developer.apple.com/icloud/documentation/data-storage/index.html

本教程介绍如何从 iCloud 备份中排除文件夹:https://developer.apple.com/library/archive/qa/qa1719/_index.html

在 Flutter 中有没有一种简单的方法可以实现这一点?还是必须调用原生 ios 函数?

最佳答案

我通过这样做在我的应用程序的文档文件夹中禁用了 iCloud 备份:

  1. 将以下函数添加到 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. application()函数中调用:
    GeneratedPluginRegistrant.register(with: self)

// Exclude the documents folder from iCloud backup.
try! setExcludeFromiCloudBackup(isExcluded: true)

return super.application(application, didFinishLaunchingWithOptions: launchOptions)

你的 AppDelegate.swift 应该看起来像这样:

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)
}

请注意,没有进行错误检查,但我想不出它会失败的任何原因。我没有注意到运行该函数会导致任何额外的启动时间,这是有道理的,因为它只是在目录上设置一个属性。

关于ios - Flutter:从 iCloud-Backup 中排除目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60836126/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com