UIDevice is an API for obtaining device information. We need add .current
to get current device information. Such as:
let name = UIDevice.current.name
It will display the device name (“General-About This xxx-Name”), such as “xxx’s iPhone”.
It has some other properties:
.name
: device name (“General-About This xxx-Name”), for example: xxx’s iPhone..model
: Model of device, for example: iPhone 11 Pro..localizedModel
: It is the same as .model
, but it will become a localized string (as a localized string)..systemName
: System name, for example: iOS..systemVersion
: Specific version number, for example: 14.4.For battery, it can display the status or level.
First turn on isBatteryMonitoringEnabled
use the following (Here is two types:
var batteryLevel: Int {
UIDevice.current.isBatteryMonitoringEnabled = true
let data = Int(UIDevice.current.batteryLevel * 100)
return data
}
// Function
func battery() -> Int {
// Enable isBatteryMonitoringEnabled
UIDevice.current.isBatteryMonitoringEnabled = true
// Obtain battery level (in Float, the range is 0-1, here multiplied by 100 and converted into integer form)
let batterylevel = Int(UIDevice.current.batteryLevel * 100)
// Return level
return batterylevel
}
// Closure
let battery = { () -> Int in
// Enable isBatteryMonitoringEnabled
UIDevice.current.isBatteryMonitoringEnabled = true
// Obtain battery level (in Float, the range is 0-1, here multiplied by 100 and converted into integer form)
let batterylevel = Int(UIDevice.current.batteryLevel * 100)
// Return level
return batterylevel
}
And we can use the returned value:
struct ContentView: View {
var body: some View {
VStack{
Text("\(battery())")
.padding()
}
}
}
Now we can see the battery level.
I hope these will help someone in need~