簡體   English   中英

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

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

我目前正在檢查我的應用程序版本。 如果有新版本,我的應用程序會收到通知,如果出現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. 我怎樣才能 go 到 App Store?

請多多幫助我。

是的,您使用的方法必須是已經發布的應用程序。

如果您使用未發布的應用程序,您將獲得results = []

Go 像這樣到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)
                    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM