Skip to content

Instantly share code, notes, and snippets.

@KentarouKanno
Last active April 28, 2018 00:11
Show Gist options
  • Save KentarouKanno/eaf94b505b1da0ac3681 to your computer and use it in GitHub Desktop.
Save KentarouKanno/eaf94b505b1da0ac3681 to your computer and use it in GitHub Desktop.
UIDevice

UIDevice

★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"

iOSModelName

★システム名を取得する

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
let strIDFV = UIDevice.current.identifierForVendor?.uuidString
print(strIDFV)
// Optional("0994CC2E-D8BA-4E0F-AB2B-F6C9AA87CF41")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment