My app is successfully opening and setting the parameters (from URL-scheme i.e. myApp://sometextToPrint) to a variable in the AppDelegate
class but whenever I want to process them it fails at the first time when the app is opened from that URL. I have the app in foreground checker that calls the function for printing the parameters given, but somehow it looks like the AppDelegate
is loaded later than the view why the view fails to print the parameters at first load.
My code looks as follow:
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
}
ViewController.m
override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(self, selector: #selector(appWillEnterForeground),name: UIApplication.willEnterForegroundNotification, object: nil)
}
@objc func appWillEnterForeground() {
print("app on foreground")
let user = UserDefaults.standard
if user.url(forKey: "DeepLinkUrl") != nil {
let str = user.value(forKey: "DeepLinkUrl") as! String
print(str) //this is printed on the second time when I click home button and open app again
}
}
For e.g., if I go to Safari and hit the following URL: myApp://printthistext, the text is not being printed. It prints when I click the home button and click the app again.
P.s. I am new at swift and don't know exactly yet which class (AppDelegate or ViewContoller) is loaded/processed first. I am already trying almost 5 hours to fix this, still keeps printing for the second time.