0

I am working on Firebase dynamic links in the Swift iOS app. Added the deep link in Associated domains and wrote code for receiving active links in the AppDelegate file.

The app opens when the user clicks on the dynamic links. I want to navigate to a particular page based on the parameters in the URL. No function related to the deep links is calling in the app delegate file to see the logs. I added the scene delegate file too. That is also not working. I deleted the app and reinstalled the Xcode to check. Nothing is working.

// Deep Linking Start in AppDelegate
   
    // Respond to URI scheme links
    func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {

        print("printing open url", url)
        let urlComponents = URLComponents(url: url, resolvingAgainstBaseURL: true)
        let host = urlComponents?.host ?? ""
        print("printing host", host)
        print("open url")

        if let dynamicLink = DynamicLinks.dynamicLinks().dynamicLink(fromCustomSchemeURL: url) {
            print("dynamicLink",dynamicLink)
            self.handleIncomingDynamicLink(dynamicLink)
            return true
        }
        else{
            return false
        }
    }

    // Respond to Universal Links
    private func application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: @escaping ([UIUserActivityRestoring]?) -> Void) -> Bool {
       
       if let incomingURL = userActivity.webpageURL{
       
           print("Incoming URL is \(incomingURL)")
           
           let linkHandled = DynamicLinks.dynamicLinks().handleUniversalLink(incomingURL)
           {(dynamicLink, error) in
               guard error == nil else{
                   print("Found an error! \(error!.localizedDescription)")
                   return
               }
               if let dynamicLink = dynamicLink{
                   self.handleIncomingDynamicLink(dynamicLink)
               }
           }
           if linkHandled{
               return true
           }
           else{
               return false
           }
       }
       return false
   }
   
   func handleIncomingDynamicLink(_ dynamicLink: DynamicLink){
       
       guard let url = dynamicLink.url else{
           print("That's weird. My dynamic link object has no url")
           return
       }
       print("Your incoming link parameter is \(url.absoluteString)")
       guard (dynamicLink.matchType == .unique || dynamicLink.matchType == .default) else{
           print("Not a strong enough match type to continue")
           return
       }
       //parse the link parameter
       guard let components = URLComponents(url: url, resolvingAgainstBaseURL: false),
       let queryItems = components.queryItems else{
           return
       }
       
       print("components",components)
       print("components.path",components.path);
       print("queryItems",queryItems)
       
       if components.path == "/ShareId"{
           if let QueryItem1 = queryItems.first(where: {$0.name == "ShareId"}){
               guard let OrderID = QueryItem1.value else{ return }
               
               print("OrderID",OrderID)
               
               userDefaultss.set(OrderID, forKey: "DeelpLinkOrderId");
               userDefaultss.synchronize()
               
               Auth.auth().addStateDidChangeListener() { auth , user in
                   if user != nil
                   {
                       print("Not nill")
                       
                       let presentViewController = self.MAIN_SB.instantiateViewController(withIdentifier: "View_Controller") as! View_Controller
                       presentViewController.ShareId = OrderID
                       self.window?.rootViewController = presentViewController
                   }
                   else{
                       print("User nill")
                   }
               }
           }
       }
   }

End of AppDelegate

// Code in SceneDelegate
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
       
        print("entering here")
    
        if let userActivity = connectionOptions.userActivities.first {
           if let incomingURL = userActivity.webpageURL {
               self.handleUniversalLink(url: incomingURL)

           }
       }
            
    guard (scene is UIWindowScene) else { return }
       
    }
    func handleUniversalLink(url: URL)
    {
        print("handled")
        DynamicLinks.dynamicLinks().handleUniversalLink(url) { dynamicLink, error in
            guard error == nil,
                  let dynamicLink = dynamicLink,
                  let urlString = dynamicLink.url?.absoluteString else {
                return
            }
            // print("Dynamic link host: \(host)")
            print("Dyanmic link url: \(urlString)")
            UserDefaults.standard.removeObject(forKey: "ShareId")
            
            let matchtype = dynamicLink.matchType.rawValue
            print("matchtype \(matchtype)")
            
            if(urlString.contains("ShareId"))
            {
                var components = dynamicLink.url?.pathComponents
                
                if let groupid = components?.popLast()
                {
                    print("groupid scence \(String(describing: groupid))")
                    UserDefaults.standard.set(groupid, forKey: "ShareId")
                    
                    let storyboard = UIStoryboard(name: "Main", bundle: nil)
                    let viewController = storyboard.instantiateViewController(withIdentifier: "ViewAutoProfileViewController") as! ViewAutoProfileViewController
                    
                    viewController.KiddoId = groupid
                    let rootVC = UIApplication.shared.windows.first!.rootViewController
                    rootVC?.present(viewController, animated: true)
                
                }
                
            }
            else{
                if(urlString != "")
                {
                    let storyboard = UIStoryboard(name: "Main", bundle: nil)
                    let viewController = storyboard.instantiateViewController(withIdentifier: "View_Controller") as! View_Controller
                    self.window?.rootViewController = viewController
                }
            }
            
         
            
        }
    }
    func scene(_ scene: UIScene, continue userActivity: NSUserActivity) {
        
        print("Continue here in scene")
            
            guard userActivity.activityType == NSUserActivityTypeBrowsingWeb,
                let url = userActivity.webpageURL,
                let host = url.host else {
                    return
            }
            print("url here outside \(url)")

            DynamicLinks.dynamicLinks().handleUniversalLink(url) { dynamicLink, error in
                guard error == nil,
                    let dynamicLink = dynamicLink,
                    let urlString = dynamicLink.url?.absoluteString else {
                        return
                }
                print("Dynamic link host: \(host)")
                print("Dyanmic link url: \(urlString)")
                UserDefaults.standard.removeObject(forKey: "ShareId")
                
                let matchtype = dynamicLink.matchType.rawValue
                print("matchtype \(matchtype)")
                
                if(urlString.contains("ShareId"))
                {
                    var components = dynamicLink.url?.pathComponents
                   
                    if let groupid = components?.popLast()
                    {
                        print("groupid scence \(String(describing: groupid))")
                        UserDefaults.standard.set(groupid, forKey: "ShareId")
                        let storyboard = UIStoryboard(name: "Main", bundle: nil)
                        let viewController = storyboard.instantiateViewController(withIdentifier: "ViewAutoProfileViewController") as! ViewAutoProfileViewController
                        
                        viewController.KiddoId = groupid
                        let rootVC = UIApplication.shared.windows.first!.rootViewController
                        rootVC?.present(viewController, animated: true)
                    }
                   
                }
                else{
                    
                    if(urlString != "")
                    {
                        let storyboard = UIStoryboard(name: "Main", bundle: nil)
                        let viewController = storyboard.instantiateViewController(withIdentifier: "View_Controller") as! View_Controller
                        
                        self.window?.rootViewController = viewController
                    }
                
                   
                }
                
                // Handle deep links
               // self.handleDeepLink(urlString: urlString)
                
                print("Dynamic link match type: \(dynamicLink.matchType.rawValue)")
            }
        
        }
   
 
    func scene(_ scene: UIScene, openURLContexts URLContexts: Set<UIOpenURLContext>) {
        print("asdfg")
        for context in URLContexts {
            
            print("printing open url", context.url)
            let urlComponents = URLComponents(url: context.url, resolvingAgainstBaseURL: true)
            let host = urlComponents?.host ?? ""
            print("printing host", host)
            print("open url")
            
            if let dynamicLink = DynamicLinks.dynamicLinks().dynamicLink(fromCustomSchemeURL: context.url) {
                print("dynamicLink",dynamicLink)
                self.handleUniversalLink(url: dynamicLink.url!)
                // return true
            }
            else{
                // return false
            }
            
        }
    }

enter image description here

enter image description here

I don't know what I am missing in this. I worked on this for days to figure out the issue. Tried so many ways. Any help and suggestions?

2
  • are you using a SceneDelegate in your app? Commented Sep 15, 2023 at 6:30
  • Yes, I am using it. Updated to code with scene delegate. Commented Sep 15, 2023 at 6:43

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.