-1

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?

Bhavesh Nayi
  • 3,626
  • 1
  • 27
  • 42
Salihan Pamuk
  • 175
  • 1
  • 2
  • 13
  • Isn't this basically a repost of [your previous question](https://stackoverflow.com/questions/55880945/loading-url-from-scheme-not-processing-first-time-appdelegate-vs-viewcontrolle)? Why not update that question instead of posting a new one? – rmaddy Apr 27 '19 at 15:51
  • I would like to know using my other question how it comes that ViewController is begin processed first, this question might help to solve my issue that is also actually the case in my previous question – Salihan Pamuk Apr 27 '19 at 15:52
  • https://developer.apple.com/documentation/uikit/uiapplication/1648685-openurl?language=objc Call it like: `UIApplication.shared.openURL(....)` from anywhere.. – Brandon Apr 27 '19 at 15:55
  • Unrelated but `if user.url(forKey: "DeepLinkUrl") != nil { let str = user.value(forKey: "DeepLinkUrl") as! String` is horrible *unswifty* code – vadian Apr 27 '19 at 16:00
  • well this won't work, since my problem is that the url is already opened by scheme URL. I call the app back from an another app like openMyApp://printthistext. Then I need that text to print in the view, but somehow first my view gets called, before this URL is set in AppDelegate. Therefore, I cant use that text variable in my View. I Need that text actually in my View... – Salihan Pamuk Apr 27 '19 at 16:02
  • @vadian could you maybe please tell me then how I can get the parameters that I send from scheme URL in that function (appWillEnterForeground()) ? – Salihan Pamuk Apr 27 '19 at 16:04
  • I told you (on the other question) how to arrange things so that your code would be called in the desired order. There is no point repeating the question. – matt Apr 27 '19 at 17:17

1 Answers1

0

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.

Bhavesh Nayi
  • 3,626
  • 1
  • 27
  • 42
gnasher729
  • 51,477
  • 5
  • 75
  • 98