Uiviewcontroller 如何在swift 4中将数据从AppDelegate发送到我的ViewController子类

Uiviewcontroller 如何在swift 4中将数据从AppDelegate发送到我的ViewController子类,uiviewcontroller,appdelegate,swift4.1,Uiviewcontroller,Appdelegate,Swift4.1,我正在使用Swift 4.1,我想知道如何传递此处给出的url数据: func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any]) -> Bool { } 在UIViewController的子类中,我称为HotSpotRISViewController。以下是我的尝试: func application(

我正在使用Swift 4.1,我想知道如何传递此处给出的url数据:

func application(_ app: UIApplication, open url: URL,
                    options: [UIApplicationOpenURLOptionsKey : Any]) -> Bool {
}
在UIViewController的子类中,我称为HotSpotRISViewController。以下是我的尝试:

func application(_ app: UIApplication, open url: URL,
                    options: [UIApplicationOpenURLOptionsKey : Any]) -> Bool {
    self.window = UIWindow(frame: UIScreen.main.bounds)
    let vc = HotSpotRISViewController()
    vc.setURL(theURL: url)
    let navigationController = UINavigationController(rootViewController: vc)
    self.window!.rootViewController = navigationController

}
My HotSpotRISViewController包含以下功能:

func setURL(theURL: URL) {
    self.url = theURL
    print(self.url ?? "nil")
}
在我的HotSpotRISViewController中,我有一个URL类型的属性可以设置。上面的代码正确地传递了信息,因为我的print语句打印出了URL,但我得到的是一个黑屏而不是我的应用程序。我错过了什么?另一方面,以下内容可以使应用程序正确启动,但我不知道如何使用此方法传递url数据:

func application(_ app: UIApplication, open url: URL,
                    options: [UIApplicationOpenURLOptionsKey : Any]) -> Bool {
    let mainStoryboard = UIStoryboard(name: "Main", bundle: nil)
    let initialViewController = mainStoryboard.instantiateInitialViewController()
    self.window = UIWindow(frame: UIScreen.main.bounds)
    self.window?.rootViewController = initialViewController
    self.window?.makeKeyAndVisible()
}

我想知道如何在没有黑屏的情况下正确启动我的应用程序并传递url数据。谢谢。

您可以在appDelegate文件中添加一个全局变量,并使用setURL函数获取该变量:

在您的AppDelegate.swift中

    var myurl: URL!
    ....

    func application(_ app: UIApplication, open url: URL,
                options: [UIApplicationOpenURLOptionsKey : Any]) -> Bool {
        self.myurl = url
        let mainStoryboard = UIStoryboard(name: "Main", bundle: nil)
        let initialViewController = mainStoryboard.instantiateInitialViewController()
        self.window = UIWindow(frame: UIScreen.main.bounds)
        self.window?.rootViewController = initialViewController
        self.window?.makeKeyAndVisible()
    }
    ...
func setURL(theURL: URL) {
    DispatchQueue.main.async {
        if let appDelegate = UIApplication.shared.delegate as? AppDelegate {
            self.url = appDelegate.myurl
        }
    }
在您的HotSpotRISViewController.swift中

    var myurl: URL!
    ....

    func application(_ app: UIApplication, open url: URL,
                options: [UIApplicationOpenURLOptionsKey : Any]) -> Bool {
        self.myurl = url
        let mainStoryboard = UIStoryboard(name: "Main", bundle: nil)
        let initialViewController = mainStoryboard.instantiateInitialViewController()
        self.window = UIWindow(frame: UIScreen.main.bounds)
        self.window?.rootViewController = initialViewController
        self.window?.makeKeyAndVisible()
    }
    ...
func setURL(theURL: URL) {
    DispatchQueue.main.async {
        if let appDelegate = UIApplication.shared.delegate as? AppDelegate {
            self.url = appDelegate.myurl
        }
    }

您可以在appDelegate文件中添加全局变量,并使用setURL函数获取该变量:

在您的AppDelegate.swift中

    var myurl: URL!
    ....

    func application(_ app: UIApplication, open url: URL,
                options: [UIApplicationOpenURLOptionsKey : Any]) -> Bool {
        self.myurl = url
        let mainStoryboard = UIStoryboard(name: "Main", bundle: nil)
        let initialViewController = mainStoryboard.instantiateInitialViewController()
        self.window = UIWindow(frame: UIScreen.main.bounds)
        self.window?.rootViewController = initialViewController
        self.window?.makeKeyAndVisible()
    }
    ...
func setURL(theURL: URL) {
    DispatchQueue.main.async {
        if let appDelegate = UIApplication.shared.delegate as? AppDelegate {
            self.url = appDelegate.myurl
        }
    }
在您的HotSpotRISViewController.swift中

    var myurl: URL!
    ....

    func application(_ app: UIApplication, open url: URL,
                options: [UIApplicationOpenURLOptionsKey : Any]) -> Bool {
        self.myurl = url
        let mainStoryboard = UIStoryboard(name: "Main", bundle: nil)
        let initialViewController = mainStoryboard.instantiateInitialViewController()
        self.window = UIWindow(frame: UIScreen.main.bounds)
        self.window?.rootViewController = initialViewController
        self.window?.makeKeyAndVisible()
    }
    ...
func setURL(theURL: URL) {
    DispatchQueue.main.async {
        if let appDelegate = UIApplication.shared.delegate as? AppDelegate {
            self.url = appDelegate.myurl
        }
    }

我在哪里调用func setURL(theURL:URL)?实际上,我最后把它放在了我的AppDelegate:
var vc=hospotrisviewcontroller()
,然后我就这样做了:
func应用程序(app:UIApplication,open URL:URL,options:[UIApplicationOpenURLOptionsKey:Any])->Bool{vc.setURL(theURL:url)返回true}
而且它是有效的。但是,我对你的方式很好奇。我想知道你为什么使用
DispatchQueue.main.async
对不起DispatchQueue.main.async。在你的情况下你不需要它。我把它放在那里是因为我是如何使用它的。亚当,你能找出你为什么会出现黑屏吗?我在哪里打电话给func setURL(theURL:URL)?实际上,我最后把它放在了我的AppDelegate:
var vc=HotSpotRISViewController()
,然后我就这样做了:
func应用程序(uApp:UIApplication,open URL:URL,options:[uiApplicationOpenUrlOptionKey:Any])->Bool{vc.setURL(theURL:URL)return true}
而且它是有效的。但是,我对你的方式很好奇。我想知道你为什么使用
DispatchQueue.main.async
对不起DispatchQueue.main.async。在你的情况下你不需要它。我把它放在那里是因为我是如何使用它的。亚当,你能找出你为什么会有黑屏吗?