[Swift通天遁地]5、高級擴展-(1)快速檢測設備屬性:版本、類型、屏幕尺寸

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(shanqingyongzhi)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:http://www.javashuo.com/article/p-vezqquke-kx.html 
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html

目錄:[Swift]通天遁地Swiftios

本文將演示如何快速檢測設備的各類屬性。git

首先確保在項目中已經安裝了所需的第三方庫。github

點擊【Podfile】,查看安裝配置文件。swift

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】項目文件。ide

在項目導航區,打開視圖控制器的代碼文件【ViewController.swift】post

如今開始編寫代碼,快速檢測設備的各類屬性。spa

  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 }
相關文章
相關標籤/搜索