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

使用Screen Time API锁定应用遇沙箱限制错误159,求解决方案

解决Screen Time API锁定应用的沙箱限制错误及权限申请指南

一、错误原因解析

com.apple.ManagedSettingsAgent was invalidated: failed at lookup with error 159 - Sandbox restriction 这个错误的核心是:你的App未正确配置Screen Time相关权限,或沙箱权限设置不完整,导致无法访问系统的ManagedSettingsAgent服务。

二、完整权限配置与问题修复步骤

1. 配置Info.plist权限描述

在App的Info.plist中添加权限申请说明,否则系统会直接拒绝权限请求:

<key>NSFamilyControlsUsageDescription</key>
<string>需要访问Screen Time权限以锁定指定应用,帮助管理使用时间</string>

2. 添加Family Controls Entitlement

  • 登录Apple开发者后台,找到你的App ID,勾选Family Controls能力;
  • 在Xcode项目的Entitlements.plist中添加对应键值:
<key>com.apple.developer.family-controls</key>
<true/>

3. 正确请求权限并锁定应用的代码示例

使用FamilyControls框架完成权限请求与应用锁定,确保流程合规:

import FamilyControls

let authCenter = AuthorizationCenter.shared
let settingsStore = ManagedSettingsStore()

// 请求个人权限(家庭共享场景改用.family)
authCenter.requestAuthorization(for: .individual) { result in
    switch result {
    case .success(let isGranted):
        guard isGranted else {
            print("用户拒绝了Screen Time权限")
            return
        }
        // 替换为目标应用的Bundle ID
        let targetAppToken = ApplicationToken(identifier: "com.apple.MobileSMS")
        let restrictions = ApplicationRestrictions(deniedApplications: [targetAppToken])
        do {
            try settingsStore.apply(restrictions)
            print("应用锁定成功")
        } catch {
            print("锁定失败:\(error.localizedDescription)")
        }
    case .failure(let error):
        print("权限请求出错:\(error.localizedDescription)")
    }
}

4. 沙箱权限检查

  • 在Xcode的Signing & Capabilities中,确保App Sandbox已启用,且未完全禁用网络权限(Screen Time需要与系统服务通信);
  • macOS开发需额外开启App Sandbox中的System Preferences权限。

三、避坑要点

  • 必须使用真机测试:模拟器对Screen Time API的支持有限,多数权限操作会报错;
  • 目标应用的Bundle ID必须精准:可通过设备设置->屏幕使用时间->查看所有活动获取,或用第三方工具查询;
  • 权限类型匹配:个人用户锁定自己的应用用.individual,家庭共享场景需用.family。

四、额外排查步骤

若配置后仍报错,尝试:

  • 重启设备,清除系统权限缓存;
  • 检查Xcode签名:确保使用的开发者证书已包含Family Controls entitlement;
  • 验证设备权限:进入设置->屏幕使用时间->内容和隐私访问限制,确认你的App已被授予权限。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.13 21:29:57