Recognized by Clutch.co as a top-rated Mobile App Development Company.
folio3-mobile
US 408 365 4638
START YOUR PROJECT
  • Solutions
    • Apps Discovery Services
    • Team Augmentation
    • Enterprise
    • AR/VR
    • IoT
    • Wearables
    • Field Sales
    • On Demand Apps
  • Industries
    • Retail
    • Agriculture
    • Healthcare
    • Pharmaceutical & Life Sciences
    • Manufacturing
    • Automotive
    • Logistics
    • Education
  • Technologies
    • Native Mobile Apps
      • iOS
      • Android
    • Cross Platform Apps
      • React Native
      • Flutter
      • Ionic
      • Xamarin
      • NativeScript
      • Sencha
  • Portfolio
  • Blog
  • Contact Us
  • Solutions
    • Apps Discovery Services
    • Team Augmentation
    • Enterprise
    • AR/VR
    • IoT
    • Wearables
    • Field Sales
    • On Demand Apps
  • Industries
    • Retail
    • Agriculture
    • Healthcare
    • Pharmaceutical & Life Sciences
    • Manufacturing
    • Automotive
    • Logistics
    • Education
  • Technologies
    • Native Mobile Apps
      • iOS
      • Android
    • Cross Platform Apps
      • React Native
      • Flutter
      • Ionic
      • Xamarin
      • NativeScript
      • Sencha
  • Portfolio
  • Blog
  • Contact Us

Parental Control – ScreenTime API iOS

Published by: Noc Folio3 | July 7, 2022 msaqlain
SCROLL AND BE AMAZED!
Home > App Development • Blog > Parental Control – ScreenTime API iOS

Introduction:

Recently, Apple released the ScreenTime API for iOS. Although the ScreenTime feature has been there since the release of iOS 13, from iOS 15, Apple has introduced ScreenTime API for developers to allow them to develop apps for their user base with customized UI for an enhanced experience for parental controls. It mostly provides features for parental controls such as allowing parents to add restrictions to apps and websites to their child’s device, enabling parents to set time limits on their child’s usage of the device, and sharing usage of the child’s device with parents.

ScreenTime API comprises 3 frameworks:

  1. Managed Settings
  2. Family Controls
  3. Device Activity

Below are the functionalities provided by each framework.

Managed Settings Framework:

  • Shield apps and websites with custom UI
  • Provides web content filtering
  • Set restrictions on devices and keep them in place
  • Provides multiple options for restrictions on AppStore, Media, Safari, Game Center, etc.

Family Controls Framework

  • Authorizes access to Screen Time API on child’s device
  • Prevents removal/deletion of your app by the child
  • Requires parent’s authorization to remove your app
  • Provides FamilyActivityPicker to choose apps and websites to restrict

Device Activity Framework

  • Monitors schedules defined by the user and execute code accordingly.
  • Monitors usage thresholds defined and execute code when threshold reached
  • The type and time of usage can be defined.
  • Provides Device Activity extension that executes the code on respective schedules and events without even the child opening the app.

Prerequisites

  1. Apple Family Sharing needs to be enabled by the user and family members’ Apple ID should be added there
  2. Adding apple id as a family member enables screen time monitoring for all iOS devices associated with that Apple ID.
  3. Child’s Apple ID can be created by a parent or an existing ID with an age below 18 can be added.

Now let’s code…

First of all, we need to add a Device Monitor Extension to our target. This acts as a background service on a child’s device for various functionalities.

Go to File > New > Target > Choose Device Activity Monitor Extension.

Give the extension the desired name and click Finish. A new group named as your extension will be added in the project. Create a file named as MyMonitor in this group and write the following code.

import UIKit
import MobileCoreServices
import ManagedSettings
import DeviceActivity

class MyMonitor: DeviceActivityMonitor {
    let store = ManagedSettingsStore()
    override func intervalDidStart(for activity: DeviceActivityName) {
        super.intervalDidStart(for: activity)
        print("interval did start")
        let model = MyModel.shared
        let applications = model.selectionToDiscourage.applicationTokens
        store.shield.applications = applications.isEmpty ? nil : applications
        store.dateAndTime.requireAutomaticDateAndTime = true
    }

    override func intervalDidEnd(for activity: DeviceActivityName) {
        super.intervalDidEnd(for: activity)
        store.shield.applications = nil
        store.dateAndTime.requireAutomaticDateAndTime = false
    }
}

The above code overrides the two methods of Device Activity Monitor which are called on the interval start and end respectively, for the intervals defined in the other parts of code. On interval start, we are restricting user/child’s device from changing their date and time along with applying shield to all those apps restricted by the parent.

Now, in the AppDelegate of the main app, write the following code. Also, don’t forget to import FamilyControls framework in AppDelegate.


class AppDelegate: NSObject, UIApplicationDelegate {

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
        AuthorizationCenter.shared.requestAuthorization { result in
            switch result {
            case .success:
                print("Success")
            case .failure(let error):
                print("error for screentime is \(error)")
            }
        }

        _ = AuthorizationCenter.shared.$authorizationStatus
            .sink() {_ in
            switch AuthorizationCenter.shared.authorizationStatus {
            case .notDetermined:
                print("not determined")
            case .denied:
                print("denied")
            case .approved:
                print("approved")
            @unknown default:
                break
            }
        }
        return true
    }
}

The above code will show the below alert to the user for the first time and will require a parent to authenticate on the child’s device.

Authorization alert
Authentication required from parent/guardian

After parent successfully authenticates the child, the next time alert won’t show up and the child cannot delete the app without the parent/guardian’s approval just like as above.

Now create a new Swift file MyModel and write the following code.

import Foundation
import FamilyControls
import DeviceActivity
import ManagedSettings

class MyModel: ObservableObject {
    static let shared = MyModel()
    let store = ManagedSettingsStore()

    private init() {}

    var selectionToDiscourage = FamilyActivitySelection() {
        willSet {
            print ("got here \(newValue)")
            let applications = newValue.applicationTokens
            let categories = newValue.categoryTokens
            let webCategories = newValue.webDomainTokens
            store.shield.applications = applications.isEmpty ? nil : applications
            store.shield.applicationCategories = ShieldSettings.ActivityCategoryPolicy.specific(categories, except: Set())
            store.shield.webDomains = webCategories
        }
    }

    func initiateMonitoring() {
        let schedule = DeviceActivitySchedule(intervalStart: DateComponents(hour: 0, minute: 0), intervalEnd: DateComponents(hour: 23, minute: 59), repeats: true, warningTime: nil)

        let center = DeviceActivityCenter()
        do {
            try center.startMonitoring(.daily, during: schedule)
        }
        catch {
            print ("Could not start monitoring \(error)")
        }

        store.dateAndTime.requireAutomaticDateAndTime = true
        store.account.lockAccounts = true
        store.passcode.lockPasscode = true
        store.siri.denySiri = true
        store.appStore.denyInAppPurchases = true
        store.appStore.maximumRating = 200
        store.appStore.requirePasswordForPurchases = true
        store.media.denyExplicitContent = true
        store.gameCenter.denyMultiplayerGaming = true
        store.media.denyMusicService = false
    }
}

extension DeviceActivityName {
    static let daily = Self("daily")
}

FamilyActivitySelection is a picker provided by the FamilyControls framework which displays all the apps, categories, and websites on a child’s device and allows parents to restrict those apps.

FamilyActivitySelection picker

Above code has the method initiateMonitoring in which we are creating a schedule for 24 hours named as daily for which the Device activity monitor extension will observe. Along with that, we are setting some additional restrictions for the child such as child cannot change date and time, cannot change apple id, passcode, use Siri, download apps above certain rating and much more.

Finally, a view is created in the below code snapshot to display FamilyActivitySelection picker on a button click and start monitoring on child’s device after which all the additional restrictions are applied.

import SwiftUI
import FamilyControls

struct ContentView: View {
    @StateObject var model = MyModel.shared
    @State var isPresented = false
    
    var body: some View {
        Button("Select Apps to Discourage") {
            isPresented = true
        }
        .familyActivityPicker(isPresented: $isPresented, selection: $model.selectionToDiscourage)
        Button("Start Monitoring") {
            model.initiateMonitoring()
        }
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

When a child launches a restricted website or app, the following screen appears which is also customizable in a limited scope. We can only change the logo, title and message of the following screen which we haven’t done in this code. Also, a restriction sign appears on the app icon of the restricted app.

Screen when launching a restricted app.

Issue Found

Although ScreenTime API provides a package to develop a parental control app there’s a lot of immaturity in the API itself. Implementing the above code should have followed the specific behavior of the app flow mentioned by Apple in WWDC21 while the following were the issues faced.

Should be App Flow:

  • Parent/Guardian opens the app on the child’s device and authorizes with their Apple ID
  • Parents can then open the app on their device and choose settings, restrictions, and rules, and ScreenTime API sends that info to the child’s device
  • Then on the child’s device, it creates schedules and events using the Device Activity extension

Issues faced:

  • For the first few times, FamilyActivityPicker does not populate the apps on the child’s device under the categories
  • No option yet for opening the picker for a specific child
  • Currently, restricting apps only from a child’s device works even if the apps populate in the picker on parent’s device
  • Does not works on Simulator. Always needs a real device
  • Error returned from the Authorization is of type NSError and not the FamilyControlsError
  • This causes problem to get the error type and perform respective actions
  • Works on iOS 15 and above and requires SwiftUI implementation.

What’s Next

Recently Apple has introduced some new features in ScreenTime API all 3 frameworks in WWDC22 but I haven’t explored them yet. These new features are available to explore for iOS 16 and above. Also, this tutorial does not have restriction screen customization which can also be explored.


About Noc Folio3

Newsletter

Search

Archives

  • December 2023
  • April 2023
  • March 2023
  • October 2022
  • September 2022
  • August 2022
  • July 2022
  • June 2022
  • April 2022
  • March 2022
  • February 2022
  • October 2021
  • September 2021
  • May 2021
  • February 2021
  • January 2021
  • December 2020
  • November 2020
  • October 2020
  • May 2020
  • April 2020
  • March 2020
  • February 2020
  • January 2020
  • December 2019
  • November 2019
  • October 2019
  • September 2019
  • August 2019
  • July 2019
  • May 2019

Recent Posts

  • Exploring Flutter Navigation: From Basics to Advanced Routes
  • Web UI Test Automation with Pytest-BDD
  • How to fix IOS compass calibration issues
  • Testing Android Applications With Perfect Coverage
  • How to use useRef hook efficiently? – React

Tags

  • android
  • angular-state-management
  • Automation
  • Compass
  • cross-platform
  • css
  • development
  • firebase
  • hooks
  • ios
  • learn-ngrx
  • ngrx-beginner
  • ngrx/store
  • QA
  • react-native
  • reactjs
  • scss
  • stylesheet
  • styling
  • Testing
  • Test Script
  • UI-UX

Newsletter

Newsletter

Post navigation

Previous Automation testing using Cucumber BDD with Selenium
Next Mobile Device Fragmentation – What is it?
Schedule an Appointment with our Mobile App Development Expert
Footer Menu
  • Company
    • About Us
    • Portfolio
    • Blog
    • Careers
    • Contact Us
  • Solutions
    • Apps Discovery Services
    • Team Augmentation
    • Enterprise App Development
    • AR/VR Application Development
    • IoT Application Development
    • Wearables Apps Development
    • Field Sales
    • On-Demand Apps Development
  • Technologies
    • iOS
    • Android
    • React Native
    • Flutter
    • Ionic
    • Xamarin
    • NativeScript
    • HTML5
    • Sencha
  • Industries
    • Retail
    • Agriculture
    • Healthcare
    • Pharmaceutical
    • Manufacturing
    • Automotive
    • Logistics
    • Education

US Office

Belmont, California – 1301 Shoreway Road, Suite 160, Belmont, CA 94002

Pleasanton, California – 6701 Koll Center Parkway, #250 Pleasanton, CA 94566

Tel: +1 408 365 4638
Support: +1 (408) 512 1812

Mexico Office

Amado Nervo #2200, Edificio Esfera 1 piso 4, Col. Jardines del Sol, CP. 45050, Zapopan, Jalisco, Mexico

Bulgaria Office

49 Bacho Kiro Street, Sofia, 1000, Bulgaria

Canada Office​

895 Don Mills Road, Two Morneau Shepell Centre, Suite 900, Toronto, Ontario, M3C 1W3, Canada

UK Office

Export House, Cawsey Way, Woking Surrey, GU21 6QX

Tel: +44 (0) 14 8361 6611

UAE Office

Dubai, UAE – Dubai Internet City, 1st Floor, Building Number 12, Premises ED 29, Dubai, UAE

Tel: +971-55-6540154
Tel: +971-04-2505173

Pakistan Office

Folio3 Tower, Plot 26, Block B, SMCH Society, Main Shahrah-e-Faisal, Karachi.

First Floor, Blue Mall 8-R, MM Alam Road Gulberg III, Lahore.

Tel: +92-21-3432 3721-4 

© 2025, Folio3 Software Inc., All rights reserved.

  • Privacy policy and terms of use
  • Cookie Policy
Follow us on
Facebook-f Linkedin-in Instagram

Get a free app audit

[contact-form-7 id="3548" title="Float Banner Form"]