我正在try 创建推送通知,该通知根据推送获得的信息确定要打开哪个视图.

我已经设法从推送中获得了信息,但我现在正在努力打开视图

看看其他堆栈溢出问题,我目前有以下问题:

应用程序委派未完成加载:

     //Extract the notification data
    if let notificationPayload = launchOptions?[UIApplicationLaunchOptionsRemoteNotificationKey] as? NSDictionary {
        // Get which page to open
        let viewload = notificationPayload["view"] as? NSString
        let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
        //Load correct view
        if viewload == "circles" {

            var viewController = self.window?.rootViewController?.storyboard?.instantiateViewControllerWithIdentifier("Circles") as! UIViewController
            self.window?.rootViewController = viewController

                        }
    }

目前,这在var ViewController=self上失败...线

推荐答案

必须将ViewController StoryBoardId属性设置为下图.

在此处输入图像描述

在SWIFT中使用如下代码打开viewController

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

         let mainStoryboardIpad : UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
         let initialViewControlleripad : UIViewController = mainStoryboardIpad.instantiateViewControllerWithIdentifier("Circles") as UIViewController
         self.window = UIWindow(frame: UIScreen.main.bounds)
         self.window?.rootViewController = initialViewControlleripad
         self.window?.makeKeyAndVisible()

         return true
    }

For iOS 13+(基于an article by dev2qa)
打开SceneDelegate.swift并添加以下内容

func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {

    // If this scene's self.window is nil then set a new UIWindow object to it.
    self.window = self.window ?? UIWindow()

    // Set this scene's window's background color.
    self.window!.backgroundColor = UIColor.red

    // Create a ViewController object and set it as the scene's window's root view controller.
    self.window!.rootViewController = ViewController()

    // Make this scene's window be visible.
    self.window!.makeKeyAndVisible()

    guard scene is UIWindowScene else { return }
}

有一个开源的navigation utility试图让这件事变得更容易.Example

Ios相关问答推荐

如何访问iOS模拟器中的zoom 设置?(在搜索中列出,但不可见)

RxSwift:如何使用另外两个可观测对象(其中一个依赖于另一个)创建可观测对象?

如何将Safari调试器附加到Turbo-iOS原生应用中的模拟器

SWIFT-仅解码感兴趣的元素

如何在expo-react native中从ios平台中排除包

确保方法调配的安全有效实施

如何链式设置 AttributeContainer 的 UIKit 属性?

Flutter iOS 构建失败:DVTCoreDeviceEnabledState_Disabled

为什么这个 init 被分派到一个内部扩展 init?

Flutter firebase_auth 中的无效应用程序凭据/令牌不匹配

在所有 tableview / collectionview 中禁用滚动指示器

如何快速设置条形按钮的图像?

在 Swift 中执行 while 循环

在 iOS 编程中使用 Storyboard 代替 xib 文件有什么好处?

判断可选数组是否为空

如何将 iPhone OSStatus 代码转换为有用的东西?

如何在react 导航中获取当前路由名称?

如何缩小 UIImage 并使其同时变得清晰/锐利而不是模糊?

Xcode 4.2 如何将一个项目包含到另一个项目中?

如何检测快速touch 或单击的tableView单元格