How can I detect DND mode within an app?
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");
}
}
}];
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()
}
}
}
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.