• [Swift通天遁地]五、高级扩展-(1)快速检测设备属性:版本、类型、屏幕尺寸


    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
    ➤微信公众号:山青咏芝(shanqingyongzhi)
    ➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/
    ➤GitHub地址:https://github.com/strengthen/LeetCode
    ➤原文地址:https://www.cnblogs.com/strengthen/p/10231336.html 
    ➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
    ➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

    目录:[Swift]通天遁地Swift

    本文将演示如何快速检测设备的各种属性。

    首先确保在项目中已经安装了所需的第三方库。

    点击【Podfile】,查看安装配置文件。

    1 platform :ios, '12.0'
    2 use_frameworks!
    3 
    4 target 'DemoApp' do
    5     source 'https://github.com/CocoaPods/Specs.git'
    6     pod "Device", '~> 2.0.0'
    7 end

    根据配置文件中的相关配置,安装第三方库。

    然后点击打开【DemoApp.xcworkspace】项目文件。

    在项目导航区,打开视图控制器的代码文件【ViewController.swift】

    现在开始编写代码,快速检测设备的各种属性。

      1 import UIKit
      2 //在当前的类文件中,引入已经安装的第三方类库
      3 import Device
      4 
      5 class ViewController: UIViewController {
      6 
      7     override func viewDidLoad() {
      8         super.viewDidLoad()
      9         // Do any additional setup after loading the view, typically from a nib.
     10         //获取设备的版本信息
     11         getDeveiceVersion()
     12         //获取屏幕的尺寸
     13         getScreenSize()
     14         //获得设备的类型
     15         getDeveiceType()
     16         //检测设备的屏幕
     17         deviceHelpers()
     18     }
     19     
     20     //添加一个方法,用来获取设备的版本信息
     21     func getDeveiceVersion()
     22     {
     23         //对设备的版本号进行遍历,在控制台输出相应的信息。
     24         //第三方类库支持对最新版本手机的检测,
     25         //当苹果发布新设备时,请及时更新第三方类库。
     26         switch Device.version()
     27         {
     28             //手机
     29             case .iPhone4:       print("It's an iPhone 4")
     30             case .iPhone4S:      print("It's an iPhone 4S")
     31             case .iPhone5:       print("It's an iPhone 5")
     32             case .iPhone5C:      print("It's an iPhone 5C")
     33             case .iPhone5S:      print("It's an iPhone 5S")
     34             case .iPhone6:       print("It's an iPhone 6")
     35             case .iPhone6S:      print("It's an iPhone 6S")
     36             case .iPhone6Plus:   print("It's an iPhone 6 Plus")
     37             case .iPhone6SPlus:  print("It's an iPhone 6 S Plus")
     38             case .iPhone7:       print("It's an iPhone 7")
     39             case .iPhone7Plus:   print("It's an iPhone 7 Plus")
     40 
     41             //平板    
     42             case .iPad1:         print("It's an iPad 1")
     43             case .iPad2:         print("It's an iPad 2")
     44             case .iPad3:         print("It's an iPad 3")
     45             case .iPad4:         print("It's an iPad 4")
     46             //Air系列平板
     47             case .iPadAir:       print("It's an iPad Air")
     48             case .iPadAir2:      print("It's an iPad Air 2")
     49             //Mini平板
     50             case .iPadMini:      print("It's an iPad Mini")
     51             case .iPadMini2:     print("It's an iPad Mini 2")
     52             case .iPadMini3:     print("It's an iPad Mini 3")
     53             case .iPadMini4:     print("It's an iPad Mini 4")
     54             //Pro平板
     55             case .iPadPro:       print("It's an iPad Pro")
     56 
     57             //iPod设备    
     58             case .iPodTouch1Gen: print("It's a iPod touch generation 1")
     59             case .iPodTouch2Gen: print("It's a iPod touch generation 2")
     60             case .iPodTouch3Gen: print("It's a iPod touch generation 3")
     61             case .iPodTouch4Gen: print("It's a iPod touch generation 4")
     62             case .iPodTouch5Gen: print("It's a iPod touch generation 5")
     63             case .iPodTouch6Gen: print("It's a iPod touch generation 6")
     64             
     65             //处理设备为模拟器时的情况    
     66             case .Simulator:
     67                 print("It's a Simulator")
     68             //处理无法识别设备时的情况
     69             default:
     70                 print("It's an unknown device")
     71         }
     72     }
     73     
     74     //添加一个方法,用来获取屏幕的尺寸
     75     func getScreenSize()
     76     {
     77         //遍历设备的尺寸信息
     78         switch Device.size()
     79         {
     80             case .screen3_5Inch:
     81                 print("It's a 3.5 inch screen")
     82             case .screen4Inch:
     83                 print("It's a 4 inch screen")
     84             case .screen4_7Inch:
     85                 print("It's a 4.7 inch screen")
     86             case .screen5_5Inch:
     87                 print("It's a 5.5 inch screen")
     88             case .screen7_9Inch:
     89                 print("It's a 7.9 inch screen")
     90             case .screen9_7Inch:
     91                 print("It's a 9.7 inch screen")
     92             case .screen12_9Inch:
     93                 print("It's a 12.9 inch screen")
     94             //检测不到设备,输出相应信息
     95             default:
     96                 print("Unknown size")
     97         }
     98     }
     99     
    100     //添加一个方法,用来获得设备的类型
    101     func getDeveiceType()
    102     {
    103         //对设备的所有类型进行遍历
    104         switch Device.type()
    105         {
    106             //音乐
    107             case .iPod:
    108                 print("It's an iPod")
    109             //手机
    110             case .iPhone:
    111                 print("It's an iPhone")
    112             //平板
    113             case .iPad:
    114                 print("It's an iPad")
    115             //模拟器
    116             case .Simulator:
    117                 print("It's a Simulated device")
    118             //检测失败
    119             default:
    120                 print("Unknown device type")
    121         }
    122     }
    123     
    124     //添加一个方法,用来检测设备的屏幕
    125     func deviceHelpers()
    126     {
    127         //检测设备的屏幕是否等于4寸
    128         if Device.isEqualToScreenSize(Size.screen4Inch)
    129         {
    130             print("It's a 4 inch screen")
    131         }
    132         
    133         //检测设备的屏幕是否大于4.7寸
    134         if Device.isLargerThanScreenSize(Size.screen4_7Inch)
    135         {
    136             print("Your device screen is larger than 4.7 inch")
    137         }
    138         
    139         //检测设备的屏幕是否小于4.7寸
    140         if Device.isSmallerThanScreenSize(Size.screen4_7Inch)
    141         {
    142             print("Your device screen is smaller than 4.7 inch")
    143         }
    144         
    145         ////检测设备的屏幕是否拥有高清屏幕
    146         if Device.isRetina()
    147         {
    148             print("It's a retina display")
    149         }
    150     }
    151     
    152     override func didReceiveMemoryWarning() {
    153         super.didReceiveMemoryWarning()
    154         // Dispose of any resources that can be recreated.
    155     }
    156 }
  • 相关阅读:
    PDB文件详解
    C++模板常用功能讲解
    Windows下多线程编程(二)
    关于静态库中使用全局变量可能导致的问题
    js中的函数
    js中字符串的加密base64
    列表推导式
    函数和方法的区别
    xshell连不上虚拟机
    网络编程,并行,并发和协程
  • 原文地址:https://www.cnblogs.com/strengthen/p/10231336.html
Copyright © 2020-2023  润新知