I would like to know how to call a function from another function within AppDelegate. It would be better to call this function from ViewController but could not get it to work.

I have in my AppDelegate.m the following code:

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
}

Since I don't know how to call the open url function from the ViewController.m I did this calling didFinishLaunchingWithOptions func from AppDelegate.m

My ViewController.m looks like:

@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)
    }
}

Any ideas?

1 Answers

0
gnasher729 On

You don't call this method at all. It is called by the operating system when your application is launched. You are absolutely not supposed to ever call it yourself.

Same with the other method, which will be called by the operating system when your application is asked to open a URL. Which might be the URL of a file, or a URL with a scheme that you registered for.

Set a breakpoint on appDidFinishLaunching, then on your viewDidLoad method, start debugging to get some idea what is going on. You might also consider reading Apple's documentation.