Universal Link handler not called in the first open

15 views Asked by At

Hello I'm trying to handle universal links in my app and works properly on background mode when I launch the app and then open a link, the problem is when I try to open a link the first time the app is running. My app is not responding to my handling. This is my code

extension AppDelegate {
    

    
    //MARK: Universal Links Handler
    func application(_ application: UIApplication,
                     continue userActivity: NSUserActivity,
                     restorationHandler: @escaping ([UIUserActivityRestoring]?) -> Void) -> Bool
    {
        // Get URL components from the incoming user activity.
        guard userActivity.activityType == NSUserActivityTypeBrowsingWeb,
              let incomingURL = userActivity.webpageURL,
              let _ = NSURLComponents(url: incomingURL, resolvingAgainstBaseURL: true) else {
            return false
        }
        
        let result = extractHostAndURL(from: incomingURL.absoluteString)
        
        if let host = result.host, let url = result.url {
            _ = handleLinks(host: host, url: url)
            return true
        } else {
            print("could not fund host or URL")
            return false
        }
    }
    
    //MARK: Universal Link Actions
    /// This function extracts the host and URL from a string.
    private func extractHostAndURL(from urlString: String) -> (host: String?, url: URL?) {
        
        if let urlComponents = URLComponents(string: urlString) {
            let host = urlComponents.host
            let url = urlComponents.url
            return (host, url)
        } else {
            return (nil, nil)
        }
    }
    
    func handleLinks(host: String?, url: URL) -> Bool {
       print("my function only works on background mode here!")
    }
    
}
0

There are 0 answers