SwiftyUpdateKit
SwiftyUpdateKit 是一个适用于 iOS 和 macOS 的框架。该框架支持用户在 App Store 上发布新版本时更新应用。
支持
OS | 版本 |
---|---|
iOS | 13.0+ |
macOS | 10.15+ |
需要
- Swift 5+
安装
Swift 包管理器
SwiftyUpdateKit 可通过 Swift 包管理器获取。要使用 Xcode 安装,指定 SwiftyUpdateKit 的 git URL。
https://github.com/HituziANDO/SwiftyUpdateKit.git
Carthage
SwiftyUpdateKit 可通过 Carthage 获取。要安装,只需将以下行添加到 Cartfile
github "HituziANDO/SwiftyUpdateKit"
CocoaPods
SwiftyUpdateKit 可通过 CocoaPods 获取。要安装,只需将以下行添加到 Podfile
pod "SwiftyUpdateKit"
使用方法
导入框架
import SwiftyUpdateKit
初始化
为了初始化此框架,您需要在 AppDelegate 中的 application(_:,didFinishLaunchingWithOptions:)
方法(macOS: applicationDidFinishLaunching(_:)
方法)中使用 SUK.initialize
方法。见下文
let config = SwiftyUpdateKitConfig(
// Current app version.
version: Bundle.main.infoDictionary!["CFBundleShortVersionString"] as! String,
// iTunes ID.
// e.g.) The App Store URL: "https://apps.apple.com/app/sampleapp/id1234567890" -> iTunesID is 1234567890
iTunesID: "1234567890",
// The App Store URL of your app.
storeURL: "https://apps.apple.com/app/your-app/id1234567890",
// The country code used by iTunes Search API.
// See http://en.wikipedia.org/wiki/ISO_3166-1_alpha-2 for a list of ISO Country Codes.
country: "us",
// The method to compare the app version.
versionCompare: VersionCompare(),
// The update alert's title.
updateAlertTitle: "Released new version!",
// The update alert's message.
updateAlertMessage: "Please update the app. Please refer to the App Store for details of the update contents.",
// The update button's label.
updateButtonTitle: "Update",
// The remind-me-later button's label. If nil is specified, the button is hidden.
// That is, you can force a user to update because it cannot be canceled.
remindMeLaterButtonTitle: "Remind me later",
// (Optional) The count of retry when iTunes Search API failed. Default value is 2.
retryCount: 2,
// (Optional) Retries iTunes Search API after this delay in seconds. Default value is 1 second.
retryDelay: 1,
// (Optional) If true, the database is in development environment.
// Otherwise it is for production environment.
// Must set false when release your app.
development: {
#if DEBUG
return true
#else
return false
#endif
}()
)
SUK.initialize(withConfig: config)
快速使用
要检查是否发布了新版本,您可以在视图控制器的 viewDidAppear
方法中使用 SUK.checkVersion
方法。见下文
SUK.checkVersion(VersionCheckConditionAlways(), newRelease: { [weak self] newVersion, releaseNotes, firstUpdated in
guard let self = self else { return }
SUK.showReleaseNotes(from: self, text: releaseNotes, version: newVersion) {
// Release Notes has been closed.
}
}) {
SUK.requestReview(RequestReviewConditionAlways())
}
首先,此代码显示更新警报,如果当前应用版本较旧。
其次,当安装了新版本的应用时,它将显示您的 App Store 页面的发布说明。
最后,如果应用版本是最新的,并且已经显示发布说明,则请求对您的应用进行评论。
使用自定义 UI
您可以使用自定义 UI 来显示更新警报和发布说明。见下文
SUK.checkVersion(VersionCheckConditionAlways(), update: { [weak self] newVersion, releaseNotes in
guard let self = self else { return }
// This closure is called when current app version is old.
// Show custom update alert: present your view controller or add your view for the update alert.
}, newRelease: { [weak self] newVersion, releaseNotes, firstUpdated in
guard let self = self else { return }
// Show custom release notes: present your view controller or add your view to show the release notes.
}) {
// noop
}
要显示自定义更新警报,视图控制器在 update
闭包中展示您的视图控制器或添加您的视图以用于更新警报。要打开应用在 App Store 的页面,您可以使用以下方法
SUK.openAppStore()
重置状态
重置状态:存储的版本检查条件日期,存储的请求评论条件日期,以及存储的发布说明应用版本。
例如,您可以在测试和开发期间使用 SUK.reset
方法。
SUK.reset()
版本比较方法
默认版本比较方法参照 此处。如果您需要另一种比较方法,则实现 VersionComparable
协议并将它设置到 SwiftyUpdateKitConfig 中的 versionCompare
参数。
API 参考
要编写此框架的文档,请在终端中运行以下命令。此脚本依赖于 jazzy。
jazzy -version
jazzy version: 0.14.3
./make_docs.sh