12

I'm building a medical diagnostic app. Interruptions while a patient (or doctor) is in the midst of using it would disrupt the purpose of the app and waste a lot of time.

I would like to know how I can detect within an app whether Do Not Disturb mode is switched on/off. (It would also be good to know whether Air Plane Mode is on/off.) That way I could remind the user to go into Settings to turn it on.

Even better (much more civilized): is there any way I could allow the user to turn on DND mode from within the app? (Rather like the user can normalize the device volume within an app using MPVolumeView.)


The closest answer I've yet found directs to this page on turning on Air Plane mode with a special 'URL.' But it only works in iOS 5.

Community
  • 1
  • 1
JohnK
  • 6,865
  • 8
  • 49
  • 75
  • good question. it's extraordinary to me that iOS Settings don't allow a user to enter a timed period of DND or Airplane mode. The oldest of Nokia's had that feature and it's very useful for entering DND but not having the risk of forgetting to turn it back on. Something I do all the time. I'd like an app just for that functionality alone. – wide_eyed_pupil Aug 05 '15 at 15:15

3 Answers3

15

There's no public API about Do Not Disturb or even Airplane mode. Not even to know the status.

About Airplane mode, you could check the network status (using Reachability), but it wouldn't be 100% accurate.

Reachability is a code sample from Apple, but there are several libraries based on it on GitHub.

Mark A. Donohoe
  • 28,442
  • 25
  • 137
  • 286
Marcelo
  • 9,916
  • 3
  • 43
  • 52
  • Thanks, Marcelo. How does one check Reachability status? – JohnK Jul 10 '13 at 01:07
  • I do appreciate your help, Marcelo, and upvoted you. Though it may be the best anyone can do, this is not a satisfying answer. That's why I haven't accepted it. – JohnK Jul 19 '13 at 16:59
9

I found a way to know if DND is on, but it may only be used in certain circumstance... The CallKit will return particular error code when DND is on, for example:

CXCallUpdate *update = [[CXCallUpdate alloc] init];
update.remoteHandle = [[CXHandle alloc] initWithType:CXHandleTypeGeneric value:handle];
[(CXProvider *)self.provider reportNewIncomingCallWithUUID:uuid
                                      update:update
                                  completion:^(NSError *error) {
                                      if (error) {
                                          NSLog(@"error when reporting imconing: %@", [error localizedDescription]);
                                          //The operation couldn’t be completed. (com.apple.CallKit.error.incomingcall error 3.)
                                          if ([error code] == CXErrorCodeIncomingCallErrorFilteredByDoNotDisturb) {
                                              NSLog(@"Disturb mode is on");
                                          }
                                      }
                                  }];
steven
  • 1,237
  • 2
  • 11
  • 13
2

I can detect Using Intents.

Steps Followed:-

1.I Include Communication Notification and Push Notification Capability to the Application.

2.Then I added NSFocusStatusUsageDescription Property in Info.Plist

3.Then Enabled Notification Within Application

4.Then Checked By Using Below Code

func findStatus(){
        INFocusStatusCenter.default.requestAuthorization { status in
            print("isFocused \(INFocusStatusCenter.default.focusStatus.isFocused)")
        }
    }

Note:-Code for Enable Notification within your Application

func requestNotificationAuthorization(){
        // Request for permissions
        UNUserNotificationCenter.current()
            .requestAuthorization(
                options: [.alert, .sound, .badge]) {
                    [weak self] granted, error in
                    //print("Notification granted: (granted)")
                    guard granted else { return }
                    
                    self?.getNotificationSettings()
                }
    }

func getNotificationSettings() {
        
        UNUserNotificationCenter.current().getNotificationSettings { settings in
            guard settings.authorizationStatus == .authorized else { return }
            DispatchQueue.main.async {
                UIApplication.shared.registerForRemoteNotifications()
                self.findStatus()
            }
        }
    }
Gowtham
  • 385
  • 1
  • 11