0

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!")
    }
    
}
1
  • //MARK: Universal Links Handler @objc(application:continueUserActivity:restorationHandler:) func application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: @escaping ([UIUserActivityRestoring]?) -> Void) -> Bool This worked for me: stackoverflow.com/questions/69538601/… Commented Jan 5 at 18:43

0

Browse other questions tagged or ask your own question.