我的应用程序有 2 种初始状态。
- 登录
- 登录后包含仪表板和其他内容。
像这样从 Appdelegate 切换视图时,我的应用程序运行良好
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
let userDataExist = CommonUserFunction.isUserDataExist() as Bool
if userDataExist == true {
let homeViewController = self.window?.rootViewController?.storyboard?.instantiateViewController(withIdentifier: "HomeView") as? HomeViewController
let navigationController = UINavigationController()
navigationController.viewControllers = [homeViewController!]
self.window!.rootViewController = navigationController
}
else{
let loginController = self.window?.rootViewController?.storyboard?.instantiateViewController(withIdentifier: "LoginView") as? LoginController
let navigationController = UINavigationController()
navigationController.viewControllers = [loginController!]
self.window!.rootViewController = navigationController
}
self.window?.makeKeyAndVisible()
return true
}
当我必须处理推送通知时,我遇到了问题。我通过这些方法在 iOS 10 中收到了推送通知
@available(iOS 10.0, *)
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (_ options: UNNotificationPresentationOptions) -> Void) {
print("Handle push from foreground")
// custom code to handle push while app is in the foreground
print(notification.request.content.userInfo)
}
@available(iOS 10.0, *)
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
// custom code to handle push while app is in the foreground
print(response.notification.request.content.userInfo)
}
当我从通知托盘点击通知时,这两种方法被触发,而应用程序是前台或后台。点击通知后,我必须显示详细信息页面。我尝试像之前在 didFinishLaunchingWithOptions 方法中所做的那样设置详细信息页面
@available(iOS 10.0, *)
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
let pushResponse = response.notification.request.content.userInfo as NSDictionary
let rtype = pushResponse.object(forKey: "rtype") as? String
let rid = pushResponse.object(forKey: "rid") as? String
if rtype != nil {
if rtype == "5" {
if rid != nil {
let noticeDetailsViewController = self.storyboard?.instantiateViewController(withIdentifier: "NoticeDetailsView") as? NoticeDetailsController
noticeDetailsViewController!.id = rtype!
noticeDetailsViewController?.fromPush = true
let navigationController = UINavigationController()
navigationController.viewControllers = [noticeDetailsViewController!]
self.window!.rootViewController = navigationController
}
}
}
}
每次我做那个应用程序都会崩溃。如何克服这一点。请帮忙。提前致谢。