【发布时间】:2019-08-31 08:06:06
【问题描述】:
以下是我以编程方式将控制器添加到 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 准备好
【问题讨论】:
-
我个人并不认为在
application:didFinishLaunchingWithOptions:中创建一个新的UIWindow对象是一个好习惯。为什么不尝试写if window == nil {window = UIWindow()}并且避免实例化一个新的窗口对象,即使它已经存在。这可能是您的问题的原因。 -
这能解决我的问题吗?
-
试过这个不行
-
不,我说是
could be the reason,但我不保证就是这样。我无法弄清楚你的问题的原因是什么。这只是我更喜欢遵循的一个好习惯,因为它可以避免将来出现其他问题。 -
但是你使用故事板吗?如果是这样,您绝对应该从情节提要构建您的
UITabBarController,并将application:didFinishLaunchingWithOptions:用于其他非 UI 设置,并将 UI 逻辑留在情节提要和UIViewController类中。
标签: ios swift apple-push-notifications uitabbarcontroller appdelegate