★UIDeviceオブジェクトを生成する
var device = UIDevice.current
var device: UIDevice = UIDevice.current
★モデル名を取得する
var modelName = UIDevice.current.model
//=> "iPhone","iPod touch","iPad"
★端末の名前を取得する
var name = UIDevice.current.name
//=> "xxxxx's iPhone6 Plus"
★システム名を取得する
var systemName = UIDevice.current.systemName
//=> "iPhone OS"
★システムバージョンを取得する
var systemVersion = UIDevice.current.systemVersion
//=> "8.1.2"
★バッテリーの残量を監視するかどうかの設定
// 監視する
device.batteryMonitoringEnabled = true
// 監視しない Default
device.batteryMonitoringEnabled = false
★バッテリー残量を取得する
// 0 .. 1.0
var batteryLevel = device.batteryLevel
//=> batteryLevel = 0.439999998
★バッテリー残量の変化を監視/取得する
NSNotificationCenter.current.addObserver(self,
selector: "batteryLevelDidChangeNotification:",
name: UIDeviceBatteryLevelDidChangeNotification,
object: nil)
// 通知を受けて呼ばれる関数
func batteryLevelDidChangeNotification(notification: NSNotification) {
var batteryLevel = UIDevice.currentDevice().batteryLevel
//=> batteryLevel = 0.949999988
}
★バッテリー充電状態を取得する
// batteryMonitoringEnabled = trueが必要
var batteryState = device.batteryState
//=> Charging
★バッテリー充電状態の変化を監視/取得する
NotificationCenter.default.addObserver(self,
selector: #selector(batteryStateDidChangeNotification(notification:)),
name: .UIDeviceBatteryStateDidChange,
object: nil)
// 通知を受けて呼ばれる関数
func batteryStateDidChangeNotification(notification: NSNotification) {
var batteryState: UIDeviceBatteryState = UIDevice.current.batteryState
//=> Full
}
★UIDeviceBatteryState
Unknown
Unplugged // on battery, discharging
Charging // plugged in, less than 100%
Full // plugged in, at 100%
★近接センサー有効/無効の設定
// 有効
device.proximityMonitoringEnabled = true
// 無効 Default
device.proximityMonitoringEnabled = false
★近接センサーの変化を監視
NSNotificationCenter.defaultCenter().addObserver(self,
selector: "proximitySensorStateDidChange:",
name: UIDeviceProximityStateDidChangeNotification,
object: nil)
// 通知を受けて呼ばれる関数
func proximitySensorStateDidChange(notification: NSNotification) {
if (UIDevice.current.proximityState == false) {
// 近接センサから離れた場合に実行
}
}
★近接センサー状態を取得
var state = device.proximityState
//=> true センサーに近づいている状態
//=> false センサーから離れている状態
★デバイスの向きの変化を監視/取得する
NSNotificationCenter.defaultCenter().addObserver(self,
selector: "OrientationDidChangeNotification:",
name: UIDeviceOrientationDidChangeNotification,
object: nil)
// 通知を受けて呼ばれる関数
func OrientationDidChangeNotification(notification: NSNotification) {
// デバイスの向きを取得
var orientation: UIDeviceOrientation = UIDevice.currentDevice().orientation
//=> LandscapeRight
}
★UIDeviceOrientation
Unknown
Portrait
PortraitUpsideDown
LandscapeLeft
LandscapeRight
FaceUp
FaceDown
- ベンダーのIDを取得する
https://qiita.com/ashdik/items/1ecbcbddf6ae476bd253
let strIDFV = UIDevice.current.identifierForVendor?.uuidString
print(strIDFV)
// Optional("0994CC2E-D8BA-4E0F-AB2B-F6C9AA87CF41")