有时我们在 App 中提交一些统计信息或者用户反馈信息时,为了能更好地进行分析,通常会附带上当前应用程序的名称、版本号、设备型号、以及设备系统版本。下面演示如何获取这些信息。

1,效果图

程序启动后自动获取相关的应用信息以及设备信息,并打印到控制台中。

2,样例代码

默认情况下我从 UIDevice 中获取设备型号时只能得到 iPhone、iPod Touch 这样笼统的类型数据。
这里对 UIDevice 做个扩展,使其可以得到具体的设备型号,比如:iPhone 6s Plus 这样准确的设备款式。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import UIKit
 
class ViewController: UIViewController {
 
    override func viewDidLoad() {
        super.viewDidLoad()
         
        //应用程序信息
        let infoDictionary = Bundle.main.infoDictionary!
        let appDisplayName = infoDictionary["CFBundleDisplayName"] //程序名称
        let majorVersion = infoDictionary["CFBundleShortVersionString"]//主程序版本号
        let minorVersion = infoDictionary["CFBundleVersion"]//版本号(内部标示)
        let appVersion = majorVersion as! String
         
        //设备信息
        let iosVersion = UIDevice.current.systemVersion //iOS版本
        let identifierNumber = UIDevice.current.identifierForVendor //设备udid
        let systemName = UIDevice.current.systemName //设备名称
        let model = UIDevice.current.model //设备型号
        let modelName = UIDevice.current.modelName //设备具体型号
        let localizedModel = UIDevice.current.localizedModel //设备区域化型号如A1533
         
        //打印信息
        print("程序名称:\(appDisplayName)")
        print("主程序版本号:\(appVersion)")
        print("内部版本号:\(minorVersion)")
        print("iOS版本:\(iosVersion)")
        print("设备udid:\(identifierNumber)")
        print("设备名称:\(systemName)")
        print("设备型号:\(model)")
        print("设备具体型号:\(modelName)")
        print("设备区域化型号:\(localizedModel)")
    }
 
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
}
 
 
//扩展UIDevice
extension UIDevice {
    //获取设备具体详细的型号
    var modelName: String {
        var systemInfo = utsname()
        uname(&systemInfo)
        let machineMirror = Mirror(reflecting: systemInfo.machine)
        let identifier = machineMirror.children.reduce("") { identifier, element in
            guard let value = element.value as? Int8, value != 0 else { return identifier }
            return identifier + String(UnicodeScalar(UInt8(value)))
        }
         
        switch identifier {
        case "iPod5,1":                                 return "iPod Touch 5"
        case "iPod7,1":                                 return "iPod Touch 6"
        case "iPhone3,1", "iPhone3,2", "iPhone3,3":     return "iPhone 4"
        case "iPhone4,1":                               return "iPhone 4s"
        case "iPhone5,1", "iPhone5,2":                  return "iPhone 5"
        case "iPhone5,3", "iPhone5,4":                  return "iPhone 5c"
        case "iPhone6,1", "iPhone6,2":                  return "iPhone 5s"
        case "iPhone7,2":                               return "iPhone 6"
        case "iPhone7,1":                               return "iPhone 6 Plus"
        case "iPhone8,1":                               return "iPhone 6s"
        case "iPhone8,2":                               return "iPhone 6s Plus"
        case "iPhone9,1":                               return "iPhone 7 (CDMA)"
        case "iPhone9,3":                               return "iPhone 7 (GSM)"
        case "iPhone9,2":                               return "iPhone 7 Plus (CDMA)"
        case "iPhone9,4":                               return "iPhone 7 Plus (GSM)"
             
        case "iPad2,1", "iPad2,2", "iPad2,3", "iPad2,4":return "iPad 2"
        case "iPad3,1", "iPad3,2", "iPad3,3":           return "iPad 3"
        case "iPad3,4", "iPad3,5", "iPad3,6":           return "iPad 4"
        case "iPad4,1", "iPad4,2", "iPad4,3":           return "iPad Air"
        case "iPad5,3", "iPad5,4":                      return "iPad Air 2"
        case "iPad2,5", "iPad2,6", "iPad2,7":           return "iPad Mini"
        case "iPad4,4", "iPad4,5", "iPad4,6":           return "iPad Mini 2"
        case "iPad4,7", "iPad4,8", "iPad4,9":           return "iPad Mini 3"
        case "iPad5,1", "iPad5,2":                      return "iPad Mini 4"
        case "iPad6,7", "iPad6,8":                      return "iPad Pro"
        case "AppleTV5,3":                              return "Apple TV"
        case "i386", "x86_64":                          return "Simulator"
        default:                                        return identifier
        }
    }
}

原文出自:www.hangge.com  转载请保留原文链接:http://www.hangge.com/blog/cache/detail_1606.html

Swift - 获取应用名称、应用版本、设备型号、系统版本等信息的更多相关文章

  1. iOS获取设备型号、设备类型等信息

    摘自 :http://www.mamicode.com/info-detail-1165460.html 设备标识 关于设备标识,历史上盛行过很多英雄,比如UDID.Mac地址.OpenUDID等,然 ...

  2. android API版本对应的系统版本及Android获取手机和系统版本等信息的代码

    学了这么久的Android,竟然一直对其API对应的名称关系一值搞不清楚,现在网上认真看了下资料,转载一个觉得写得不错的作者的文章,记下来: [背景] 之前折腾android期间,慢慢地知道了,And ...

  3. iOS开发-获取设备型号信息

    开发中有的时候查看设计统计数据,或者通过日志查看错误信息,这个时候我们就需要获取获取设备信息,看下关于设备有几种方法: NSLog(@"%@",[[UIDevice current ...

  4. iOS获取设备型号、装置类型等信息

    iOS获取设备型号.设备类型等信息 设备标识 关于设备标识,历史上盛行过很多英雄,比如UDID.Mac地址.OpenUDID等,然而他们都陆陆续续倒在了苹果的门下.苹果目前提供了2个方法供App获取设 ...

  5. php 读取windows 的系统版本,硬盘,内存,网卡,数据流量等

    php 读取windows 的系统版本,硬盘,内存,网卡,数据流量等 <?php header("Content-type: text/html; charset=utf-8" ...

  6. iOS获取设备型号和App版本号等信息(OC+Swift)

    iOS获取设备型号和App版本号等信息(OC+Swift) 字数1687 阅读382 评论3 喜欢10 好久没有写过博客了,因为中间工作比较忙,然后有些个人事情所以耽误了.但是之前写的博客还一直有人来 ...

  7. iOS 获取手机的型号,系统版本,软件名称,软件版本

    转载自:http://www.2cto.com/kf/201210/162333.html   网上搜索出来的,记录下来以后使用方便: [java]//手机序列号      NSString* ide ...

  8. IOS 获取最新设备型号方法

    1.IOS 获取最新设备型号方法列表最新对照表:http://theiphonewiki.com/wiki/Models方法: #import "sys/utsname.h” struct ...

  9. ?Swift获取手机设备信息

    使用UiDevice获取设备信息: 获取设备名称 let name = UIDevice.currentDevice().name 获取设备系统名称 let systemName = UIDevice ...

随机推荐

  1. 抽象类(abrstract class)与接口(interface)有何异同

    抽象类:如果一个类中包含抽象方法(用abstract修饰的方法),那么这个类就是抽象类 接口:是指一个方法的集合,接口中的所有方法都没有方法体 相同点: 1)都不能被实例化 2)接口的实现类或抽象类的 ...

  2. BPM不同表单之间子表的赋值

    上次写的是同一个表单的子表之间赋值,这次是不同表单之间子表的赋值 首先,我们给需要赋值的表单添加一个复制按钮 $.MvcSheet.AddAction({            Action: &qu ...

  3. Dalvik虚拟机和JVM的对比

    Dalvik虚拟机与Java虚拟机有着很多相似的特性,都支持GC,JIT,JNI等等.其主要区别在于文件格式以及指令集不同,下面对两者的特性进行比较与讨论. Difference1:文件格式 Dalv ...

  4. Oracle数据库实例

    数据库通常由两部分组成:数据库和数据库实例 数据库与实例的关系:数据库指的是:物理数据.数据库管理系统.即物理数据.内存.操作系统.用户访问Oracle都是访问一个实例,实例名指的是用于相应某个数据库 ...

  5. 关于安卓调用wcf的一些问题

    最近公司有个项目需要和别的系统做对接,对方开放的是webservice接口,搞了很久终于搞出来了,在此记录一下 获取数据的service public class SoapService implem ...

  6. JDBC+MYSQL初始学习

    JDBC+MYSQL初始学习 一.学习准备 Eclipse 开发工具  + mysql数据库+navicat 数据库连接工具 Mysql的数据库连接驱动jar包  + testing测试集成+mave ...

  7. sphinx with discuz

    安装sphinx: sudo apt-get install sphinxsearch 配置: source discuz { type = mysql sql_host = xx.xx.xx.xx ...

  8. Dynamics CRM 使用 Profiler 来做debug

    首先,我们需要install Profiler 我们选中一个plugin, 并且选择start Profilling 然后我们选择Persist to Entity 然后我们执行trigger这个pl ...

  9. Java Spring 两大特色

    0 引言 本文主要描述的是Spring常用的两大特色功能:AOP和IoC容器 1 IoC Spring的IoC:就是常说的“控制反转”,也又叫依赖注入的(DI). 优点:IoC最大的好处就是把对象生成 ...

  10. N1-1 - 树 - Minimum Depth of Binary Tree

    题目描述: Given a binary tree, find its minimum depth.The minimum depth is the number of nodes along the ...