美文网首页iOS TableView 优化
swift AppStore版本与本地版本比较

swift AppStore版本与本地版本比较

作者: 屈涯 | 来源:发表于2019-03-25 17:50 被阅读0次

    调用

            //use
            VersionCheckManager.checkVersion(appId: "836500024") { (compareResult) -> Void in
    
                DDLogDebug("\n\(compareResult)\n")
            }
    

    封装

    
    import UIKit
    import Alamofire
    
    class VersionCheckManager: NSObject {
    
        // MARK: - check to see if you need to update the version
        // - Parameter appId: application appId
        // - Returns: true : The current version is lower than the store version
        // - false : The current version is equal to or higher than the store version
        static func checkVersion(appId:String,compareResult: @escaping (Bool) -> Void) {
    
            VersionCheckManager.appSoreVersion(appId: appId) { (appVersion) -> Void in
                let localVersion : String = VersionCheckManager.currentVersion()
                let result : Bool = VersionCheckManager().versionCompare(localVersion: localVersion, appStoreVersion: appVersion)
                compareResult(result)
            }
        }
    
        // MARK: - get app version on the AppStore
        // - Parameter appId:  application appId
        // - Returns: store version
        static func appSoreVersion(appId:String, handle: @escaping ((String) -> Void)) {
            let urlString = "http://itunes.apple.com/lookup?id=" + appId
            Alamofire.request(urlString, method: .get, parameters: [:])
                .responseJSON { (response) in
    
                    if let value = response.result.value {
                        let appMsgDict:Dictionary = value as? Dictionary ?? [:]
                        let appResultsArray:NSArray = (appMsgDict["results"] as? NSArray)!
                        let appResultsDict:NSDictionary = appResultsArray.lastObject as? NSDictionary ?? ["":""]
                        guard let appStoreVersion = appResultsDict["version"] as? String else {
                            return
                        }
                        handle(appStoreVersion)
                    }
            }
        }
    
        // MARK: - Get the current version well
        // - Returns: current version
        static func currentVersion() -> String {
    
            guard let localVersion = Bundle.main.infoDictionary!["CFBundleShortVersionString"] as? String else {
                return ""
            }
            return localVersion
        }
    
        // MARK: - compares the store version number to the current version number
        // - Parameter localVersion: currentVersion  appStoreVersion: appSoreVersion
        // - Returns: The current version is equal to or higher than the store version
       func versionCompare(localVersion:String,appStoreVersion:String) -> Bool {
            if checkSeparat(versionString: localVersion) == "" || checkSeparat(versionString: appStoreVersion) == ""{
                DDLogDebug("\n\("Only support '.''-'/''*'_'delimiter")\n")
                return false
            }
            guard let localVersionArray = cutUpNumber(vString: localVersion) as? [String] else { return false}
            guard let storeVersionArray = cutUpNumber(vString: appStoreVersion) as? [String] else {return false}
            return compareNumber(localArray: localVersionArray, storeArray: storeVersionArray)
        }
    
        // MARK: - extract link
        // - Parameter  versionString : version
        // - Returns: version
        func checkSeparat(versionString:String) -> String {
            var separated:String = ""
            if versionString.contains(".") {separated = "."}
            if versionString.contains("-") {separated = "-"}
            if versionString.contains("/") {separated = "/"}
            if versionString.contains("*") {separated = "*" }
            if versionString.contains("_") {separated = "_" }
    
            return separated
        }
    
        // MARK: - extract each individual version number
        // - Parameter vString: version
        // - Returns: version array
        func cutUpNumber(vString:String) -> NSArray {
            let  separat = checkSeparat(versionString: vString)
            let versionChar = NSCharacterSet(charactersIn:separat) as CharacterSet
            let vStringArr = vString.components(separatedBy: versionChar)
            return vStringArr as NSArray
        }
    
        // MARK: - compare the store version to the current version
        // - Parameter appId: application appId
        // - Returns: true : The current version is lower than the store version
        // - false : The current version is equal to or higher than the store version
        func compareNumber(localArray:[String],storeArray:[String]) -> Bool {
            for version in 0..<localArray.count {
                if  Int(localArray[version])! != Int(storeArray[version])! {
                    if Int(localArray[version])! > Int(storeArray[version])! {
                        return true
                    } else {
                        return false
                    }
                }
            }
            return false
        }
    }
    
    

    相关文章

      网友评论

        本文标题:swift AppStore版本与本地版本比较

        本文链接:https://www.haomeiwen.com/subject/cfvbvqtx.html