ios - UITabBarController 以编程方式添加 Controller 导致通知打开时出现问题

标签 ios swift apple-push-notifications uitabbarcontroller appdelegate

以下是我以编程方式将 Controller 添加到 UITabBarController 的代码

class AppDelegate: UIResponder, UIApplicationDelegate {

var window: UIWindow?

var firstTabNavigationController : UINavigationController!
var secondTabNavigationControoller : UINavigationController!
var thirdTabNavigationController : UINavigationController!
var fourthTabNavigationControoller : UINavigationController!
var fifthTabNavigationController : UINavigationController!


func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    // Override point for customization after application launch.
    Fabric.with([Crashlytics.self])

   window = UIWindow(frame: UIScreen.main.bounds)


    window?.backgroundColor = UIColor.black


    let tabBarController = UITabBarController()

    firstTabNavigationController = UINavigationController.init(rootViewController: FirstViewController())
    secondTabNavigationControoller = UINavigationController.init(rootViewController: SecondViewController())
    thirdTabNavigationController = UINavigationController.init(rootViewController: ThirdViewController())
    fourthTabNavigationControoller = UINavigationController.init(rootViewController: FourthViewController())
    fifthTabNavigationController = UINavigationController.init(rootViewController: FifthViewController())

    tabBarController.viewControllers = [firstTabNavigationController, secondTabNavigationControoller, thirdTabNavigationController, fourthTabNavigationControoller, fifthTabNavigationController]


    let item1 = UITabBarItem(title: "Home", image: UIImage(named: "ico-home"), tag: 0)
    let item2 = UITabBarItem(title: "Contest", image:  UIImage(named: "ico-contest"), tag: 1)
    let item3 = UITabBarItem(title: "Post a Picture", image:  UIImage(named: "ico-photo"), tag: 2)
    let item4 = UITabBarItem(title: "Prizes", image:  UIImage(named: "ico-prizes"), tag: 3)
    let item5 = UITabBarItem(title: "Profile", image:  UIImage(named: "ico-profile"), tag: 4)

    firstTabNavigationController.tabBarItem = item1
    secondTabNavigationControoller.tabBarItem = item2
    thirdTabNavigationController.tabBarItem = item3
    fourthTabNavigationControoller.tabBarItem = item4
    fifthTabNavigationController.tabBarItem = item5

    UITabBar.appearance().tintColor = UIColor(red: 0/255.0, green: 146/255.0, blue: 248/255.0, alpha: 1.0)

    self.window?.rootViewController = tabBarController

    window?.makeKeyAndVisible()

    return true
}

现在这工作正常但是当我的应用程序关闭并且通知打开时它只是带我到主页而不是预期的页面

以下是处理通知的代码

@available(iOS 10.0, *)
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
    // Called to let your app know which action was selected by the user for a given notification.
    let userInfo = response.notification.request.content.userInfo as? NSDictionary
    var userNotification : UserNotification?
    if userInfo is [String : Any] {
        userNotification = createNSaveNotification(userInfo as! [AnyHashable : Any])
    }
    DeeplinkHandler.handleNotification(userNotification: userNotification)
}

类DeeplinkHandler主要方法代码

class func handleNotification(userNotification :  UserNotification?){
    let appDelegate = UIApplication.shared.delegate as! AppDelegate
    DispatchQueue.main.asyncAfter(deadline: .now() + 0.8) {
        if let tabBarController = appDelegate.window?.rootViewController as? UITabBarController,
            let navController = tabBarController.selectedViewController as? UINavigationController {
            handleDeeplinkByType(navController,userNotification)
        }
    }
}

现在观察我已经添加了 DispatchQueue.main.asyncAfter(deadline: .now() + 0.8) 时间延迟来处理通知 通知点击重定向到用户,只有在添加最小延迟时间的情况下,否则它不工作

如何处理这种情况并要求应用程序等待第一个标签栏 ViewController 在处理通知之前准备好

最佳答案

您可以复制 (tabBarController.viewControllers) 添加您的 newViewController 并设置 (tabBarController.viewControllers = newViewControllers)

关于ios - UITabBarController 以编程方式添加 Controller 导致通知打开时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57736278/

相关文章:

ios - 如何修复 Alamofire 库错误?

iphone - 如何实现 uislider 以便它可以用作 AVQueueplayer 中的洗涤器

ios - 如何验证多维 NSArray 结构?

iphone - iOS 文本字段验证

swift - 无法获取默认的 Firebase 数据库实例。必须调用`[FIRApp configure]

swift - 使用谷歌地图获取地址照片

apple-push-notifications - 如何颁发开发 MDM APNS 证书?

iphone - 点击iPhone通知中心的消息时如何转到特定 View ?

apple-push-notifications - ECDsa在Linux上登录.Net Core

iphone - 如何暂时禁用 View 及其所有 subview 的动画?