如何在Swift5上的App Store中查看我的应用是否有新版本?

t98cgbkg  于 2023-09-30  发布在  Swift
关注(0)|答案(4)|浏览(170)

我正在检查我的应用程序版本。如果有新版本,我的应用程序会收到通知,如果出现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.如果我有一个应用程序商店,如果我有一个版本要更新,我可以得到什么回应?
1.如何进入App Store?
请多多帮助我。

anauzrmj

anauzrmj1#

是的,您使用的方法必须是已发布的应用程序。
如果您使用未发布的应用程序,您将获得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小时或更长时间后提供

nnt7mjpx

nnt7mjpx2#

我已经实现了这种方式来更新我的新版本。
步骤1:我已经保存了我的当前版本,这将是活的。示例:

struct appVersion {
static let version = "1.3.45"

}
步骤2:检查我的应用程序版本已经保存,等于活的应用程序或没有.
示例:

if userDefaults.value(forKey: "liveAppVersion") != nil {
        if userDefaults.string(forKey: "liveAppVersion") != appVersion.version {
            DispatchQueue.main.asyncAfter(deadline: .now() + 1){
                self.alertUpdate(msz: ConstantStr.updateStr.val, titleStr: "Alert!", btntext: "Update") { success in
                    if success == true {
                        if let url = URL(string: "itms-apps://itunes.apple.com/app/id1462877960") {
                            UIApplication.shared.open(url)
                        }
                    }}
            }
            
        }
        
    }
    else {
        UpdateVersion.share.appUpdateAvailable { success in
            if userDefaults.string(forKey: "liveAppVersion") != appVersion.version {
                DispatchQueue.main.asyncAfter(deadline: .now() + 1){
                    self.alertUpdate(msz: ConstantStr.updateStr.val, titleStr: "Alert!", btntext: "Update") { success in
                        if success == true {
                            if let url = URL(string: "itms-apps://itunes.apple.com/app/id1462877960") {
                                UIApplication.shared.open(url)
                            }
                        }}
                }
            }
        }
    }

step 3:此方法将检查应用商店中的当前版本。
示例:

func appUpdateAvailable(handler:@escaping(Bool?) -> ())
{
    let storeInfoURL: String = "http://itunes.apple.com/lookup?bundleId=com.test.app"
      let urlOnAppStore = NSURL(string: storeInfoURL)
        if let dataInJSON = NSData(contentsOf: urlOnAppStore! as URL) {
            // Try to deserialize the JSON that we got
            if let dict: NSDictionary = try? JSONSerialization.jsonObject(with: dataInJSON as Data, options: JSONSerialization.ReadingOptions.allowFragments) as! [String: AnyObject] as NSDictionary {
                if let results:NSArray = dict["results"] as? NSArray {
                   if let version = (results[0] as AnyObject).value(forKey: "version") {
                       userDefaults.set(version, forKey: "liveAppVersion")
                        handler(true)
 
                    }
                }
            }
        }
   }

->对于警报方法,当点击更新按钮时可以跳转到App Store以更新当前版本:

func alertUpdate(msz: String, titleStr : String, btntext: String,handler:@escaping(Bool?) -> ()){
    let alert = UIAlertController(title: titleStr, message: msz, preferredStyle: .alert)
   
    let ok = UIAlertAction(title: btntext, style: .default) { alert in
       handler(true)
   }
   
    alert.addAction(ok)
    self.present(alert, animated: true)
}

最后!你可以这个弹出更新x1c 0d1x

fzsnzjdm

fzsnzjdm3#

我已经通过一个完成处理程序完成了这一点

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

zkure5ic

zkure5ic4#

我也使用了completion handler。

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

相关问题