Last active
April 27, 2023 09:29
-
-
Save dhaneshgosai/3d45e03e6e7ea2577c2e685f4d0800a0 to your computer and use it in GitHub Desktop.
This Gist Class for Checking App Store version with local version.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// AppStoreUpdate.swift | |
// | |
// Created by CodeChanger on 03/11/19. | |
// Copyright © 2019 CodeChanger. All rights reserved. | |
// | |
import UIKit | |
enum CustomError: Error { | |
case jsonReading | |
case invalidIdentifires | |
case invalidURL | |
case invalidVersion | |
case invalidAppName | |
} | |
class AppStoreUpdate: NSObject { | |
static let shared = AppStoreUpdate() | |
static let iTunesAppBaseUrl = "https://itunes.apple.com/lookup?bundleId=" | |
func showAppStoreVersionUpdateAlert(isForceUpdate: Bool) { | |
do { | |
//Get Bundle Identifire from Info.plist | |
guard let bundleIdentifire = Bundle.main.infoDictionary?["CFBundleIdentifier"] as? String else { | |
print("No Bundle Info found.") | |
throw CustomError.invalidIdentifires | |
} | |
// Build App Store URL | |
guard let url = URL(string:AppStoreUpdate.iTunesAppBaseUrl + bundleIdentifire) else { | |
print("Issue with generating URL.") | |
throw CustomError.invalidURL | |
} | |
let request = URLRequest(url: url, cachePolicy: URLRequest.CachePolicy.reloadIgnoringLocalAndRemoteCacheData, timeoutInterval: 60.0) | |
let serviceTask = URLSession.shared.dataTask(with: request) { (responseData, response, error) in | |
do { | |
// Check error | |
if let error = error { throw error } | |
//Parse response | |
guard let data = responseData else { throw CustomError.jsonReading } | |
let result = try? JSONSerialization.jsonObject(with: data, options: .allowFragments) | |
let itunes = ItunesAppInfoItunes.init(fromDictionary: result as! [String : Any]) | |
if let itunesResult = itunes.results.first { | |
//Get Bundle Version from Info.plist | |
guard let appShortVersion = Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String else { | |
print("No Short Version Info found.") | |
throw CustomError.invalidVersion | |
} | |
let compareResult = appShortVersion.versionCompare(itunesResult.version) | |
switch compareResult { | |
case .orderedAscending: | |
print("Itune version grater than local version") | |
//Show Update alert | |
var message = "" | |
//Get Bundle Version from Info.plist | |
if let appName = Bundle.main.infoDictionary?["CFBundleName"] as? String { | |
message = "\(appName) has a new version(\(itunesResult.version!)) available on the App Store." | |
} else { | |
message = "This app has a new version(\(itunesResult.version!)) available on the App Store." | |
} | |
//Show Alert on main thread | |
DispatchQueue.main.async { | |
self.showUpdateAlert(message: message, appStoreURL: itunesResult.trackViewUrl, isForceUpdate: isForceUpdate) | |
} | |
case .orderedDescending: | |
print("Itune version less than local version") | |
case .orderedSame: | |
//App Store & Local App Have same Version. | |
print("Current Version is the latest version of the app.") | |
default: | |
print("No Result") | |
} | |
} | |
} catch { | |
print(error) | |
} | |
} | |
serviceTask.resume() | |
} catch { | |
print(error) | |
} | |
} | |
func showUpdateAlert(message : String, appStoreURL: String, isForceUpdate: Bool) { | |
let controller = UIAlertController(title: "New Version", message: message, preferredStyle: .alert) | |
//Optional Button | |
if !isForceUpdate { | |
controller.addAction(UIAlertAction(title: "Later", style: .cancel, handler: { (_) in })) | |
} | |
controller.addAction(UIAlertAction(title: "Update", style: .default, handler: { (_) in | |
guard let url = URL(string: appStoreURL) else { | |
return | |
} | |
if #available(iOS 10.0, *) { | |
UIApplication.shared.open(url, options: [:], completionHandler: nil) | |
} else { | |
UIApplication.shared.openURL(url) | |
} | |
})) | |
let applicationDelegate = UIApplication.shared.delegate as? AppDelegate | |
applicationDelegate?.window?.rootViewController?.present(controller, animated: true) | |
} | |
} | |
extension String { | |
func versionCompare(_ otherVersion: String) -> ComparisonResult { | |
return self.compare(otherVersion, options: .numeric) | |
} | |
} |
Hey iShawnWang,
I used modal classes for reading iTunes App version API.
Please find below links for missing modal files.
Add these files to your project and try again.
Thanks,
CodeChanger
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Use of unresolved identifier 'ItunesAppInfoItunes'