4 app版本号 swift_swift app版本比较 - CSDN
  • let appVersion = Bundle.main.infoDictionary!["CFBundleShortVersionString"] as! String let appName = Bundle.main.infoDictionary!["CFBundleDisplayName"] as!...链接: iOS 获取当前app的名称和版本号 ...
    let appVersion = Bundle.main.infoDictionary!["CFBundleShortVersionString"] as! String
            let appName = Bundle.main.infoDictionary!["CFBundleDisplayName"] as! String
    

    参考博客:
    链接: iOS 获取当前app的名称和版本号

    展开全文
  • I have an IOS app with an Azure back-end, and would like to log certain events, like logins and whi

    本文翻译自:How do I get the App version and build number using Swift?

    I have an IOS app with an Azure back-end, and would like to log certain events, like logins and which versions of the app users are running. 我有一个带有Azure后端的IOS应用程序,并且想记录某些事件,例如登录名和正在运行的应用程序版本。

    How can I return the version and build number using Swift? 如何使用Swift返回版本和内部版本号?


    #1楼

    参考:https://stackoom.com/question/1kwkB/如何使用Swift获取App版本和内部版本号


    #2楼

    EDIT 编辑

    Updated for Swift 4.2 为Swift 4.2更新

    let appVersion = Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String
    

    EDIT 编辑

    As pointed out by @azdev on the new version of Xcode you will get a compile error for trying my previous solution, to solve this just edit it as suggested to unwrap the bundle dictionary using a ! 正如@azdev在新版本的Xcode上指出的那样,您尝试我以前的解决方案时会遇到编译错误,要解决该问题,只需按照建议的方法对其进行编辑即可使用!来解开bundle字典。

    let nsObject: AnyObject? = NSBundle.mainBundle().infoDictionary!["CFBundleShortVersionString"]
    

    End Edit 结束编辑

    Just use the same logic than in Objective-C but with some small changes 只需使用与Objective-C相同的逻辑,但有一些小的更改

    //First get the nsObject by defining as an optional anyObject
    let nsObject: AnyObject? = NSBundle.mainBundle().infoDictionary["CFBundleShortVersionString"]
    
    //Then just cast the object as a String, but be careful, you may want to double check for nil
    let version = nsObject as String
    

    I hope this helps you out. 我希望这能够帮到你。

    David 大卫


    #3楼

    I know this has already been answered but personally I think this is a little cleaner: 我知道已经回答了这个问题,但就我个人而言,我认为这更干净一些:

    Swift 3.0: Swift 3.0:

     if let version = Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String {
        self.labelVersion.text = version
    }
    

    Swift <2.3 迅捷<2.3

    if let version = NSBundle.mainBundle().infoDictionary?["CFBundleShortVersionString"] as? String {
        self.labelVersion.text = version
    }
    

    This way, the if let version takes care of the conditional processing (setting the label text in my case) and if infoDictionary or CFBundleShortVersionString are nil the optional unwrapping will cause the code to be skipped. 这样,if let版本负责条件处理(在我的情况下设置标签文本),并且如果infoDictionary或CFBundleShortVersionString为nil,则可选的展开将导致代码被跳过。


    #4楼

    I also know this has already been answered but I wrapped up the previous answers: 我也知道这已经得到回答,但是我总结了以前的答案:

    (*)Updated for extensions (*)已更新扩展名

    extension Bundle {
        var releaseVersionNumber: String? {
            return infoDictionary?["CFBundleShortVersionString"] as? String
        }
        var buildVersionNumber: String? {
            return infoDictionary?["CFBundleVersion"] as? String
        }
        var releaseVersionNumberPretty: String {
            return "v\(releaseVersionNumber ?? "1.0.0")"
        }
    }
    

    Usage: 用法:

    someLabel.text = Bundle.main.releaseVersionNumberPretty
    

    @Deprecated: Old answers @不推荐使用:旧答案

    Swift 3.1 : Swift 3.1

    class func getVersion() -> String {
        guard let version = Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String else {
            return "no version info"
        }
        return version
    }
    

    For older versions : 对于旧版本

    class func getVersion() -> String {
        if let version = NSBundle.mainBundle().infoDictionary?["CFBundleShortVersionString"] as? String {
            return version
        }
        return "no version info"
    }
    

    So if you want to set label text or want to use somewhere else; 因此,如果您要设置标签文本或要在其他地方使用,请执行以下操作:

    self.labelVersion.text = getVersion()
    

    #5楼

    Updated for Swift 3.0 为Swift 3.0更新

    The NS -prefixes are now gone in Swift 3.0 and several properties/methods have changed names to be more Swifty. NS前缀现在在Swift 3.0中消失了,并且一些属性/方法已将名称更改为更Swifty。 Here's what this looks like now: 这是现在的样子:

    extension Bundle {
        var releaseVersionNumber: String? {
            return infoDictionary?["CFBundleShortVersionString"] as? String
        }
        var buildVersionNumber: String? {
            return infoDictionary?["CFBundleVersion"] as? String
        }
    }
    
    Bundle.main.releaseVersionNumber
    Bundle.main.buildVersionNumber
    

    Old Updated Answer 旧的更新答案

    I've been working with Frameworks a lot since my original answer, so I wanted to update my solution to something that is both simpler and much more useful in a multi-bundle environment: 自从我最初的回答以来,我就一直在使用Frameworks,所以我想将我的解决方案更新为在多捆绑环境中既简单又有用的解决方案:

     extension NSBundle { var releaseVersionNumber: String? { return self.infoDictionary?["CFBundleShortVersionString"] as? String } var buildVersionNumber: String? { return self.infoDictionary?["CFBundleVersion"] as? String } } 

    Now this extension will be useful in apps to identify both the main bundle and any other included bundles (such as a shared framework for extension programming or third frameworks like AFNetworking), like so: 现在,此扩展将在应用程序中有用,以识别主捆绑包和任何其他包含的捆绑包(例如,用于扩展编程的共享框架或诸如AFNetworking的第三种框架),如下所示:

     NSBundle.mainBundle().releaseVersionNumber NSBundle.mainBundle().buildVersionNumber // or... NSBundle(URL: someURL)?.releaseVersionNumber NSBundle(URL: someURL)?.buildVersionNumber 

    Original Answer 原始答案

    I wanted to improve on some of the answers already posted. 我想改善已经发布的一些答案。 I wrote a class extension that can be added to your tool chain to handle this in a more logical fashion. 我写了一个类扩展,可以将其添加到您的工具链中,以更合乎逻辑的方式进行处理。

     extension NSBundle { class var applicationVersionNumber: String { if let version = NSBundle.mainBundle().infoDictionary?["CFBundleShortVersionString"] 

    as? 如? String { return version } return "Version Number Not Available" } 字符串{返回版本}返回“版本号不可用”}

     class var applicationBuildNumber: String { if let build = NSBundle.mainBundle().infoDictionary?["CFBundleVersion"] as? String { return build } return "Build Number Not Available" } } 

    So now you can access this easily by: 因此,现在您可以通过以下方式轻松访问此地址:

     let versionNumber = NSBundle.applicationVersionNumber 

    #6楼

    For Swift 1.2 it's: 对于Swift 1.2,它是:

    let version = NSBundle.mainBundle().infoDictionary!["CFBundleShortVersionString"] as! String
    let build = NSBundle.mainBundle().infoDictionary!["CFBundleVersion"] as! String
    
    展开全文
  • private func checkUpdate() { let path = NSString(format: "http://itunes.apple.com/cn/lookup?id=%@", AppConfig.appStoreID) as String let url = NSURL(string: path) let request =
    private func checkUpdate() {
            let path = NSString(format: "http://itunes.apple.com/cn/lookup?id=%@", AppConfig.appStoreID) as String
            let url = NSURL(string: path)
            let request = NSMutableURLRequest(URL: url!, cachePolicy: NSURLRequestCachePolicy.ReloadIgnoringLocalAndRemoteCacheData, timeoutInterval: 10.0)
            request.HTTPMethod = "POST"
            
            NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue()) { (response, data, error) in
                let receiveStatusDic = NSMutableDictionary()
                if data != nil {
                    do {
                        let dic = try NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.MutableContainers)
                        if let resultCount = dic["resultCount"] as? NSNumber {
                            if resultCount.integerValue > 0 {
                                receiveStatusDic.setValue("1", forKey: "status")
                                if let arr = dic["results"] as? NSArray {
                                    if let dict = arr.firstObject as? NSDictionary {
                                        if let version = dict["version"] as? String {
                                            receiveStatusDic.setValue(version, forKey: "version")
                                            NSUserDefaults.standardUserDefaults().setObject(version, forKey: "Version")
                                            NSUserDefaults.standardUserDefaults().synchronize()
                                        }
                                    }
                                }
                            }
                        }
                    }catch let error {
                        log.debug("checkUpdate -------- \(error)")
                        receiveStatusDic.setValue("0", forKey: "status")
                    }
                }else {
                    receiveStatusDic.setValue("0", forKey: "status")
                }
                self.performSelectorOnMainThread(#selector(AppDelegateSplash.checkUpdateWithData(_:)), withObject: receiveStatusDic, waitUntilDone: false)
            }
        }
        
        @objc private func checkUpdateWithData(data: NSDictionary) {
            let status = data["status"] as? String
            let localVersion = NSBundle.mainBundle().objectForInfoDictionaryKey("CFBundleShortVersionString") as! String
            if status == "1" {
                let storeVersion = data["version"] as! String
                self.compareVersion(localVersion, storeVersion: storeVersion)
                return
            }
            if let storeVersion = NSUserDefaults.standardUserDefaults().objectForKey("Version") as? String {
                self.compareVersion(localVersion, storeVersion: storeVersion)
            }
        }
        
        private func compareVersion(localVersion: String, storeVersion: String) {
            if localVersion.compare(storeVersion) == NSComparisonResult.OrderedAscending {
                //做你想做的事情
            }
        }


    注意,从APPStore获取信息不是每次都能获取成功的,所以需要在本地做个缓存,只要有一次成功就记住版本号

    展开全文
  • CFBundleShortVersionString(Version):真正的APP版本号,即应用程序的迭代版本; APP Store 所识别到的版本号。发布版本号是三个时期分离整数组成的字符串。例如 1.0.1。第一个整数表示应用程序的主版本号,表示...

    CFBundleShortVersionString(Version) :真正的APP版本号,即应用程序的迭代版本;

    APP Store 所识别到的版本号。发布版本号是三个时期分离整数组成的字符串。例如 1.0.1。第一个整数表示应用程序的主版本号,表示实现新功能或重大变化。第二个整数代表次版本号,表示实现不太突出的功能。第三个整数代表修正版本号,表示维护已发行版本。

    CFBundleVersion(Build) :指定包的构建版本号,是Xcode里的一个构造版本,自己内侧的小版本号

    方法1:连接国外的服务器获取,会出现延迟或 请求的版本号不稳定,与刚刚发布的版本号不一致

    用 "https://itunes.apple.com/lookup?id=" 获取版本号

    方法2:使用 "https://itunes.apple.com/cn/lookup?id=" 获取版本号

    注意:用"https://itunes.apple.com/lookup?id=" 获取版本号,如果上架只选中国,怎resultCount 为 0,获取不到版本数据

    展开全文
  • Swift3.0获取APP版本号,提示用户升级
  • 1.获取appstore版本号 func checkVersionTapGestrue(sender: UITapGestureRecognizer){ Alamofire.request("https://itunes.apple.com/lookup?bundleId=你的bundleId", method: .get, pa...
  • 获取AppStore中App的相关信息:  使用蒲公英发布包的时候,我们如果查看应用信息,会看到和应用显示在AppStore上类似的介绍...《ios 获取appstore 版本号》   获取手机上app信息或设备信息可参考: 《iOS获取...
  • 1、从App Store获取版本号 func appStoreVersion(appId: String) { let config = URLSessionConfiguration.default let session = URLSession(configuration: config) let url = URL(string: "h...
  • 获取设备名称 OC代码 NSString *deviceName = [[UIDevice currentDevice] name]; Swift代码 ...let deviceName = UIDevice....获取系统版本号 OC代码 NSString *sysVersion = [[UIDevice currentDevice...
  • 本文参考http://www.cnblogs.com/YYSheng/p/5550127.html获取设备名称OC代码NSString *deviceName = [...Swift代码let deviceName = UIDevice.current.name获取系统版本号OC代码NSString *sysVersion = [[UIDevi
  • //获取手机程序的版本号 NSString *ver = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"]; NSString *appId = @"1268501964";//在开发者账号中...
  • swift 获取app版本号及 ios 版本号 func versionCheck(){ let infoDictionary = NSBundle.mainBundle().infoDictionary let appDisplayName: AnyObject? = infoDictionary!["CFBundleDisplayName"]...
  • 抓包获取app版本没有versionCode,只要versionName,手动解析versionName判断app是否有新版本,能够识别以下几种格式: "1.1.0" "1.1.0-alpha" "1.1.0rc" "1.1.0" "7.59.1 build 27 34903" "1_46_0" "4.6.4(#64)" ...
  • iOS app版本更新CheckVersion_Swift 前言 CheckVersion_Swift是一款轻量级的Swift版本的框架,框架功能:根据Apple提供的API去请求JSON,根据JSON中的版本号决定是否去更新原生App。更新提示的内容可以自定义,也...
  • Swift获取应用版本号:version 1、获取本地版本号 /// 获取本地版本号 func getLocalVersion() -> String { var localVersion:String = "" //当前版本 if let v:String = Bundle.main.object...
  • Swift 获取版本号

    2017-05-10 11:01:45
    1.简介iOS的版本号是Version,还有一个叫做Build,这两个值都可以在Xcode ... Version在plist文件中的key是“CFBundleShortVersionString”,和AppStore上的版本号保持一致。 2. Bulid Build在plist中的key是“CF
  • //联系人:石虎 QQ:1224614774 昵称:嗡嘛呢叭咪哄 /**  1.图形显示  2.代码实现  3.下载 demo  */ 一、图形展示 图1: 图2: 二、代码实现 // // ViewController.m ...// Cr
  • NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary]; CFShow(infoDictionary); // app名称 NSString *app_Name = [infoDictionary objectForKey:@"CFBundleDisplayName"];... // app版本
1 2 3 4 5 ... 20
收藏数 4,171
精华内容 1,668
关键字:

4 app版本号 swift