如何在 Swift5 的 App Store 中检查我的应用程序是否有新版本?

How To check if there is a new version of my app in the App Store on Swift5?

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

    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?

请多多帮助。

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

如果您使用未发布的应用,您将获得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 小时或更长时间后可用

我是通过完成处理程序完成的

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

我也用过完成处理程序。

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)
                    }
}