ios - 如何在应用程序委托(delegate)中将选项卡 Controller 设置为根 Controller 而不实例化(重新加载)它?

标签 ios swift swift3 push-notification appdelegate

我在我的应用程序 (firebase) 中发送通知,因此在 didReceiveRemoteNotification 中,我将用户引导到应用程序中的新位置(导航 Controller 之一中的 View Controller ,位于选项卡 Controller 内部)。

在我的选项卡 Controller 的第一个 View 中,它始终检查 UserDefaults 是否用户已通过位置服务和推送通知 View Controller (这是他们接受或拒绝权限的位置)。如果用户尚未使用这些 View Controller (例如第一次启动应用程序时),它将转至适当的 View Controller (图像中的转接 1 和转接 2)。问题是,当它转到位置服务或推送通知 View Controller 时,我的 didReceiveRemoteNotification 中的选项卡栏 Controller 引用似乎不起作用。这意味着当我按下通知时,如果用户处于位置服务或推送通知中,它无法正确导航选项卡 Controller 。如果用户不需要通过定位服务或推送通知,它确实有效。这就是为什么我认为如果用户经过它,根 Controller 会出于某种原因切换到其中之一。

简单明了,我只想让我的标签栏 Controller 引用在我的 AppDelegate 中工作。为什么只有当用户使用定位服务或推送通知时它才不起作用?这是我的代码:

func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
    let state = UIApplication.shared.applicationState
    if(state == .active) {
        // Create local notification
    } else {
        if let businessID = userInfo["business_id"] as? String {
            Globals.notificationBusinessId = Int(businessID)!
        }
        if let businessName = userInfo["business_name"] as? String {
            Globals.notificationBusinessName = "\(businessName)"
        }
        if let itemID = userInfo["item_id"] as? String {
            Globals.notificationItemID = Int(itemID)!
        }
        if let navigationLevel = userInfo["navigation_level"] as? String {
            Globals.notificationNavigationLevel = Int(navigationLevel)!
        }
        Globals.popToBusinessesController = true

        myTabBar.selectedIndex = 1
        myTabBar.selectedIndex = 2
    }
}

Image

最佳答案

如果您点击通知,didReceiveRemoteNotification 将不会被调用,只有当您在应用程序处于事件状态时收到通知时才会调用此函数。 如果远程通知到达时应用程序未运行,则该方法将启动应用程序并在启动选项字典中提供适当的信息。

您可以查看全部here

关于ios - 如何在应用程序委托(delegate)中将选项卡 Controller 设置为根 Controller 而不实例化(重新加载)它?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45194511/

相关文章:

iphone - UITableViewCell 背景颜色在单元格的左右边缘是不同的阴影

ios - 初始化后整数变为0

ios - UIButton 按下时不会更改文本颜色

css - 将某人的 CSS 转换为 Xamarin iOS 以获得渐变

uikit - CGRectMake、CGPointMake、CGSizeMake、CGRectZero、CGPointZero 在 Swift 中不可用

ios - 我将如何在选项卡栏按钮点击(快速)上创建幻灯片菜单

iOS:用于presentViewControllers架构的popToRootViewController

iphone - 增加包含 2 个 UILabel 的自定义 UITableViewCell 的大小

ios - 无法分配给属性 'tabBarController' 是仅获取属性

ios - UIViewControllerAnimatedTransitioning 未触发