iOS开发技术

iOS 通过屏幕时间API实现应用上锁和隐藏【更新】

2023-04-11  本文已影响0人  那月无痕

(最近有小伙伴说申请权限时间太长,没反应。我分享下我的一次授权经历:我有一次也是等了个把月,后面问了客服,说没有渠道去查询状态或者加急,所以重新提交了一遍权限申请,第三天就收到了授权成功的邮件。仅代表个人经历,非绝对)

经过一段时间断断续续的琢磨,终于实现了在自己App内实现应用隐藏、上锁和解锁的功能。

授权
AuthorizationCenter.shared.requestAuthorization(for: .individual)
获取列表
.familyActivityPicker(isPresented: $isLockPresented, selection: $lockModel.selectionToLock)
上锁和隐藏
store.shield.applications = applications.isEmpty ? nil : applications
store.shield.applicationCategories = ShieldSettings.ActivityCategoryPolicy.specific(categories, except: Set())
被锁的App页面定制

App上锁后,打开这个App会出现如下页面:


image.png
如何自定义页面?

直接上代码

class ShieldConfigurationExtension: ShieldConfigurationDataSource {
    override func configuration(shielding application: Application) -> ShieldConfiguration {
        let appname = application.localizedDisplayName ?? ""
        return ShieldConfiguration(
            backgroundBlurStyle: UIBlurEffect.Style.light,
            backgroundColor: UIColor.white,
            icon: UIImage(named: "ic_unlock_gb"),
            title: ShieldConfiguration.Label(text: "\(appname) \(localizedString("title_shield_unlocked"))", color: .black),
            subtitle: ShieldConfiguration.Label(text: localizedString("title_shield_text"), color: .black),
            primaryButtonLabel: ShieldConfiguration.Label(text: localizedString("title_shield_button"), color: .white),
            secondaryButtonLabel: ShieldConfiguration.Label(text: "", color: .white)
          )
    }

可以看到使用的是ShieldConfigurationExtension

如何响应自定义页面的按钮点击?
class ShieldActionExtension: ShieldActionDelegate {
    override func handle(action: ShieldAction, for application: ApplicationToken, completionHandler: @escaping (ShieldActionResponse) -> Void) {
        // Handle the action as needed.
        switch action {
        case .primaryButtonPressed:
            sendNotification(application: application)
            completionHandler(.defer)//close
        case .secondaryButtonPressed:
            completionHandler(.defer)
        @unknown default:
            fatalError()
        }
    }

可以看到使用的是ShieldActionExtension

本文也是给有经验的伙伴准备的,所以不做一些基础的创建之类的说明。

参考文档
There is a video about it from WWDC 2021 called Meet the Screen Time API: [https://developer.apple.com/videos/play/wwdc2021/10123/](https://developer.apple.com/videos/play/wwdc2021/10123/)

There are actually 3 APIs (none of them have Screen Time in the name)

*   Managed Settings ([https://developer.apple.com/documentation/ManagedSettings](https://developer.apple.com/documentation/ManagedSettings))
*   Family Controls ([https://developer.apple.com/documentation/familycontrols/](https://developer.apple.com/documentation/familycontrols/))
*   Device Activity ([https://developer.apple.com/documentation/deviceactivity/](https://developer.apple.com/documentation/deviceactivity/))
采坑

打包上架时需要申请权限,附上链接:https://5am.is/journal/2023/getting-an-entitlement-for-family-controls-distribution/
申请链接:https://developer.apple.com/contact/request/family-controls-distribution
附上截图:

IMG_0015.PNG IMG_0014.PNG
审核被拒
审核邮件

有解决的大佬帮忙留言,非常感谢。

【2023年6月10】感谢各位关注,审核已通过,应用已上架。顺带说一句,本次还解决了4.3的问题,功夫不负有心人

至此,完结!!!

上一篇 下一篇

猜你喜欢

热点阅读