Finding out information about app or capability for device can sometimes be useful so today, we will build helper functions to fetch various features about app and device.
Apps support multiple scenes, depending upon the platform they are being rendered. UIApplication provides a property to check if current app supports multiple scenes or not so we will wrap it in a static function and use it in our view.
struct AppInfo {
static var supportsMultipleScene: Bool {
UIApplication.shared.supportsMultipleScenes
}
}
Similarly, we can check if app supports dynamic icon change capability.
struct AppInfo {
static var supportsMultipleScene: Bool {
UIApplication.shared.supportsMultipleScenes
}
static var supportsAlternateIcons: Bool {
UIApplication.shared.supportsAlternateIcons
}
}
We can check if ShakeToEdit is supported by the app.
struct AppInfo {
static var supportsMultipleScene: Bool {
UIApplication.shared.supportsMultipleScenes
}
static var supportsAlternateIcons: Bool {
UIApplication.shared.supportsAlternateIcons
}
static var supportsShakeToEdit: Bool {
UIApplication.shared.applicationSupportsShakeToEdit
}
}
With this helper function in place, we can make use of this inside our app’s view.
struct DevTechieDeviceInfo: View {
var body: some View {
VStack {
Text("DevTechie")
.font(.largeTitle)
Divider()
Text("App Supports Multiple Scene? **\(AppInfo.supportsMultipleScene.description)**")
Text("App Supports Alternate Icons? **\(AppInfo.supportsAlternateIcons.description)**")
Text("App Supports Shake To Edit? **\(AppInfo.supportsShakeToEdit.description)**")
}
}
}
UIDevice
class represents the device on which the app is currently running. We can query lots of information about the device using current
instance of UIDevice
class.
We can get Device’s name
struct DeviceInfo {
static var name: String {
UIDevice.current.name
}
}
We can fetch system name which is the name of the operating system running on the device.
struct DeviceInfo {
static var name: String {
UIDevice.current.name
}
static var systemName: String {
UIDevice.current.systemName
}
}
We can query model of the device.
struct DeviceInfo {
static var name: String {
UIDevice.current.name
}
static var systemName: String {
UIDevice.current.systemName
}
static var model: String {
UIDevice.current.model
}
}
System version gives us information about the current version of the operating system.
struct DeviceInfo {
static var name: String {
UIDevice.current.name
}
static var systemName: String {
UIDevice.current.systemName
}
static var model: String {
UIDevice.current.model
}
static var systemVersion: String {
UIDevice.current.systemVersion
}
}
struct DeviceInfo {
static var name: String {
UIDevice.current.name
}
static var systemName: String {
UIDevice.current.systemName
}
static var model: String {
UIDevice.current.model
}
static var systemVersion: String {
UIDevice.current.systemVersion
}
static var batteryState: String {
switch UIDevice.current.batteryState {
case .charging:
return "Charging"
case .full:
return "Full"
case .unplugged:
return "Unplugged"
default:
return "Unknown"
}
}
}
In order to check battery charge level on device, we need to have battery monitoring enabled. We enable battery monitoring if the app needs to be notified of changes to the battery state, or if we want to check the battery charge level.
We can check if monitoring is enabled or not
struct DeviceInfo {
static var name: String {
UIDevice.current.name
}
static var systemName: String {
UIDevice.current.systemName
}
static var model: String {
UIDevice.current.model
}
static var systemVersion: String {
UIDevice.current.systemVersion
}
static var batteryState: String {
switch UIDevice.current.batteryState {
case .charging:
return "Charging"
case .full:
return "Full"
case .unplugged:
return "Unplugged"
default:
return "Unknown"
}
}
static var isBatteryMonitoringEnabled: Bool {
UIDevice.current.isBatteryMonitoringEnabled
}
}
We can set the monitoring to be enabled. (Note that this will not show any change in simulator so test on device.)
static func setBatteryMonitoring(to value: Bool) -> Bool {
UIDevice.current.isBatteryMonitoringEnabled = true
return isBatteryMonitoringEnabled
}
Once the monitoring is enabled, we can check the battery charge levels
struct DeviceInfo {
static var name: String {
UIDevice.current.name
}
static var systemName: String {
UIDevice.current.systemName
}
static var model: String {
UIDevice.current.model
}
static var systemVersion: String {
UIDevice.current.systemVersion
}
static var batteryState: String {
switch UIDevice.current.batteryState {
case .charging:
return "Charging"
case .full:
return "Full"
case .unplugged:
return "Unplugged"
default:
return "Unknown"
}
}
static var isBatteryMonitoringEnabled: Bool {
UIDevice.current.isBatteryMonitoringEnabled
}
static func setBatteryMonitoring(to value: Bool) -> Bool {
UIDevice.current.isBatteryMonitoringEnabled = true
return isBatteryMonitoringEnabled
}
static var batteryLevel: String {
guard isBatteryMonitoringEnabled else { return "Unknown" }
let battery = UIDevice.current.batteryLevel
if battery == -1 {
return "Unknown"
}
return "\(battery.formatted(.percent))"
}
}
Driver view:
struct DevTechieDeviceInfo: View {
var body: some View {
VStack {
Text("DevTechie")
.font(.largeTitle)
Divider()
Text("Device Name **\(DeviceInfo.name)**")
Text("System Name **\(DeviceInfo.systemName)**")
Text("Model **\(DeviceInfo.model)**")
Text("System Version **\(DeviceInfo.systemVersion)**")
Text("Battery Charging State **\(DeviceInfo.batteryState)**")
Text("Battery Monitoring Enabled? **\(DeviceInfo.isBatteryMonitoringEnabled.description)**")
Text("Battery Monitoring Enabled (After change)? **\(DeviceInfo.setBatteryMonitoring(to: true).description)**")
Text("Battery Charge Level **\(DeviceInfo.batteryLevel)**")
}
}
}