0
0
iOS Swiftmobile~20 mins

Why platform APIs access device capabilities in iOS Swift - Build It to Prove It

Choose your learning style9 modes available
Build: Device Info Access
This screen shows how an app uses platform APIs to access device capabilities like battery level and device name.
Target UI
-------------------------
| Device Info Access     |
|-----------------------|
| Device Name:          |
| [Loading...]          |
|                       |
| Battery Level:        |
| [Loading...]          |
|                       |
| [Refresh Info] Button |
-------------------------
Display the device name using UIDevice API
Display the current battery level using UIDevice API
Add a Refresh button to update the displayed info
Show 'Loading...' text while fetching info
Starter Code
iOS Swift
import SwiftUI

struct DeviceInfoView: View {
    @State private var deviceName: String = "Loading..."
    @State private var batteryLevel: String = "Loading..."

    var body: some View {
        VStack(spacing: 20) {
            Text("Device Info Access")
                .font(.title)
                .padding()

            VStack(alignment: .leading, spacing: 10) {
                Text("Device Name:")
                Text(deviceName)
                    .fontWeight(.bold)

                Text("Battery Level:")
                Text(batteryLevel)
                    .fontWeight(.bold)
            }

            Button("Refresh Info") {
                // TODO: Add code to update device info
            }
            .padding()
            .background(Color.blue)
            .foregroundColor(.white)
            .cornerRadius(8)

            Spacer()
        }
        .padding()
        // TODO: Add code to fetch info when view appears
    }
}

struct DeviceInfoView_Previews: PreviewProvider {
    static var previews: some View {
        DeviceInfoView()
    }
}
Task 1
Task 2
Task 3
Task 4
Solution
iOS Swift
import SwiftUI
import UIKit

struct DeviceInfoView: View {
    @State private var deviceName: String = "Loading..."
    @State private var batteryLevel: String = "Loading..."

    func updateDeviceInfo() {
        let device = UIDevice.current
        device.isBatteryMonitoringEnabled = true
        deviceName = device.name
        let level = device.batteryLevel
        if level < 0 {
            batteryLevel = "Unknown"
        } else {
            batteryLevel = String(format: "%.0f%%", level * 100)
        }
    }

    var body: some View {
        VStack(spacing: 20) {
            Text("Device Info Access")
                .font(.title)
                .padding()

            VStack(alignment: .leading, spacing: 10) {
                Text("Device Name:")
                Text(deviceName)
                    .fontWeight(.bold)

                Text("Battery Level:")
                Text(batteryLevel)
                    .fontWeight(.bold)
            }

            Button("Refresh Info") {
                updateDeviceInfo()
            }
            .padding()
            .background(Color.blue)
            .foregroundColor(.white)
            .cornerRadius(8)

            Spacer()
        }
        .padding()
        .onAppear {
            updateDeviceInfo()
        }
    }
}

struct DeviceInfoView_Previews: PreviewProvider {
    static var previews: some View {
        DeviceInfoView()
    }
}

This app screen uses platform APIs from UIDevice to access device capabilities.

We get the device name from UIDevice.current.name. This tells us the name the user gave their device, like "John's iPhone".

We also get the battery level from UIDevice.current.batteryLevel. We enable battery monitoring first to get this info. The battery level is a float from 0.0 to 1.0, so we convert it to a percentage string.

The "Refresh Info" button calls the updateDeviceInfo() function to fetch and show the latest info. We also call this function when the view appears so the info loads right away.

This shows how platform APIs let apps safely and easily access device features like name and battery status.

Final Result
Completed Screen
-------------------------
| Device Info Access     |
|-----------------------|
| Device Name:          |
| John's iPhone         |
|                       |
| Battery Level:        |
| 87%                   |
|                       |
| [Refresh Info] Button |
-------------------------
When the screen loads, device name and battery level appear instead of 'Loading...'.
Tapping 'Refresh Info' updates the device name and battery level displayed.
Stretch Goal
Add a toggle to switch between light and dark mode for the screen.
💡 Hint
Use SwiftUI's @Environment(\.colorScheme) and a toggle button to switch color schemes.