PrinceOfVersions 4.0.5

PrinceOfVersions 4.0.5

由以下使用者维护:Josip CavarFilip BećIvana MrsicJasmin Abou AldanFilip GulanAntonijo Bezmalinovic



  • 作者:
  • Jasmin Abou Aldan

Prince of Versions

Bitrise GitHub CocoapodsCarthage compatible Swift Package Manager compatible Cocoapods platforms

PoV

库使用配置从某些资源中检查更新。

特性

  • 网络 资源加载更新配置
  • 使用预定义解析器解析 JSON 格式 的更新配置
  • 异步加载,并使用 回调 通知结果

需求

  • iOS 8.0+
  • macOS 10.10+
  • Xcode 11.0+

安装

在项目中使用 Prince of versions 的一种简单方法是通过 CocoaPods 包管理器。

CocoaPods

如果尚未安装,请参阅 CocoaPods 的安装说明

要将库集成到您的 Xcode 项目中,请在 Podfile 中指定 pod 依赖项

platform :ios, '8.0'
use_frameworks!

pod 'PrinceOfVersions'

或者

platform :osx, '10.10'
use_frameworks!

pod 'PrinceOfVersions'

运行 pod install

pod install

Carthage

有关 Carthage 的安装和使用说明,您可以查看官方 快速入门文档

要将库集成到您的 Xcode 项目中,请在 Cartfile 中指定它

github "infinum/iOS-prince-of-versions"

运行 carthage update

Swift Package Manager

要从 Swift Package Manager 安装 "Prince of Versions",您应该

更多信息,请参阅 Swift Package Manager

JSON 文件

有关 JSON 文件详情和格式,请参阅 JSON 规范

使用

从网络资源加载

获取所有数据

let princeOfVersionsURL = URL(string: "https://pastebin.com/raw/0MfYmWGu")!

PrinceOfVersions.checkForUpdates(from: princeOfVersionsURL, completion: { [unowned self] response in
    switch response.result {
    case .success(let updateResultData):
        print("Update version: \(updateResultData.updateVersion)")
        print("Installed version: \(updateResultData.updateInfo.installedVersion)")
        print("Update status: \(updateResultData.updateStatus)")
    case .failure:
        // Handle error
        break
    }
})

添加要求

对于配置中列出的每个要求键,应该存在一个要求检查闭包。如果您不提供它,则该要求被视为未满足,整个配置将被丢弃。但是,如果您提供要求检查,但JSON不包含检查所需的要求键,则检查将被忽略。

如果JSON中存在required_os_version键,则在要求下,库本身将处理检查该要求是否满足。您不需要提供闭包。

以下是添加要求检查闭包的示例。

let options = PoVRequestOptions()

options.addRequirement(key: "region") { (value) -> Bool in
    guard let value = value as? String else { return false }
    // Check OS localisation
    return value == "hr"
}

options.addRequirement(key: "bluetooth") { (value) -> Bool in
    guard let value = value as? String else { return false }
    // Check device bluetooth version
    return value.starts(with: "5")
}

let princeOfVersionsURL = URL(string: "https://pastebin.com/raw/0MfYmWGu")!

PrinceOfVersions.checkForUpdates(from: princeOfVersionsURL, options: options, completion: { [unowned self] response in
    switch response.result {
    case .success(let updateResultData):
        print("Update version: \(updateResultData.updateVersion)")
        print("Installed version: \(updateResultData.updateInfo.installedVersion)")
        print("Update status: \(updateResultData.updateStatus)")
    case .failure:
        // Handle error
        break
    }
})

如果您考虑以下JSON示例和上述示例中添加的要求检查,第一个配置将被认为是不合适的,因为对于free-memory的要求检查未定义。但是,第二个配置中的所有要求都得到了满足,其值将被返回。

...
      {
         "required_version":"1.2.3",
         "last_version_available":"1.9.0",
         "notify_last_version_frequency":"ALWAYS",
         "requirements":{
            "required_os_version":"8.0.0",
            "region":"hr",
            "bluetooth":"5.0",
            "free-memory":"80MB"
         },
      },
      {
         "required_version":"1.2.3",
         "last_version_available":"2.4.5",
         "notify_last_version_frequency":"ALWAYS",
         "requirements":{
            "required_os_version":"12.1.2",
            "region":"hr",
            "bluetooth":"5.0"
         },
      }
...

使用App Store数据自动检查

如果您不希望管理所需的JSON配置文件checkForUpdates,可以使用checkForUpdateFromAppStore。此方法将自动获取您的应用程序BundleID,并将返回从App Store获取的版本信息。

但是,updateStatus结果只能是值UpdateStatus.noUpdateAvailableUpdateStatus.newUpdateAvailable。使用此方法和AppStore提供的数据无法检查更新是否是强制性的。

PrinceOfVersions.checkForUpdateFromAppStore(
    trackPhaseRelease: false,
    notificationFrequency: .once,
    completion: { result in
        switch result {
        case .success(let appStoreResult):
            print("Update version: \(appStoreResult.updateVersion)")
            print("Installed version: \(appStoreResult.updateInfo.installedVersion)")
            print("Update status: \(appStoreResult.updateStatus)")
        case .failure:
            // Handle error
        }
})

可以使用参数 notificationFrequency 定义更新通知频率。如果您将参数值设置为 NotificationType.once,则在第一次调用此方法时,如果存在新版本,将返回 UpdateStatus.newUpdateAvailable,而在随后的每个调用中,将返回 UpdateStatus.noUpdateAvailable,针对特定版本。如果描述的行为不符合您的需求,您总是可以将此参数设置为 NotificationType.always,并在有新版本可用时将更新状态设置为 UpdateStatus.newUpdateAvailable

多重目标

如果您的应用程序具有多个目标,您可能需要多个JSON配置文件。如果是这种情况,不要忘记为每个目标设置不同的URL。

安全证书固定

如果您使用证书固定与保存您的JSON版本文件的服务器进行安全通信,请将证书放入应用程序资源文件夹中(确保证书具有以下扩展名之一:".cer""CER""crt""CRT""der""DER")。Prince Of Versions将查找主包中的所有证书。然后,在调用 loadConfiguration 方法时,将 shouldPinCertificates 参数设置为 true

let url = URL(string: "https://pastebin.com/raw/0MfYmWGu")
PrinceOfVersions.checkForUpdates(from: url, shouldPinCertificates: true) { (response) in
    switch response.result {
    case .success(let result):
        if let latestVersion = result.updateInfo.latestVersion {
            print("Is minimum version satisfied: ", latestVersion)
        }
    case .failure(let error):
        print(error.localizedDescription)
    }
}

贡献

欢迎反馈和代码贡献。只需提交一个带有更改描述的拉取请求。通过为此项目做出贡献,您授予您的代码在此 许可证 下使用的权限。

致谢

Infinum 维护和支持。