如何使用iOS屏幕时间API获取用户的总屏幕时间(在我正在构建的应用中)

yrwegjxp  于 2023-01-18  发布在  iOS
关注(0)|答案(1)|浏览(810)

iOS屏幕时间API(使用DeviceActivity框架)能够为用户生成自定义屏幕时间报告

问题是,除了使用虚拟数据创建示例应用程序的入门iOS编程类之外,我根本不知道自己在做什么。
我得想办法
1.请求设备API的授权,如下所示
1.从用户处获取总屏幕时间并显示
这看起来很简单,也是可能的,但是我甚至不确定把文档中的代码放在哪个文件中,更不用说处理来自用户的真实数据了。
以下是我目前掌握的情况:

    • 内容视图**
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)")
                    }
                }
        }
    }
        
    }
    
}
lhcgjxsq

lhcgjxsq1#

do {
  try await center.requestAuthorization(
for: /* can be .individual to look at your 
       own screen time or .child if this 
       is going on a child's device */)
} catch {
print("Failed with \(error)")
}

在fetchScreenTime()中,您将对使用DeviceActivityMonitor的应用进行扩展。我将在弄清楚它们如何配合时进行更新

相关问题