Search code examples
iosswiftiphoneapp-store

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


I am currently checking my app version. My apps are notified if there is a new version and should the App Store screen, press the OK. I am checking the app version to do it, but it always shows an error.

    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
    }

Usage

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

Is this because my app doesn't have an app store?

  1. If I have an app store, What response can I get to know if I have a version to update?
  2. How can I go to the App Store?

Please help me a lot.


Solution

  • Yes, the method you use must be an already published app.

    If you use an unpublished app, you will get results = []

    Go to the App Store like this

    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
    
    }
    

    Note:

    This method will not be very timely, meaning that the application you just released, even if it can be searched in the App store, but results will not be updated immediately. Update information will be available after approximately 1 hour, or longer