Saturday, March 16, 2024
HomeiOS Developmentswift - Use Deeplink dynamically in Webview iOS software?

swift – Use Deeplink dynamically in Webview iOS software?


I created a webview software on the iOS facet. I used the common hyperlink construction for deeplink. If the consumer’s telephone has the applying put in, the applying will open. No drawback thus far. Nonetheless, the preliminary net web page at all times opens: https://instance.com/account

I need no matter hyperlink the consumer clicks on to open within the webview. I have been coping with this drawback for a number of hours. However sadly I could not discover a answer.

Within the software, the ViewController.swift file is loaded first for spashscreen. Then, it’s directed to the AbcViewController.swift file.

My code working is under:

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

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

— AppDelegate.swift file —

var universalLinkURL: String = ""

func software(_ software: UIApplication, proceed 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
}

--- ViewController.swift file ---
@objc func changeVC(){
    let storyboard  = UIStoryboard(identify: "Most important",bundle: nil)
    let vc          = storyboard.instantiateViewController(withIdentifier: "AbcViewController") as! AbcViewController
    vc.modalPresentationStyle = .computerized
    vc.modalTransitionStyle = .crossDissolve
    
    // Get the url from AppDelegate right here
    let appDelegate = UIApplication.shared.delegate as! AppDelegate
    let universalurl = appDelegate.universalLinkURL
    
    if (!universalurl.isEmpty)
    {
        let urluniversal = URL(string: universalurl)
        vc.urlToLoad = urluniversal
    }
    
    self.current(vc, animated: 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://instance.com/account") else { return }
        
        // Load URL if offered
        if let url = urlToLoad {
           webView.load(URLRequest(url: url))
         }
        var request = URLRequest(url: url)
       
        webView.allowsBackForwardNavigationGestures = true
        webView.allowsLinkPreview = true
        webView.load(request)
            
    }
    
    personal func animationSTR() {
        animationView = .init(identify: "loading")
        /*...*/
        // Set completion block to take away animationView from superview
        animationView!.play { [weak self] _ in
            self?.animationView?.removeFromSuperview()
        }
    }
    
    extension AbcViewController: WKNavigationDelegate {
        func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) {
            animationSTR()
        }
    }
}



Supply hyperlink

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments