F
F
Fanis X2021-05-22 19:59:53
iOS
Fanis X, 2021-05-22 19:59:53

How to pass a universal link to an iOS app controller?

Made PWA want to shove it inside an iOS application, but there is no experience in creating such applications.

With the help of Google and Apple documentation for developers, we managed to create a simple application.
My site this application opens. I also performed a number of preparatory measures so that when you click on the link to the site, the application opens.

The problem is that only the "hard-coded" main page opens, but I want the link that the user wants to go to to open.

Question:
1) If the application is not launched yet: How can I make the application launch and open the required page of the site when clicking on the link?
2) If the application is already running: How can I make the application open the required page of the site when clicking on the link?

Please help with links or a piece of code. The google options turned out to be difficult to understand since this is the very first application and I see swift and xcode for the first time)

My ViewController:

import UIKit
import WebKit
class ViewController: UIViewController, WKUIDelegate {
    
    var webView: WKWebView!
    
    override func loadView() {
        let webConfiguration = WKWebViewConfiguration()
        webView = WKWebView(frame: .zero, configuration: webConfiguration)
        webView.uiDelegate = self
        view = webView
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        
        let myURL = URL(string:"https://my-pwa-site.com/web/")
        let myRequest = URLRequest(url: myURL!)
        webView.load(myRequest)
    }
}


My AppDelegate:
import UIKit

@main
class AppDelegate: UIResponder, UIApplicationDelegate {



    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.
        return true
    }

    // MARK: UISceneSession Lifecycle

    func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {
        // Called when a new scene session is being created.
        // Use this method to select a configuration to create the new scene with.
        return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role)
    }

    func application(_ application: UIApplication, didDiscardSceneSessions sceneSessions: Set<UISceneSession>) {
        // Called when the user discards a scene session.
        // If any sessions were discarded while the application was not running, this will be called shortly after application:didFinishLaunchingWithOptions.
        // Use this method to release any resources that were specific to the discarded scenes, as they will not return.
    }


}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Roman Tutubalin, 2021-05-25
@gagarin67

Good afternoon
First of all, you should add domain support on the site, this is a JSON file (doc below):
https://developer.apple.com/documentation/Xcode/su...
Next, in the AppDelegate of your application, you need to describe the method that will process activity:
func application(_ application: NSApplication,
continue userActivity: NSUserActivity, restorationHandler
: @escaping ([NSUserActivityRestoring]) -> Void) -> Bool /xcode/su... )
After getting the URL, you can access the rootViewController of the current UIWindow (property var window: UIWindow of your AppDelegate), cast it to your ViewController and pass it the URL, in this case you can do this (window?.rootViewController as? ViewController)?.webView. load(URLRequest(url: url))

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question