【问题标题】:How can I detect DND mode within an app?如何在应用程序中检测 DND 模式?
【发布时间】:2013-07-10 00:57:54
【问题描述】:

我正在构建一个医疗诊断应用程序。患者(或医生)在使用过程中的中断会破坏应用程序的用途并浪费大量时间。

我想知道如何在应用中检测“请勿打扰”模式是否已打开/关闭。 (最好知道飞行模式是否打开/关闭。)这样我可以提醒用户进入设置打开它。

更好(更文明):有什么方法可以让用户在应用程序开启免打扰模式? (就像用户可以使用MPVolumeView 在应用程序中标准化设备音量一样。)


最接近的answer I've yet found 指向this page,通过特殊的“URL”打开飞行模式。但它在 iOS 5 中是 only works

【问题讨论】:

  • 好问题。 iOS设置不允许用户进入DND或飞行模式的定时时段,这对我来说很不寻常。最古老的诺基亚具有该功能,它对于进入免打扰非常有用,但不会有忘记重新打开它的风险。我一直在做的事情。我想要一个仅用于该功能的应用程序。

标签: iphone ios cocoa-touch


【解决方案1】:

没有关于请勿打扰甚至飞行模式的公共 API。连状态都不知道。

关于飞行模式,您可以检查网络状态(使用可达性),但它不会 100% 准确。

Reachability 是一个code sample from Apple,但在 GitHub 上有几个基于它的库。

【讨论】:

  • 谢谢,马塞洛。如何检查可达性状态?
  • 我非常感谢您的帮助,Marcelo,并支持您。尽管这可能是任何人都可以做到的最好的,但这并不是一个令人满意的答案。这就是我没有接受它的原因。
【解决方案2】:

我找到了一种知道DND是否开启的方法,但它可能只在某些情况下使用...... 当 DND 开启时,CallKit 会返回特定的错误代码,例如:

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");
                                          }
                                      }
                                  }];

【讨论】:

  • 请将代码中的3 替换为CXErrorCodeIncomingCallErrorFilteredByDoNotDisturb
  • @VyachaslavGerchicov 完成,感谢 :)
【解决方案3】:

我可以检测到 Using Intents。

遵循的步骤:-

1.我在应用程序中包含通信通知和推送通知功能。

2.然后我在Info.Plist中添加了NSFocusStatusUsageDescription属性

3.然后在应用程序中启用通知

4.然后使用下面的代码检查

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

注意:-在您的应用程序中启用通知的代码

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()
            }
        }
    }

【讨论】:

    猜你喜欢
    • 2020-05-15
    • 2021-07-31
    • 2013-01-14
    • 2018-12-22
    • 2021-08-25
    • 2015-11-26
    • 2014-09-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多