'Universal Links redirection to specific view
I'm having a hard time understanding how universal Link works. First I have tried redirection to a specific UIViewController (Universal link works as it redirects the user from button click in email to my app) from the SceneDelegate but it ain't working. I also noticed that the URL is not handled in appDelegate and I have no idea why.
Here is a snippet from the function in AppDelegate :
func application(_ application: UIApplication,
continue userActivity: NSUserActivity,
restorationHandler: @escaping ([UIUserActivityRestoring]?) -> Void) -> Bool
{
print("am I even here ?")
guard userActivity.activityType == NSUserActivityTypeBrowsingWeb,
let incomingURL = userActivity.webpageURL,
let components = NSURLComponents(url: incomingURL, resolvingAgainstBaseURL: true) else {
return false
}
// Check for specific URL components that you need.
guard let path = components.path,
let params = components.queryItems else {
return false
}
print("path = \(path)")
if let userId = params.first(where: { $0.name == "_id" } )?.value,
let token = params.first(where: { $0.name == "token" })?.value {
print("_id = \(userId)")
print("token = \(token)")
return true
} else {
print("Either INDEX ARE MISSING")
return false
}
}
Snippet from SceneDelegate :
func scene(_ scene: UIScene, continue userActivity: NSUserActivity) {
// the url is going to be in userActivity.webpageURL
print(userActivity.webpageURL)
print(userActivity.webpageURL?.pathComponents[2])
print(userActivity.webpageURL?.pathComponents[1])
if (userActivity.webpageURL?.lastPathComponent != nil ) {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = self.window?.rootViewController?.storyboard?.instantiateViewController(withIdentifier: "ViewController") as? ViewController
//queryVC?.urlQueryId = queryId!
self.window?.rootViewController?.present(vc!, animated: true, completion: nil)
print("am I moving ?")
}
}
I am sorry in advance, I'm still new to swift and IOS in general and it feels like I am stuck in a loop.
ps: the data from SceneDelagate prints, I get both parameters and the full URL too with parameteres.
Thank you,
Solution 1:[1]
Okey this was fixed, starting ios 13 and if you have scene delegate, that's where you should handle the response. This was how I fixed it.
if (userActivity.webpageURL?.pathComponents[2] != nil)
{
let userId = userActivity.webpageURL?.pathComponents[1]
let token = userActivity.webpageURL?.pathComponents[2]
if self.window?.rootViewController?.presentedViewController != nil {
self.window?.rootViewController?.dismiss(animated: false, completion: nil)
}
let Main = self.window?.rootViewController?.storyboard?.instantiateViewController(withIdentifier: "ResetPassword") as? ResetPasswordViewController
//Main.userIdentification = "fd"
Main?.userIdFromUrl = userId
Main?.UsertokenFromUrl = token
// Main.userToken = "aa"
Main?.modalPresentationStyle = .fullScreen
self.window?.rootViewController?.present(Main!, animated: true, completion: nil)
}
else {print("doesn't contain")}
userIdFromUrl and UsertokenFromUrl are too variables I initialized in my target view Controller. I hope this helps anyone. There were few tutorials mentioning the use of scene delegate and after reading through a dozen, I was eventually running around in cirlces.
You can also read more about it :
https://developer.apple.com/documentation/xcode/supporting-universal-links-in-your-app
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | ZahraCarthage CHOUCHENE |
