Search code examples
iosios15

What's the API for querying Focus mode on iOS 15?


iOS 15 introduced Focus mode. Apple mentioned that communication apps can query if the device is currently in Focus so we can tell the other person that they might not be notified of messages. However I couldn't find the API in apple docs. Does anyone know?


Solution

  • See https://developer.apple.com/forums/thread/682143

    FocusStatusCenter

    import Intents
    
    /// Retrieve the current authorization status: INFocusStatusAuthorizationStatus
    INFocusStatusCenter.default.authorizationStatus
    
    /// Request authorization to check Focus Status
    //
    INFocusStatusCenter.default.requestAuthorization { status in
        /// Provides a INFocusStatusAuthorizationStatus
    }
    
    /// Check if Focus is enabled. INFocusStatusAuthorizationStatus must be .authorized
    INFocusStatusCenter.default.focusStatus.isFocused
    

    You need add NSFocusStatusUsageDescription to Info.plist

    https://onesignal.com/blog/ios-notification-changes-updates-from-apples-wwdc-21/