How to Use Deeplink dynamically in Webview iOS application?

45 Views Asked by At

I created a webview application on the iOS side. I used the universal link structure for deeplink. If the user's phone has the application installed, the application will open. No problem so far. However, the initial web page always opens: https://example.com/account

I want whatever link the user clicks on to open in the webview. I've been dealing with this problem for a few hours. But unfortunately I couldn't find a solution.

In the application, the ViewController.swift file is loaded first for spashscreen. Then, it is directed to the AbcViewController.swift file.

My code working is below:

--- apple-app-site-association file on the server:---

{
    "applinks": {
        "details": [{
            "appIDs": ["example.com.abc.abc"],
            "components": [{
                "/": "*",
                "comment": "Matches all URLs"
            }]
        }]
    }
}

--- AppDelegate.swift file ---

var universalLinkURL: String = ""

func application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: @escaping ([UIUserActivityRestoring]?) -> Void) -> Bool {
    guard userActivity.activityType == NSUserActivityTypeBrowsingWeb,
        let url = userActivity.webpageURL else {
        print(universalLinkURL)
        return false
    }
    let universalURL = url.absoluteString
    universalLinkURL = universalURL
    print(universalURL, universalLinkURL)
    return true
}

--- AbcViewController.swift file ---

import UIKit
import WebKit
import Lottie
class AbcViewController: UIViewController {
    @IBOutlet weak var webView: WKWebView!
    var urlToLoad :URL?
    override func viewDidLoad() {
        webView.navigationDelegate = self
        guard let url = URL(string: "https://example.com/account") else { return }
        
        // Load URL if provided
        if let url = urlToLoad {
           webView.load(URLRequest(url: url))
         }
        var request = URLRequest(url: url)
       
        webView.allowsBackForwardNavigationGestures = true
        webView.allowsLinkPreview = true
        webView.load(request)
            
    }
    
    private func animationSTR() {
        animationView = .init(name: "loading")
        /*...*/
        // Set completion block to remove animationView from superview
        animationView!.play { [weak self] _ in
            self?.animationView?.removeFromSuperview()
        }
    }
    
    extension AbcViewController: WKNavigationDelegate {
        func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) {
            animationSTR()
        }
    }
}

--- ViewController.swift file ---

@objc func changeVC(){
    let storyboard  = UIStoryboard(name: "Main",bundle: nil)
    let vc          = storyboard.instantiateViewController(withIdentifier: "AbcViewController") as! AbcViewController
    vc.modalPresentationStyle = .automatic
    vc.modalTransitionStyle = .crossDissolve
    
    // Get the url from AppDelegate here
    let appDelegate = UIApplication.shared.delegate as! AppDelegate
    let universalurl = appDelegate.universalLinkURL
    
    if (!universalurl.isEmpty)
    {
        let urluniversal = URL(string: universalurl)
        vc.urlToLoad = urluniversal
    }
    
    self.present(vc, animated: true)
}
0

There are 0 best solutions below