如何从didFinishLaunchingWithOptions调用func应用程序[复制]

问题描述 投票:-1回答:1

我想知道如何从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调用open url函数,我这样做是从AppDelegate.m调用didFinishLaunchingWithOptions函数

我的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)
    }
}

有任何想法吗?

ios swift appdelegate
1个回答
0
投票

你根本不打电话给这个方法。启动应用程序时,操作系统会调用它。你绝对不应该自己打电话。

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

appDidFinishLaunching上设置一个断点,然后在你的viewDidLoad方法上,开始调试以了解发生了什么。您也可以考虑阅读Apple的文档。

© www.soinside.com 2019 - 2024. All rights reserved.