即时获取 iOS 设备的详细信息(如类型、方向和 GPS 功能),以便在代码中做不同的事情!
SwiftDevice 是 Apple 的 UIDevice.currentDevice() 方法和属性的友好实现,并添加了一些关于您的设备的更详细信息。
我想找到一个更简单、更友好的方式来访问 iOS 设备的类型和方向,以便根据我使用的设备和如何持握设备来根据程序更改内容。因此,SwiftDevice 的 Device
应运而生。由于我不太关心设备是被横向持握还是纵向持握、或是否颠倒,所以我省略了这些选项,只用“纵向”和“横向”。
在使用这些函数后,我发现我还想了解我的设备是否支持 GPS、正在运行 iOS 操作系统的哪个版本、我的设备的名称等等。因此,我实现了其他方法来报告更多有趣的内容。
使用 SwiftDevice 无需初始化或声明。只需调用 Device
并通过 Device.[propery]
在需要获取设备信息的任何地方使用即可。
Device.type
- 设备的“物理类型”(例如,“平板”,“电视”)
TYPE:
Unspecified
Phone
Pad
TV
CarPlay
Device.orientation
- 设备的“屏幕方向”(例如,“横向”)
ORIENTATION:
Unknown
Portrait
Landscape
Device.orientationDetail
- 设备的[完全 Apple 指定的]“屏幕方向”(如果真的需要知道具体转向哪个方向,但这通常不重要)
UIDeviceOrientation:
Portrait
PortraitUpsideDown
LandscapeLeft
LandscapeRight
FaceUp
FaceDown
Unknown
Device.typeAndOrientation
- 设备的“物理类型和屏幕方向”(例如,“手机纵向”,“平板横向”)
TYPE_AND_ORIENTATION:
UnspecifiedPortrait
UnspecifiedLandscape
UnspecifiedUnknown
PhoneUnknown
PhonePortrait
PhoneLandscape
PadUnknown
PadPortrait
PadLandscape
TVUnknown
TVPortrait
TVLandscape
CarPlayUnknown
CarPlayPortrait
CarPlayLandscape
Device.batteryStatus
- 设备的“电池充电状态”(例如,“充电”,“未连接”);这取决于是否启用了电池监控器
BATTERY_STATUS:
Unknown
Charging
Full
Unplugged
Device.batteryLevel
- 设备的“当前电池电量”(例如 0.75,0.25,1.0)
Device.batteryMonitored
- “电池监控开启/关闭”状态;调用 Device.batteryMonitored
获取当前的布尔状态,或将设置为 true
或 false
以更改值
Device.proximityToUser
- 设备的接近传感器(如果启用)与用户的“接近程度”(例如“靠近用户”);这取决于接近监控器是否启用
PROXIMITY_TO_USER:
Unknown
CloseToUser
AwayFromUser
Device.proximityMonitored
- “ proximity monitoring 开关”状态;调用 Device.proximityMonitored
获取当前的布尔状态,或将其设置为 true
或 false
以更改值
Device.name
- 设备的“名称”,可在设置 > 一般 > 关于(例如,“Luke的iPad mini”)中找到
Device.model
- 设备的“型号”(例如,“iPod touch”)
Device.osName
- 设备的“操作系统名称”(例如,“iPhone OS”)
Device.osVersion
- 设备的“操作系统版本”(例如,“9.2”)
Device.os
- 设备的“操作系统名称和版本”(例如,“iPad OS 8.1”)
Device.idForVendor
- 设备的“供应商标识”字符串;基于App Store ID(例如,“AC295404-B9DA-4645-B13F-E89E36B94153”),对于非App Store应用,则为他者的包标识符(例如,“com.example.testapp”)
Device.hardwareIdentifier
- 设备的“硬件标识符”(例如,“iPad5,3”)
Device.hasGPS
- 设备的“GPS功能”(例如,“false”表示“没有GPS!”)
Device
使用不同的 .[value]
枚举可以直接与 Device.[ENUM_NAME].[value]
一起使用,但不是必需的。改用类型推理!
您可以使用这些类型作为已分配的变量
// infers variable is type Device.TYPE
let myDevice = Device.type
然后稍后按此方式访问它,无需 [something] == Device.TYPE.Phone
if myDevice == .Phone {
// do something great!
}
或使用类型推理直接按此方式访问它
if Device.proximityToUser == .CloseToUser {
// do something like shut the screen off -- they can't see the screen when their phone is against their ear!
}
Device
类与Apple的授权有什么不同?易于阅读和额外的功能!通常,如果您想了解您的设备类型或方向,您会这样写
// returns an enum like .LandscapeLeft or .PortraitUpsideDown
UIDevice.currentDevice().orientation
// returns an enum like .Pad or .Phone
UIDevice.currentDevice().UIInterfaceIdiom
使用 SwiftDevice 的 Device
对象,您可以替换为以下内容
// returns a simpler enum like .Landscape or .Portrait
Device.orientation
// still returns an enum like .Pad or .Phone
Device.type
添加的一个 优秀功能 是能够在同一个调用中同时获取类型和方向
// returns an enum like .PadLandscape or .PhonePortrait
Device.typeAndOrientation
关于“该设备内部是否有GPS芯片”的问题的答案又如何呢?
Device.hasGPS
获取当前设备的硬件标识符(例如,“iPhone1,1”)并将其与Apple的数据进行比较,指定该设备是否支持GPS;这是“基于ID的”GPS检查,与“基于实时硬件的”检查相对,这强制您启用CoreLocation并开始检查数字,这要复杂得多
// checks GPS capability of the current device's hardware identifier
if Device.hasGPS {
// use CoreLocation framework to do GPS stuff
} else {
// Disable in-app/game GPS features
// OR
// try and use CoreMotion framework to "emulate" GPS instead
}
示例:检查iPad是处于横屏模式用于布局GUI(因为自动布局不允许在iPad上为横屏和竖屏设置不同的约束,因为自动布局不允许在iPad上为横屏和竖屏设置不同的约束)
if Device.typeAndOrientation == .PadLandscape {
// do something different for an iPad in Landscape
} else {
// do yer default thang
}
示例:检查设备是处于横屏还是竖屏,然后控制是否在键盘推视图向上弹起时,使用自动布局显示顶层标志
@IBOutlet weak var logoImage: UIImageView! // some logo image at the top of your view
@IBOutlet weak var signInButton: UIButton! // outlet to the sign-in button in your view
...
override func viewDidLoad() {
// your keyboard notification observer initialization code
}
...
func yourKeyboardIsGoingToShowMethod(notification: NSNotification) {
if let keyboardSize = (notification.userInfo?[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.CGRectValue() {
let animationDuration = notification.userInfo?[UIKeyboardAnimationDurationUserInfoKey] as! Double
let keyboardTop = self.view.frame.height - keyboardSize.size.height
let signInButtonBottom = signInButton.frame.origin.y + signInButton.frame.height
let changeHeight = ceil(signInButtonBottom - keyboardTop) + keyboardMargin
if changeHeight > 0 {
if Device.typeAndOrientation == .PhoneLandscape {
logoImage.hidden = true // completely hide the image if using an iPhone in Lanscape
} else {
// do something like shrink the image the same amount the sign-in button needs to come up
}
self.view.frame.origin.y -= changeHeight / 2 // push the view up so the sign-in button sits on top of the keyboard
UIView.animateWithDuration(animationDuration, animations: { () -> Void in
self.view.layoutIfNeeded()
})
}
}
}
将其添加到您的Xcode项目的Podfile中
use_frameworks!
pod 'SwiftDevice', '0.3.0'
…将其安装到项目中
$ pod install
…通过.xcworkspace文件在Xcode中打开您的项目
$ open [YourProjectName].xcworkspace
…并将SwiftDevice框架导入到您想使用Device
的文件中
import SwiftDevice
SwiftDevice 由 Luke A Chase 创建并维护,邮箱:[[email protected]] | 位于 Github上的ChaseLukeA。