프로젝트 소개

Screen Time이란?

앱과 웹사이트를 얼마나 자주 사용하는지 추적하고, 사용을 제한하도록 하여 휴대폰 사용을 관리할 수 있도록 해주는 Apple의 서비스입니다.

사용량 추적과 사용 제한의 대상은 사용자 개인이 될 수도 있고, iCloud에 연동되어 있는 자녀가 될 수도 있습니다.

FamilyControls, DeviceActivity, ManagedSettings 3가지의 프레임워크를 같이 활용하여 Screen Time 관련 기능을 개발할 수 있습니다.

프로젝트 구조

.
├── ScreenTime_Barebones // 프로젝트의 메인 타겟입니다.
│   ├── App
│   │   ├── ContentView.swift
│   │   ├── ScreenTime_BarebonesApp.swift
│   │   └── XCConfig
│   │       └── shared.xcconfig
│   ├── Assets.xcassets
│   │   ├── AccentColor.colorset
│   │   │   └── Contents.json
│   │   ├── AppIcon.appiconset
│   │   │   ├── AppIcon_box.png
│   │   │   └── Contents.json
│   │   ├── AppSymbol.imageset
│   │   │   ├── AppIcon.png
│   │   │   └── Contents.json
│   │   └── Contents.json
│   ├── Extension
│   │   ├── Bundle+Extension.swift
│   │   └── Color+Extension.swift
│   ├── Presentation
│   │   ├── ViewModels
│   │   │   ├── Components
│   │   │   │   └── MainTabVM.swift
│   │   │   ├── PermissionVM.swift
│   │   │   └── ScheduleVM.swift // 스크린타임을 통해 사용시간을 트래킹하는 스케쥴을 관리하기 위한 ViewModel
│   │   └── Views
│   │       ├── Components
│   │       │   └── MainTabView.swift
│   │       ├── MonitoringView
│   │       │   └── MonitoringView.swift
│   │       ├── PermissionView
│   │       │   └── PermissionView.swift
│   │       └── ScheduleView
│   │           └── ScheduleView.swift
│   ├── Preview Content
│   │   └── Preview Assets.xcassets
│   │       └── Contents.json
│   ├── ScreenTime_Barebones.entitlements
│   └── Utils
│       ├── DeviceActivtyManager.swift
│       └── FamilyControlsManager.swift
├── DeviceActivityMonitor // 생성한 스크린타임 스케쥴에 기반한 이벤트 발생 시 호출되는 메서드를 관리하기 위한 타겟
│   ├── DeviceActivityMonitor.entitlements
│   ├── DeviceActivityMonitorExtension.swift
│   └── Info.plist
├── ScreenTimeReport // 스크린타임을 통해 사용내역을 조회하고 관리하기 위한 타겟
│   ├── Info.plist
│   ├── ScreenTimeActivityReport.swift
│   ├── ScreenTimeReport.entitlements
│   ├── ScreenTimeReport.swift
│   ├── TotalActivityReport.swift
│   └── TotalActivityView.swift
├── ShieldAction // 스크린타임을 통해 앱 사용이 제한된 화면에서의 이벤트 발생 시 호출되는 메서드를 관리하기 위한 타겟
│   ├── Info.plist
│   ├── ShieldAction.entitlements
│   └── ShieldActionExtension.swift
└── ShieldConfiguration // 스크린타임을 통해 앱 사용이 제한된 화면을 커스텀할 수 있게 도와주는 타겟
    ├── AppSymbol.png
    ├── Info.plist
    ├── ShieldConfiguration.entitlements
    └── ShieldConfigurationExtension.swift

핵심 코드

✅ 스크린타임 사용권한 요청하기

// ./ScreenTime_Barebones/Utils/FamilyControlsManager.swift

import FamilyControls

class FamilyControlsManager: ObservableObject {
	// MARK: - FamilyControls 권한 상태를 관리하는 객체
    let authorizationCenter = AuthorizationCenter.shared

    // MARK: - ScreenTime 권한 상태를 활용하기 위한 멤버 변수
    @Published var hasScreenTimePermission: Bool = false

	@MainActor
    func requestAuthorization() {
        if authorizationCenter.authorizationStatus == .approved {
            print("ScreenTime Permission approved")
        } else {
            Task {
                do {
                    try await authorizationCenter.requestAuthorization(for: .individual)
                    hasScreenTimePermission = true
                    // 동의함
                } catch {
                    // 동의 X
                    print("Failed to enroll Aniyah with error: \\\\(error)")
                    hasScreenTimePermission = false
                    // 사용자가 허용안함.
                    // Error Domain=FamilyControls.FamilyControlsError Code=5 "(null)
                }
            }
        }
    }
}