0

When logging in to an app I wrote, it says "We have a new update. Please update to continue." gives a warning. But the app is up to date. If I click OK, it redirects me to the app store, but there is no update.

How do I resolve this error?

Update Alert

I told it to compare the app version with the app store version and give an update warning accordingly, but it keeps giving.

1 Answers1

1
    enum VersionError: Error {
    case invalidResponse, invalidBundleInfo
}

@discardableResult
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 VersionError.invalidBundleInfo
    }
        
    let request = URLRequest(url: url, cachePolicy: URLRequest.CachePolicy.reloadIgnoringLocalCacheData)
    
    let task = URLSession.shared.dataTask(with: request) { (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 lastVersion = result["version"] as? String else {
                throw VersionError.invalidResponse
            }
            completion(lastVersion > currentVersion, nil)
        } catch {
            completion(nil, error)
        }
    }
    
    task.resume()
    return task
}

Usage:

  try? isUpdateAvailable {[self] (update, error) in
                if let error = error {
                    print(error)
                } else if update ?? false {
                    // show alert
                }
            }

I am using the same structure code and its is working fine, here is the reference link -> Check if my app has a new version on AppStore

Chandaboy
  • 1,302
  • 5
  • 10
  • Can you tell me where exactly to put these codes? I am somewhat new in this industry. – Kerem DEMİR Jul 06 '23 at 11:48
  • implmement above code in bottom of any class then replace your existing updatealertfuction with this one isUpdateAvailable – Chandaboy Jul 06 '23 at 11:51
  • I understand. How can I test if this is working? – Kerem DEMİR Jul 06 '23 at 11:58
  • you need to put previous version and run build on device then you will get this popup. like example, your current app store version is 2.0 (change to 1.0) run on device then you will get popup, and when you put 2.0 then it will not show popup, i hope you understand my point – Chandaboy Jul 06 '23 at 12:00
  • Yes I understood. Really thank you so much. – Kerem DEMİR Jul 06 '23 at 12:05