0

我的应用程序有 2 种初始状态。

  1. 登录
  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
            }
        }
    }
}

每次我做那个应用程序都会崩溃。如何克服这一点。请帮忙。提前致谢。

4

1 回答 1

0

最后使用观察者设计模式解决了这个问题。

当应用程序处于后台并点击通知托盘中的通知时,然后像这样从此处发布通知

@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 {

                NotificationCenter.default.post(name: Notification.Name(rawValue: "getPush"), object: pushResponse)
            }
        }
    }
}

要在下面的代码中接收此通知 init 到您的 viewController

NotificationCenter.default.addObserver(self, selector: #selector(self.getPushResponse), name: NSNotification.Name(rawValue: "getPush"), object: nil)

从以下代码中获取响应

func getPushResponse(_ notification: Notification){

    if let info = notification.object as? NSDictionary {
        let rid = info.object(forKey: "rid") as? String
        let noticeDetailsViewController = self.storyboard?.instantiateViewController(withIdentifier: "NoticeDetailsView") as? NoticeDetailsController
        noticeDetailsViewController?.id = rid!
        noticeDetailsViewController?.fromPush = true
        self.navigationController?.pushViewController(noticeDetailsViewController!, animated: true)
    }
}

不要忘记像这样从 viewController 中删除 Observer

deinit {
    NotificationCenter.default.removeObserver(self)
}

如果应用程序在后台没有实例并点击托盘中的通知,那么如何显示没有实例的通知。这可以像在 AppDelegate 中那样处理

let prefs: UserDefaults = UserDefaults.standard
    if let remoteNotification = launchOptions?[UIApplicationLaunchOptionsKey.remoteNotification] as? NSDictionary {
        prefs.set(remoteNotification as! [AnyHashable: Any], forKey: "startUpPush")
        prefs.synchronize()
    }

检查您的初始视图控制器是否存在此键

let prefs:UserDefaults = UserDefaults.standard
    if prefs.value(forKey: "startUpPush") != nil {

        let pushResponse = prefs.object(forKey: "startUpPush") as! NSDictionary
        NotificationCenter.default.post(name: Notification.Name(rawValue: "getPush"), object: pushResponse)
    }

像这样显示 detailsController 后不要忘记删除此键

if fromPush == true {
        let prefs: UserDefaults = UserDefaults.standard
        if prefs.value(forKey: "startUpPush") != nil {
            prefs.removeObject(forKey: "startUpPush")
            prefs.synchronize()
        }
    }
于 2017-01-11T13:09:46.573 回答