Skip to content

Commit

Permalink
Merge pull request #13 from sha8wn/refactor-update-api
Browse files Browse the repository at this point in the history
v1.7.0 refactor update api approved on the app store
  • Loading branch information
sha8wn authored May 3, 2022
2 parents 4b7bf55 + 1b37dbe commit 0a777b0
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 45 deletions.
4 changes: 2 additions & 2 deletions First Class And More.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -1440,7 +1440,7 @@
INFOPLIST_FILE = "First Class And More/Info.plist";
IPHONEOS_DEPLOYMENT_TARGET = 11.0;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
MARKETING_VERSION = 1.6;
MARKETING_VERSION = 1.7;
PRODUCT_BUNDLE_IDENTIFIER = "de.first-class-and-more";
PRODUCT_NAME = "$(TARGET_NAME)";
PROVISIONING_PROFILE = "";
Expand Down Expand Up @@ -1468,7 +1468,7 @@
INFOPLIST_FILE = "First Class And More/Info.plist";
IPHONEOS_DEPLOYMENT_TARGET = 11.0;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
MARKETING_VERSION = 1.6;
MARKETING_VERSION = 1.7;
PRODUCT_BUNDLE_IDENTIFIER = "de.first-class-and-more";
PRODUCT_NAME = "$(TARGET_NAME)";
PROVISIONING_PROFILE = "";
Expand Down
103 changes: 61 additions & 42 deletions First Class And More/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -62,66 +62,85 @@ class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterD
case invalidResponse, invalidBundleInfo
}

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: "https://www.first-class-and-more.de/blog/fcam-api/app/v1/app-version/?auth=tZKWXujQ") else {
func isUpdateAvailable(completion: @escaping (Bool?, Bool?, Error?) -> Void) throws -> URLSessionDataTask {
guard let info = Bundle.main.infoDictionary,
let currentVersion = info["CFBundleShortVersionString"] as? String,
let url = URL(string: "https://www.first-class-and-more.de/blog/fcam-api/app/v1/app-version-v2?auth=tZKWXujQ&app=1") else {
throw VersionError.invalidBundleInfo
}
print("url: ", url)
print("current version", currentVersion)
let task = URLSession.shared.dataTask(with: url) { (data, response, error) in
do {
if let error = error
{
throw error
}
print("url: ", url)
print("current version", currentVersion)
let task = URLSession.shared.dataTask(with: url) { (data, response, error) in
do {
if let error = error
{
throw error

}

if data == nil {
throw VersionError.invalidResponse
}

let data = data!

print()
let json = try JSONSerialization.jsonObject(with: data, options: [.allowFragments]) as? [String: Any]

guard let responseData = json?["data"] as? [String: Any],
let version = responseData["version"] as? String
else {
throw VersionError.invalidResponse
}


var isUpdateForced = false

if let forceUpdate = responseData["force_update"] as? Bool,
forceUpdate {
isUpdateForced = true
}

print(version)

completion(version > currentVersion, isUpdateForced, nil)
} catch {
completion(nil, nil, error)
}

if data == nil {
throw VersionError.invalidResponse
}

let data = data!

print()
let json = try JSONSerialization.jsonObject(with: data, options: [.allowFragments]) as? [String: Any]

print(json)

guard let result = (json?["data"] as? String) else {
throw VersionError.invalidResponse
}

print(result)

completion(result > currentVersion, nil)
} catch {
completion(nil, error)
}
task.resume()
return task
}
task.resume()
return task
}

_ = try? isUpdateAvailable { (update, error) in
_ = try? isUpdateAvailable { (update, isUpdateForced, error) in
DispatchQueue.main.async {
if let error = error {
print(error)
} else if let update = update, update {
self.showUpdateAlert()
} else if let update = update, update,
let isUpdateForced = isUpdateForced {
self.showUpdateAlert(isForced: isUpdateForced)
}
}
}
}

private func showUpdateAlert() {
private func showUpdateAlert(isForced: Bool = true) {
if var topController = UIApplication.shared.keyWindow?.rootViewController {
while let presentedViewController = topController.presentedViewController {
topController = presentedViewController
}
topController.showPopupDialog(title: "Eine neue Version der First Class & More Reisedeals-App ist im App Store verfügbar. Möchten Sie jetzt updaten?", message: nil, cancelBtn: true, okBtnTitle: "Updaten", okBtnCompletion: {

var popupTitle = "Eine neue Version der First Class & More Reisedeals-App ist im App Store verfügbar. Bitte führen Sie das Update jetzt durch, um die App weiter nutzen zu können."

if !isForced {
popupTitle = "Eine neue Version der First Class & More Reisedeals-App ist im App Store verfügbar. Möchten Sie jetzt updaten?"
}

topController.showPopupDialog(title: popupTitle,
message: nil,
cancelBtn: !isForced,
okBtnTitle: "Updaten",
okBtnCompletion: {

if let url = URL(string: "itms-apps://itunes.apple.com/app/first-class-more-reisedeals/id1474514915"),
UIApplication.shared.canOpenURL(url) {
Expand Down
7 changes: 6 additions & 1 deletion First Class And More/UIViewController+Ext.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,11 @@ extension UIViewController: NVActivityIndicatorViewable {
cancelBtnAppearance.titleFont = UIFont(name: "Roboto-Regular", size: 14.0)!
cancelBtnAppearance.titleColor = fcamDarkGold
// creating popup
let popup = PopupDialog(title: title, message: message)
let popup = PopupDialog(title: title,
message: message,
tapGestureDismissal: cancelBtn,
panGestureDismissal: cancelBtn)

popup.buttonAlignment = .horizontal
popup.transitionStyle = .bounceUp
let okBtn = DefaultButton(title: okBtnTitle ?? "OK") {
Expand All @@ -41,6 +45,7 @@ extension UIViewController: NVActivityIndicatorViewable {
let cancelBtn = CancelButton(title: "ABBRECHEN".uppercased(), action: nil)
popup.addButtons([cancelBtn, okBtn])
} else {
okBtn.dismissOnTap = false
popup.addButtons([okBtn])
}
present(popup, animated: true, completion: nil)
Expand Down

0 comments on commit 0a777b0

Please sign in to comment.