- Документация: https: //developer.apple.com/documentation/deviceactivity
- Видео, показывающее это: https://developer.apple.com/videos/play ... 22/110336/
- запросить авторизацию для такого API устройства
- Получите общее время использования экрана пользователя и отобразите его
Вот что у меня есть на данный момент:
ContentView
import SwiftUI
import DeviceActivity
struct ContentView: View {
var body: some View {
NavigationView {
VStack {
Text("Welcome to the first view")
}
.toolbar {
NavigationLink(destination: APICall()) {
Text("Next")
}
}
.navigationBarTitle("First View")
}
}
}
struct APICall: View {
@State private var screenTime: Double? = nil
@State private var error: Error? = nil
var body: some View {
VStack {
HStack {
Image(systemName: "clock")
.resizable()
.frame(width: 50, height: 50)
.aspectRatio(contentMode: .fit)
.padding(.leading, 20)
VStack(alignment: .leading) {
if screenTime != nil {
Text("Total screen time: \(screenTime!) hours")
.font(.title)
.foregroundColor(.primary)
} else {
Text("Press the button to fetch screen time")
.font(.title)
.foregroundColor(.secondary)
}
}
Spacer()
}
.padding(.vertical, 20)
Button(action: fetchScreenTime) {
Text("Fetch screen time")
.font(.title)
.foregroundColor(.blue)
.padding()
.background(LinearGradient(gradient: Gradient(colors: [.red, .orange]), startPoint: .topLeading, endPoint: .bottomTrailing))
.cornerRadius(10)
.overlay(
RoundedRectangle(cornerRadius: 10)
.stroke(Color.white, lineWidth: 4)
)
}
if error != nil {
Text("Error: \(error!.localizedDescription)")
}
}
}
func fetchScreenTime() {
//WHAT DO I DO FOR THIS
}
}
Запрос аутентификации
import Foundation
import SwiftUI
import DeviceActivity
import FamilyControls
@main
struct Worklog: App {
let center = AuthorizationCenter.shared
var body: some Scene {
WindowGroup {
VStack {Text("Hi")}
.onAppear {
Task {
do {
try await center.requestAuthorization(for: //WHAT DO I CALL HERE?
)
} catch {
print("Failed with \(error)")
}
}
}
}
}
}
Подробнее здесь: https://stackoverflow.com/questions/750 ... -in-an-app