简体   繁体   English

应用启动时无法从didFinishLaunchingWithOptions访问ViewController

[英]Unable to access viewcontroller from didFinishLaunchingWithOptions when app launches

I am having an issue where I am unable to access a function in my viewcontroller.swft file when lunching from a killed app in didFinishLaunchingWithOptions . 我遇到了一个问题,当我在didFinishLaunchingWithOptions被杀死的应用程序中didFinishLaunchingWithOptions时,无法访问viewcontroller.swft文件中的功能。

I am trying to access the function with viewController?.loadRequestnotificationwaiting(for: url as! String) from AppDelegate which I pass data to the viewcontroller where I can do some stuff. 我试图通过viewController?.loadRequestnotificationwaiting(for: url as! String)访问该函数,该函数将数据传递给viewcontroller,在其中我可以做一些事情。 But when I place an alert in the function in the viewcontroller loadRequestnotificationwaiting . 但是当我在视图loadRequestnotificationwaiting中的函数中放置警报时。 The data is not being passed. 数据未传递。

Now I use this same method in other areas to pass data to the viewcontroller from the appdelegate and they work fine. 现在,我在其他区域使用相同的方法将数据从appdelegate传递到viewcontroller,并且它们可以正常工作。 It seems to not work when using it in didFinishLaunchingWithOptions didFinishLaunchingWithOptions使用它似乎不起作用

Is the viewcontroller not available yet when trying to access it from didFinishLaunchingWithOptions ? 尝试从didFinishLaunchingWithOptions访问didFinishLaunchingWithOptions时,它尚不可用吗?

AppDelegate.swift AppDelegate.swift

class AppDelegate : UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate {
    weak var viewController : ViewController?

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        UNUserNotificationCenter.current().delegate = self
        ConnectionManager.sharedInstance.observeReachability()
        // Override point for customization after application launch.
        FirebaseApp.configure()
        registerForPushNotifications()

        // opened from a push notification when the app is closed
        if let userInfo = launchOptions?[.remoteNotification] as? [String : AnyObject] {
            if let object = userInfo["aps"] {
                let url = object["url"]

                viewController?.loadRequestnotificationwaiting(for: url as! String)
            }
        }
        return true
    }

}

ViewController.swift ViewController.swift

class ViewController: UIViewController, WKNavigationDelegate {
    func loadRequestnotificationwaiting(for notification_url_wait : String) {
        notification_url_final_wait = notification_url_wait

        let url = URL(string: notification_url_final_wait!)
        let URLrequest = URLRequest(url: url!)
        self.webView.load(URLrequest)
    }
}

You are relying on your AppDelegate 's viewController property, but you are setting this property in your viewDidLoad method of your view controller; 您依赖于AppDelegateviewController属性,但是正在视图控制器的viewDidLoad方法中设置此属性; This is fine when a notification is received when your app is already running, since the viewController property is already set. 当您的应用已在运行时收到通知时,这很好,因为已经设置了viewController属性。

When a notification causes your application to be launched, the viewController property is not set and the function isn't called. 通知导致您的应用程序启动时, 未设置 viewController属性,并且不会调用该函数。

Assuming that the view controller you need is the initial view controller from your storyboard, you can get the root view controller from your app delegate's window property; 假设所需的视图控制器是情节提要中的初始视图控制器,则可以从应用程序委托的window属性中获取根视图控制器。

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    UNUserNotificationCenter.current().delegate = self
    ConnectionManager.sharedInstance.observeReachability()
    // Override point for customization after application launch.
    FirebaseApp.configure()
    registerForPushNotifications()

    // opened from a push notification when the app is closed
    if let userInfo = launchOptions?[.remoteNotification] as? [String : AnyObject] {
        if let object = userInfo["aps"],
            let url = object["url"] as? String,
            let viewController = self.window?.rootViewController as? ViewController {
                viewController.loadRequestnotificationwaiting(for: url)
        }
    }
    return true
}
  1. if your view controller is root, you can summon it by call UIApplication.shared.keyWindow!.rootViewController! 如果您的视图控制器是root用户,则可以通过调用UIApplication.shared.keyWindow!.rootViewController!来召唤它UIApplication.shared.keyWindow!.rootViewController! or overwrite it with your viewController 或用您的viewController覆盖它
  2. you can raise a notification using -[NotificationCenter postNotificationName:object:userInfo:] and observe this notification name in View Controller 您可以使用-[NotificationCenter postNotificationName:object:userInfo:]发出通知,并在View Controller中观察此通知名称

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从didFinishLaunchingWithOptions打开ViewController - Open ViewController from didFinishLaunchingWithOptions 当我在app didFinishLaunchingWithOptions中收到UIApplicationLaunchOptionsLocationKey时,我是否初始化viewController? - Do I initialize the viewController when I receive UIApplicationLaunchOptionsLocationKey in app didFinishLaunchingWithOptions? 如果视图是最后一个打开,则启动应用程序时ViewController不会刷新 - ViewController not refreshing when app launches if view is last one open Swift - 在应用程序启动时显示 revealingSplashView,无论哪个 ViewController - Swift - show revealingSplashView when app launches no matter which ViewController 从didFinishLaunchingWithOptions终止应用程序 - terminating an app from didFinishLaunchingWithOptions 从网址架构启动应用程序时未调用didFinishLaunchingWithOptions - didFinishLaunchingWithOptions not called when app launch from url schema 当应用程序从推送通知打开时,是否调用didFinishLaunchingWithOptions - Is didFinishLaunchingWithOptions called when app opens from push notification 应用关闭时,didFinishLaunchingWithOptions APNS - didFinishLaunchingWithOptions APNS when app is close 调用didFinishLaunchingWithOptions时,应用崩溃 - App crashes when didFinishLaunchingWithOptions is called 尝试从AppDelegate访问ViewController时应用崩溃 - App crashes when trying to access a viewcontroller from AppDelegate
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM