BundleFileUpdater
使用最新本地资源文件版本交付您的应用程序,并在每次构建和运行时动态地从远程 URL 更新它们。您的用户始终拥有资源文件的最新版本,无需提交新应用程序。
使用说明
在运行时更新文件
从两个源保持应用包中本地文件的最新状态:从应用包和远程 URL。因此,一个可更新的文件副本会自动从应用包复制到文档目录,该目录应该是您应用程序中的唯一真相来源。除了调用 `BundleFileUpdater.updateFile` 之外,无需在其他地方直接引用文件从应用包或远程 URL。此方法支持在更新文件之前进行简单、自动和可选的字符串搜索和替换。
let localFileURL = BundleFileUpdater.updateFile("about.html", url: "https://www.example.com/about.html", header: ["User-Agent": "My User-Agent"], replacingTexts: ["href=\"/terms-of-service.html\"": "href=\"tos.html\""], didReplaceFile: { (destinationURL, error) in
guard error == nil else {
// an error occured or the remote file had no changes …
return
}
// local file was updated from url or from app bundle (because of an app update) …
})
获取由此类管理的可更新文件的文件 URL,该文件存储在文档目录中
let localFileURL = BundleFileUpdater.urlForFile("about.html")
随着每次构建更新文件
为此,您应该在您的项目中添加一个新文件(但不要添加到任何目标中),在该文件中,您指定要更新的文件,作为一个字典,键是相对于项目根目录的本地文件路径,值是检查更新的文件内容的对应远程URL。然后像这样调用该方法,以文件字典作为参数:
let files = [
"YourSourceDirectory/about.html": "https://www.example.com/about.html",
"YourSourceDirectory/tos.html": "https://www.example.com/terms-of-service.html"
]
BundleFileUpdater.updateBundleFilesFromCLI(files, header: ["User-Agent": "My User-Agent"], replacingTexts: ["href=\"/terms-of-service.html\"": "href=\"tos.html\""])
要每次构建时更新文件,请转到您的项目的目标设置中的 构建阶段 选项卡,在 编译源代码 阶段之前添加一个新的 运行脚本阶段,并插入以下脚本,其中 "$SRCROOT/YourSourceDirectory/DownloadScript.swift"
是您通过 BundleFileUpdater.updateBundleFilesFromCLI
调用创建的新文件:
cat "$PODS_ROOT/BundleFileUpdater/BundleFileUpdater/BundleFileUpdater.swift" "$SRCROOT/YourSourceDirectory/DownloadScript.swift" | xcrun -sdk macosx swift -
安装
BundleFileUpdater 通过 CocoaPods 提供。要安装它,只需将以下行添加到您的 Podfile 中:
pod "BundleFileUpdater"
许可
BundleFileUpdater 在 MIT 许可下可用。更多信息请参阅 LICENSE 文件。