You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

iOS 17开发:如何通过自研App实现已安装应用的限制与解除?

iOS 17 Screen Time 应用限制功能实现方案

核心前提说明

iOS的Screen Time API有严格的权限管控:

  • 必须通过系统弹窗获取用户手动授权,无法静默操作
  • 所有限制操作仅能基于用户授权的范围执行,无法绕过系统规则

实现步骤

1. 项目配置

  • 在Info.plist中添加NSScreenTimeUsageDescription键,填写授权说明(例如:"需要访问屏幕使用时间权限以帮您管理应用使用时长")
  • 导入ScreenTime框架到项目中

2. 请求Screen Time授权

import ScreenTime

func requestScreenTimeAuth() {
    STAuthorizationCenter.shared.requestAuthorization { status in
        DispatchQueue.main.async {
            switch status {
            case .authorized:
                // 授权成功,可执行后续操作
                print("Screen Time 授权通过")
            case .denied, .restricted:
                // 引导用户去设置开启权限
                print("授权失败,请前往系统设置开启Screen Time权限")
            case .notDetermined:
                break
            @unknown default:
                break
            }
        }
    }
}

3. 获取已安装应用列表

仅能获取Screen Time已追踪的应用,代码示例:

func getInstalledApps() {
    guard STAuthorizationCenter.shared.authorizationStatus == .authorized else {
        print("未授权,无法获取应用列表")
        return
    }
    
    let startDate = Calendar.current.date(byAdding: .month, value: -1, to: Date())!
    let endDate = Date()
    
    STAppUsageReport.shared.getReports(for: .daily, startDate: startDate, endDate: endDate) { reports, error in
        if let error = error {
            print("获取应用列表失败:\(error.localizedDescription)")
            return
        }
        
        // 去重提取应用信息
        let uniqueApps = Set(reports?.flatMap { $0.appUsage } ?? [])
        let targetApps = uniqueApps.filter { $0.bundleIdentifier != nil }
        
        // 将targetApps展示给用户选择即可
        print("共获取\(targetApps.count)个可管理应用")
    }
}

4. 设置/解除应用限制

设置时长限制(以每天10分钟为例)

func setAppLimit(bundleID: String, dailySeconds: TimeInterval) {
    guard STAuthorizationCenter.shared.authorizationStatus == .authorized else {
        print("未授权,无法设置限制")
        return
    }
    
    let limit = STAppUsageLimit(
        bundleIdentifier: bundleID,
        categoryIdentifier: nil,
        dailyDuration: dailySeconds
    )
    
    STAppUsageLimitCenter.shared.add(limit) { error in
        if let error = error {
            print("设置限制失败:\(error.localizedDescription)")
        } else {
            print("应用限制已生效")
        }
    }
}

解除应用限制

func removeAppLimit(bundleID: String) {
    guard STAuthorizationCenter.shared.authorizationStatus == .authorized else {
        print("未授权,无法解除限制")
        return
    }
    
    STAppUsageLimitCenter.shared.limits.forEach { limit in
        if limit.bundleIdentifier == bundleID {
            STAppUsageLimitCenter.shared.remove(limit) { error in
                if let error = error {
                    print("解除限制失败:\(error.localizedDescription)")
                } else {
                    print("应用限制已解除")
                }
            }
        }
    }
}

常见问题排查

  • 无法获取应用列表:确认已授权,且用户未在系统设置中关闭你的应用的Screen Time访问权限;新安装的应用可能需要等待Screen Time同步数据,可延迟重试
  • 限制不生效:检查bundleIdentifier是否正确;若用户在系统Screen Time中手动修改过限制,会覆盖你的设置
  • 授权被拒:可通过以下代码跳转至设置页面引导用户开启:
func openScreenTimeSettings() {
    guard let url = URL(string: UIApplication.openSettingsURLString) else { return }
    UIApplication.shared.open(url)
}

内容的提问来源于stack exchange,提问作者David Johansen

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.18 00:13:15