0
votes

i created an homemade PWA app in Swift 4 to browse my site with some options like Firebase and Notifies
I need open my App when my site is open in Safari or when a user click an Universal Link shared by a Friend.
I correctly setup my site with apple-app-site-association and it works, when i open the relative url in Safari it shows me the hint to open my app.
But the problem is when i try to handle the universal link when my app is opened between the hint appeared in Safari, I tried using this(as Apple documentation says universal-link)

func application(_ application: UIApplication,
          continue userActivity: NSUserActivity,
          restorationHandler: @escaping ([UIUserActivityRestoring]?) -> Void) -> Bool{

To change my WKWebView i used the code based on this answer.
Here is my AppDelegate code inside the func continue to change WebView url:

if(userActivity.webpageURL != nil){
    g_url = userActivity.webpageURL // g_url is declared at top as 'var g_url: URL?'
    let notificationName = NSNotification.Name(rawValue: "updateUrl")
    NotificationCenter.default.post(name: notificationName, object: nil)
    return true
}else{
    return false
}

Here is the code to set the url inside ViewController:

let appDelegate = UIApplication.shared.delegate as! AppDelegate
url = appDelegate.g_url

let notificationName = Notification.Name("updateUrl")
NotificationCenter.default.addObserver(self, selector: #selector(ViewController.updateUrl), name: notificationName, object: nil)

updateUrl()


updateUrl: @objc func updateUrl(){

    let appDelegate = UIApplication.shared.delegate as! AppDelegate
    if(appDelegate.g_url != nil){
        url = appDelegate.g_url!
    }else{
        url = URL(string: "https://my.Site/Home")!
    }
    let request = URLRequest(url: url)
    MyWebView.load(request)

    MyWebView.navigationDelegate = self

When app is called by Safari the WebView page is the last one opened or 'Home' if its the first time it opens.
I need to understand if I missed something or if I should use something other to make this

1

1 Answers

0
votes

The code wasnt working because of latest updates of IOS 13. The continue function in AppDelegate was not getting called but adding the continue function inside SceneDelegate it worked. I get url params with an old mode( working well with only one Scene):

private(set) static var shared: SceneDelegate?
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
    guard let _ = (scene as? UIWindowScene) else { return }
    Self.shared = self
}

and inside my ViewController code i set

let appDelegate = SceneDelegate.shared?.g_url

to get the variable and and this Observer to call the change

let notificationName = Notification.Name("updateUrl")
    NotificationCenter.default.addObserver(self, selector: #selector(ViewController.updateUrl), name: notificationName, object: nil)