简体   繁体   English

检测WatchKit应用程序中的活动接口控制器?

[英]Detect active interface controller in WatchKit app?

I've built a WatchKit app with a hierarchical structure that can receive actionable push notifications. 我已经构建了一个具有分层结构的WatchKit应用程序,可以接收可操作的推送通知。 When the notification comes in, the action is to push one particular interface controller so that the user can view it immediately without having to drill down through menus. 当通知进入时,操作是推送一个特定的接口控制器,以便用户可以立即查看它而无需向下钻取菜单。

Apple's documentation on handleActionWithIdentifier:ForRemoteNotification: states that this method is only called on the initial interface controller, so my code for handling the remote notification in the initial interface controller looks like this: Apple关于handleActionWithIdentifier:ForRemoteNotification:文档 handleActionWithIdentifier:ForRemoteNotification:声明此方法仅在初始接口控制器上调用,因此我在初始接口控制器中处理远程通知的代码如下所示:

override func handleActionWithIdentifier(identifier: String?, forRemoteNotification remoteNotification: [NSObject : AnyObject]) {
    if let notificationIdentifier = identifier {
        if notificationIdentifier == "myActionIdentifier" {
            pushControllerWithName("myActionController", context: nil)
        }
    }
}

However, the problem is, if the user was already viewing this myActionController previously and then had suspended my app, and then later this notification comes in and the action is taken, the app will push this same myActionController onto the stack again. 但是,问题是,如果用户之前已经在查看此myActionController,然后暂停了我的应用程序,然后稍后会收到此通知并执行操作,则应用程序会将此同样的myActionController再次推送到堆栈中。 Then when the user taps the "back" button, they'll just pop onto the previous myActionController, which will look the same. 然后,当用户点击“后退”按钮时,他们只会弹出前一个myActionController,它看起来是一样的。 This problem stacks, as well - if the user receives multiple notifications of this type and takes the action on all of them, the myActionController will just start stacking up. 此问题也会堆叠 - 如果用户收到此类型的多个通知并对所有这些通知执行操作,则myActionController将开始堆叠。

I'd like to remedy this problem by in handleActionWithIdentifier:forRemoteNotification: checking what the current active interface controller is, and if it's already myActionController, don't do anything (because it will reload once willActivate: is called on it anyway). 我想在handleActionWithIdentifier:forRemoteNotification:解决这个问题handleActionWithIdentifier:forRemoteNotification:检查当前活动接口控制器是什么,如果它已经是myActionController,不要做任何事情(因为它会重新加载一次willActivate:无论如何willActivate:调用它)。 I know this is possible (sometimes) in iOS, for example on a UINavigationController, but I don't know if it is possible in WatchKit. 我知道这有可能(有时)在iOS中,例如在UINavigationController上,但我不知道它是否可以在WatchKit中使用。 Does anyone have any ideas? 有没有人有任何想法?

I have almost the exact same scenario in my own WatchKit app, and I've handled it by tracking which view controller is currently being presented. 我在自己的WatchKit应用程序中几乎完全相同的情况,我通过跟踪当前正在呈现的视图控制器来处理它。 In a simple version, you could track the name of the last view controller you've presented and clear it in the presenting controller's willActivate method (as it will be called when the presenting controller is re-displayed). 在简单版本中,您可以跟踪您呈现的最后一个视图控制器的名称,并在呈现控制器的willActivate方法中清除它(因为它将在重新显示呈现控制器时调用)。 If you received your notification while something is presented, you can then decide if you need to dismiss/pop the controller first. 如果您在出示某些内容时收到了通知,则可以先决定是否需要解除/弹出控制器。

It might be more than you want/need, but I've written a JBInterfaceController subclass that wraps a lot of this functionality: https://github.com/mikeswanson/JBInterfaceController 它可能比您想要/需要的更多,但我编写了一个JBInterfaceController子类,它包含了很多这个功能: https//github.com/mikeswanson/JBInterfaceController

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM