Ios 如何从didFinishLaunchingWithOptions调用func应用程序

Ios 如何从didFinishLaunchingWithOptions调用func应用程序,ios,swift,appdelegate,Ios,Swift,Appdelegate,我想知道如何从AppDelegate中的另一个函数调用函数。最好从ViewController调用此函数,但无法使其工作 我的AppDelegate.m中有以下代码: func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool { let geturl = url.host?.removingPercent

我想知道如何从AppDelegate中的另一个函数调用函数。最好从ViewController调用此函数,但无法使其工作

我的AppDelegate.m中有以下代码:

func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {

    let geturl = url.host?.removingPercentEncoding;
    UserDefaults.standard.set(geturl, forKey: "DeepLinkUrl")
    return true
}

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    //I WANT CALL the upper function to set the URL IN HERE
    return true
}
因为我不知道如何从ViewController.m调用openurl函数,所以我调用了
didfishlaunchingwithoptions
func fromAppDelegate.m

我的ViewController.m看起来像:

@objc func appWillEnterForeground() {
    print("app on foreground")

    let appDelegate: AppDelegate? = UIApplication.shared.delegate as? AppDelegate
    appDelegate?.application(UIApplication.shared, didFinishLaunchingWithOptions: nil)

//ACTUALLY I WANT TO CALL THE SET URL FUNCTION IN STEAD OF didFinishLaunchingWithOption BUT DON'T KNOW HOW. SO I FOUND THIS WHICH IS BEING CALLED


    let user = UserDefaults.standard
    if user.url(forKey: "DeepLinkUrl") != nil{
        let str = user.value(forKey: "DeepLinkUrl") as! String
        print(str)
    }
}

有什么想法吗?

您根本不调用此方法。当应用程序启动时,操作系统会调用它。你绝对不应该自己称呼它

与其他方法相同,当应用程序被要求打开URL时,操作系统将调用该方法。它可能是文件的URL,也可能是您注册的方案的URL


appDidFinishLaunching
上设置一个断点,然后在
viewDidLoad
方法上,开始调试以了解发生了什么。你也可以考虑阅读苹果的文档。 这基本上不是一个转载的吗?为什么不更新这个问题而不是发布一个新的问题呢?我想使用我的另一个问题来了解ViewController是如何首先开始处理的,这个问题可能有助于解决我的问题,这也是我上一个问题中的实际情况,可以这样调用:
UIApplication.shared.openURL(…)
来自任何地方..不相关但
如果user.url(forKey:“DeepLinkUrl”)!=nil{let str=user.value(forKey:“DeepLinkUrl”)因为!String
是可怕的、不简洁的代码,这不起作用,因为我的问题是该url已经由scheme url打开。我从另一个应用程序(如openMyApp://printthistext)调用该应用程序。然后我需要在视图中打印该文本,但在AppDelegate中设置此url之前,不知何故首先调用我的视图。因此,我无法使用t我的视图中的hat文本变量。我需要在我的视图中实际显示该文本。。。