【问题标题】:How To check if there is a new version of my app in the App Store on Swift5?如何在 Swift5 上检查 App Store 中是否有我的应用程序的新版本?
【发布时间】:2020-02-10 14:53:40
【问题描述】:

我目前正在检查我的应用版本。如果有新版本,我的应用程序会收到通知,如果出现 App Store 屏幕,请按 OK。我正在检查应用程序版本,但它总是显示错误。

    func isUpdateAvailable(completion: @escaping (Bool?, Error?) -> Void) throws -> URLSessionDataTask {
        guard let info = Bundle.main.infoDictionary,
            let currentVersion = info["CFBundleShortVersionString"] as? String,
            let identifier = info["CFBundleIdentifier"] as? String,
            let url = URL(string: "http://itunes.apple.com/lookup?bundleId=\(identifier)") else {
                throw IXError.invalidBundleInfo
        }
        Log.Debug(currentVersion)
        let task = URLSession.shared.dataTask(with: url) { (data, response, error) in
            do {
                if let error = error { throw error }
                guard let data = data else { throw IXError.invalidResponse }
                let json = try JSONSerialization.jsonObject(with: data, options: [.allowFragments]) as? [String: Any]
                guard let result = (json?["results"] as? [Any])?.first as? [String: Any], let version = result["version"] as? String else {
                    throw IXError.invalidResponse
                }
                completion(version != currentVersion, nil)
            } catch {
                completion(nil, error)
            }
        }
        task.resume()
        return task
    }

用法

        _ = try? isUpdateAvailable { (update, error) in
            if let error = error {
                Log.Error(error)
            } else if let update = update {
                Log.Info(update)
            }
        }

这是因为我的应用没有应用商店吗?

  1. 如果我有应用商店,如果我有要更新的版本,我可以得到什么响应?
  2. 如何前往 App Store?

请多多帮助我。

【问题讨论】:

  • 当我提供自己的包标识符时,该端点似乎为我返回空,因此请尝试检查文件是否有任何结果。无论如何,这个解决方案似乎不是非常可扩展或可靠,我建议在某个地方托管一个包含版本号的文件。虽然开销更大,但更可靠
  • @DavidLiaw 你是说我用的功能不好?那你能不能给我一个更好的解决方案??

标签: ios swift iphone app-store


【解决方案1】:

我也使用了完成处理程序。

private func getAppInfo(completion: @escaping (AppInfo?, Error?) -> Void) -> URLSessionDataTask? {
    guard let identifier = Bundle.main.infoDictionary?["CFBundleIdentifier"] as? String,
        let url = URL(string: "http://itunes.apple.com/lookup?bundleId=\(identifier)") else {
            DispatchQueue.main.async {
                completion(nil, VersionError.invalidBundleInfo)
            }
            return nil
    }
private func getVersion() {
_ = getAppInfo { info, error in
                if let appStoreAppVersion = info?.version {
                    let new = appStoreAppVersion.components(separatedBy: ".")
                    if let error = error {
                        print("error getting app store version: \(error)")
                    } else {
                        print(new)
                    }
}

【讨论】:

    【解决方案2】:

    是的,您使用的方法必须是已经发布的应用程序。

    如果您使用未发布的应用,您将获得results = []

    像这样转到App Store

    let appId = "1454358806" // Replace with your appId
    let appURL = URL.init(string: "itms-apps://itunes.apple.com/cn/app/id" + appId + "?mt=8") //Replace cn for your current country
    
    UIApplication.shared.open(appURL!, options:[.universalLinksOnly : false]) { (success) in
    
    }
    

    注意:

    这种方法不会很及时,也就是说你刚刚发布的应用,即使在App store可以搜索到,但是results不会马上更新。大约 1 小时或更长时间后将提供更新信息

    【讨论】:

    • 感谢您的好主意。但是移动屏幕时需要将屏幕改为异步模式,所以先短暂显示黑屏,然后再移动到屏幕上。我该怎么做?
    • 什么意思,我不太明白。
    • 使用“isUpdateAvailable”功能时,应禁用屏幕移动。否则,它将运行缓慢。但是在使用异步机的时候,黑屏会短暂显示,然后移动到我指定的屏幕。
    • 在哪里可以获得我的应用 ID?
    • appId 在账户中心可用,前提是您创建了发布应用程序。 developer.apple.com/account。输入app store之前有没有指出黑屏?我会仔细检查的。
    【解决方案3】:

    我已经通过完成处理程序完成了这项工作

    func appStoreVersion(callback: @escaping (Bool,String)->Void) {
        let bundleId = Bundle.main.infoDictionary!["CFBundleIdentifier"] as! String
        Alamofire.request("https://itunes.apple.com/lookup?bundleId=\(bundleId)").responseJSON { response in
          if let json = response.result.value as? NSDictionary, let results = json["results"] as? NSArray, let entry = results.firstObject as? NSDictionary, let appStoreVersion = entry["version"] as? String{
            callback(true,appStoreVersion)
          }else{
            callback(false, "-")
            }
        }
      }
    

    appStoreVersion 包含您在应用商店中的应用版本。请记住:当您的应用在应用商店上线时,您可能需要最多 24 小时才能看到最新版本。 使用方法如下:

    appStoreVersion { (success,version) in
                appVersion = version
                self.VersionLabel.text = "App version \(appVersion)"
    
            }
    

    您可以使用成功版本来执行无法检索版本的不同操作。即您没有连接或。 您可以检查它在此应用程序中的使用方式(设置选项卡): https://apps.apple.com/us/app/group-expenses-light/id1285557503

    【讨论】:

    • 尝试使用时如何使用?
    猜你喜欢
    • 2011-09-09
    • 2021-03-13
    • 1970-01-01
    • 2023-03-26
    • 1970-01-01
    • 2023-03-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多