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

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

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

点击【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 }

原文地址:https://www.cnblogs.com/strengthen/p/10231336.html

时间: 2024-08-27 08:42:57

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

[Swift通天遁地]五、高级扩展-(8)ImageView(图像视图)的各种扩展方法

本文将演示图像和图像视图在下载.压缩.裁剪方面的扩展. 首先确保在项目中已经安装了所需的第三方库. 点击[Podfile],查看安装配置文件. 1 platform :ios, '12.0' 2 use_frameworks! 3 4 target 'DemoApp' do 5 source 'https://github.com/CocoaPods/Specs.git' 6 pod 'EZSwiftExtensions' 7 end 根据配置文件中的相关配置,安装第三方库. 然后点击打开[De

[Swift通天遁地]四、网络和线程-(13)创建一个Socket客户端

请点击Socket服务端文章:[Swift通天遁地]四.网络和线程-(14)创建一个Socket服务端 本文将演示Socket(套接字)客户端的使用. 网络上的两个程序通过一个双向的通信连接实现数据的交换,这个连接的一端称为一个socket. 在Github中下载项目:[SwiftSocket] 在[Source]文件夹,按下[Shift]选择多个文件. [yudpsocket.c] [ytcpsocket.c] [UDPClient.swift] [TCPClient.swift] [Swif

[Swift通天遁地]七、数据与安全-(18)使用Swift实现原生的MD5加密

本文将演示如何使用Swift实现原生的MD5加密. 首先创建一个桥接头文件,因为需要使用到OC语言的通用加密解密类库. 在项目文件夹[DemoApp]上点击鼠标右键,弹出右键菜单. [New File]->[Header File]->[Next]->[Save As]:Header.h->[Create] 在该文件中,添加需要引用到的框架. 1 //添加需要引用到的框架. 2 #ifndef _4_1_2SecurityProject_MD5_Bridging_Header_h

[Swift通天遁地]七、数据与安全-(19)使用Swift实现原生的SHA1加密

本文将演示如何使用Swift实现原生的SHA1加密. 首先创建一个桥接头文件,因为需要使用到OC语言的通用加密解密类库. 在项目文件夹[DemoApp]上点击鼠标右键,弹出右键菜单. [New File]->[Header File]->[Next]->[Save As]:Header.h->[Create] 在该文件中,添加需要引用到的框架. 1 //添加需要引用到的框架 2 #ifndef _4_1_2SecurityProject_SHA1_Bridging_Header_h

[Swift通天遁地]九、拔剑吧-(13)创建页面的景深视差滚动效果

景深视差经常被应用在游戏项目中. 本文将演示创建一个简单的景深视差滚动效果 首先确保已经安装了所需的第三方类库.双击查看安装配置文件[Podfile] 1 platform :ios, '12.0' 2 use_frameworks! 3 4 target 'DemoApp' do 5 source 'https://github.com/CocoaPods/Specs.git' 6 pod 'Presentation' 7 end 根据配置文件中的相关设置,安装第三方类库. 安装完成之后,双击

[Swift通天遁地]一、超级工具-(5)使用UIWebView(网页视图)加载本地页面并调用JavaScript(脚本)代码

本文将演示如何使用UIWebView(网页视图)读取项目中的网页文件,以及执行JavaScript脚本代码. 在项目文件夹[DemoApp]上点击鼠标右键,弹出右键菜单. [New File]->[Blank]空白模板->[next] ->[Save As]:Register.html->[Create] 在Register.html中输入网页代码: 1 <!DOCTYPE HTML> 2 <html> 3 <head> 4 <meta h

[Swift通天遁地]一、超级工具-(10)使用地图视图MKMapView的相机功能实现创建三维地图

本文将演示使用地图视图MKMapView的相机功能实现创建三维地图. 在项目导航区,打开视图控制器的代码文件[ViewController.swift] 1 import UIKit 2 //在当前的类文件中引入所需的类库 3 import MapKit 4 5 class ViewController: UIViewController { 6 7 override func viewDidLoad() { 8 super.viewDidLoad() 9 // Do any additiona

[Swift通天遁地]二、表格表单-(3)在表格中嵌套另一个表格并使Cell的高度自适应

本文将演示如何在表格中嵌套另一个表格并使Cell的高度自适应,创建更加强大的布局效果. 在项目文件夹[DemoApp]上点击鼠标右键,弹出右键菜单. [New File]->[Cocoa Touch Class]->[Next]-> [Class]:CustomizeUITableViewCell ,类名. [Subclass of]:UITableViewCell ,父类 [Language]:Swift ->[Next]->[Create]在项目导航区,打开刚刚创建的代码

[Swift通天遁地]八、媒体与动画-(4)给相机添加CoreImage滤镜效果

本文将演示如何给相机添加实时的滤镜效果. 首先打开项目的配置文件[Info.plist],在空白区域点击鼠标右键,弹出右键菜单. 选择[Add Row]添加行命令,添加一行配置选项. 在[Key]键输入框输入相机的访问标识:[Application Category] 在[Value]值输入框输入当应用程序访问相机设备时的提示语: [Requires access to the camera] 在左侧的项目导航区,打开视图控制器的代码文件[ViewController.swift] 现在开始编写