Skip to content

Instantly share code, notes, and snippets.

@shinrikiken
Created April 12, 2017 05:45
Show Gist options
  • Save shinrikiken/838478afd3dcaf727b6f8ce1ec8d0538 to your computer and use it in GitHub Desktop.
Save shinrikiken/838478afd3dcaf727b6f8ce1ec8d0538 to your computer and use it in GitHub Desktop.
CheckAppVersion
//
// CheckAppVersion.swift
// ENEOSPOS
//
// Created by Than Htike Aung on 12/2/16.
// Copyright © 2016 CODIUM. All rights reserved.
//
import UIKit
class CheckAppVersion: NSObject {
static var sharedInstance = CheckAppVersion()
var isChecking = false
func canCheck() -> Bool {
let check = self.isChecking
if !check {
self.isChecking = !check
}
return check
}
func checkingAppVersion() {
if !self.canCheck() {
let appIdentifier = MTConfiguration.appIdentifier()
let identifier = appIdentifier.componentsSeparatedByString(".")
let appUrl = "\(identifier[identifier.count - 2])-\(identifier[identifier.count - 1])"
NSLog("check app version URL : \(appUrl), uuid : \(Utils.deviceUUID())")
let manager = AFHTTPSessionManager()
manager.GET("\(Configurations.shared.BaseURL)\(EndpointURLs.appLatest)\(appUrl)/latest/", parameters: nil, success: {(task: NSURLSessionDataTask, obj: AnyObject?) -> Void in
let responseObject = obj as! NSDictionary
self.isChecking = false
let version = responseObject["version"] as! NSString
// different version
if version.isNewerThanVersion(Utils.getStoreAppVersion()) {
let alert = UIAlertController(title: NSLocalizedString("Attention", comment: "Attention"), message: NSLocalizedString("There is new App Version", comment: "There is new App Version"), preferredStyle: UIAlertControllerStyle.Alert)
let cancelAction = UIAlertAction(title: NSLocalizedString("Cancel", comment: "Cancel"), style: UIAlertActionStyle.Default, handler: nil)
let okAction = UIAlertAction(title: NSLocalizedString("Install", comment: "Install"), style: UIAlertActionStyle.Default, handler: { action in
let url = responseObject["manifest"] as! String
NSLog("url : \(url)")
let itmsUrl = "itms-services://?action=download-manifest&url=\(url)"
NSLog("itmsUrl : \(itmsUrl)")
UIApplication.sharedApplication().openURL(NSURL(string: itmsUrl)!)
})
alert.addAction(cancelAction)
alert.addAction(okAction)
UIApplication.sharedApplication().keyWindow?.rootViewController!.presentViewController(alert, animated: true, completion: nil)
}
}, failure: {(task: NSURLSessionDataTask?, error: NSError) -> Void in
self.isChecking = false
let httpResponse = (task!.response as? NSHTTPURLResponse)
var errorCode = error.code
if httpResponse != nil {
errorCode = httpResponse!.statusCode
}
NSLog("request error : \(task!.currentRequest!.URL!.description), statusCode : \(errorCode)")
SBRANDLog.printFailureResponseToSentry(task, params: nil, error: error)
})
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment