一个简单的基础框架,用于帮助推荐/强制应用更新并淘汰旧版本
在您的Package.swift文件中添加依赖项
import PackageDescription
let package = Package(
...
...
dependencies: [
.Package(url: "https://github.com/pseudomuto/Retired.git", majorVersion: 2)
],
...
...
)
您需要在某服务器上托管一个JSON文件,该文件定义了是否需要更新、推荐或不必要更新的选项。
该文件还定义了在各情况下要显示的消息。例如,如果用户已安装版本1.0(并使用下面的文件),当调用Retire.check
时,在完成块中提供的消息将是强制
的。
如果他们运行的是1.1,将得到推荐的消息,如果运行的是2.0,则不需要更新。
以下是一个版本文件的示例
{
"messaging": {
"forced": {
"title": "App Update Required",
"message": "A new version of the app is available. You need to update now",
"continueButtonText": "Let's do this"
},
"recommended": {
"title": "App Update Available",
"message": "A new version is available. Want it?",
"continueButtonText": "I want it",
"cancelButtonText": "No thanks"
}
},
"versions": [
{ "version": "1.0", "policy": "force" },
{ "version": "1.1", "policy": "recommend" },
{ "version": "2.0", "policy": "none" }
]
}
这里有两种类型的对象;消息和版本。
消息
title
- 提醒的标题(例如,新版本可用)message
- 要显示的消息(例如,有新版本可用)continueButtonText
- 连接到应用商店的按钮标签cancelButtonText
- (可选)取消按钮的标签(仅适用于推荐更新)版本
version
- 应用的版本(从CFBundleShortVersionString
获取)policy
- force
、recommend
或none
之一我不打算告诉您如何处理显示提醒,但以下是在示例应用中我所做的(如果您打开工作区,将会在AppDelegate.swift中看到它)。
首先,我在Message
上创建了一个扩展
extension Message {
func presentInController(controller: UIViewController?) {
let alert = UIAlertController(title: title, message: message, preferredStyle: .Alert)
alert.addAction(UIAlertAction(title: continueButtonText, style: .Default, handler: goToAppStore))
if cancelButtonText != nil {
alert.addAction(UIAlertAction(title: cancelButtonText, style: .Cancel, handler: nil))
}
controller?.presentViewController(alert, animated: true, completion: nil)
}
private func goToAppStore(action: UIAlertAction) {
UIApplication.sharedApplication().openURL(iTunesURL)
}
}
然后在applicationDidFinishLaunching:withOptions
中,我用版本文件的URL来配置Retired
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
let versionURL = NSURL(string: "https://example.com/versions.json")!
let intervalBetweenRequests = 60 * 60 * 24 // one day between recommended app updates
Retired.configure(versionURL, suppressionInterval: intervalBetweenRequests)
return true
}
最后,在applicationDidBecomeActive
中,我查询状态并在必要时显示消息
func applicationDidBecomeActive(application: UIApplication) {
// you should catch the error in production apps
try! Retired.check() { updateRequired, message, error in
guard updateRequired else { return }
// handle error (non 200 status or network issue)
if let message = message {
message.presentInController(application.keyWindow?.rootViewController)
}
}
}
版权所有 © 2016 pseudomuto [email protected]
特此授予任何人获得本软件及其相关文档文件(以下简称“软件”)副本的权限,无代价地处理该软件,包括但不限于使用、复制、修改、合并、发布、分发、转让和/或出售软件副本的权利,并允许向软件提供方提供软件的人进行上述操作,但需遵守以下条件
上述版权声明和本许可声明应包含在软件的所有副本或主要部分中。
软件按原样提供,不提供任何形式的保证,无论是明示的还是暗示的,包括但不限于适销性、特定用途适用性和非侵权性保证。在任何情况下,作者或版权持有人均不对任何索赔、损害或其他责任承担责任,无论这些责任是否基于合同、侵权或其它行为,是否源自、因或与软件及其使用或其它方式有关。