0

can any one help me how can i impliment alert for force update my iOS app. if new version is available on AppStore where in any Device have old or different version then i want to check if version is different then user update new version from AppStore. and how to get app information for my app.

Vvk
  • 4,031
  • 29
  • 51

1 Answers1

0

You can develop the function by comparing the version of the modern app with the version on the App Store.

enum VersionError: Error {

    case invalidResponse, invalidBundleInfo

}

class AppStoreCheck {

    static 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: your appstore url) else {

                throw VersionError.invalidBundleInfo

        }

        let task = URLSession.shared.dataTask(with: url) { (data, response, error) in

            do {

                if let error = error { throw error }

                guard let data = data else { throw VersionError.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 VersionError.invalidResponse

                }

                let verInt = NSString.init(string: version).intValue

                let currentVerInt = NSString.init(string: currentVersion).intValue

                completion(verInt > currentVerInt, nil) 

            } catch {

                completion(nil, error)

            }

        }

        task.resume()

        return task

    }

}

This class compares your app version with your app store version and reports it as a bool value. You can call AppStoreCheck.isUpdateAvailable and add the function through a bool value.

Hailey
  • 302
  • 1
  • 7