Home > OS >  Universal links not working correctly in IOS App - Not opening the correct page
Universal links not working correctly in IOS App - Not opening the correct page

Time:05-09

I have an IOS App that requires a member login. I've set up a separate page for new members to log in. When I email them to confirm their new account, I want the link in the email to go to the login page in the app, which is "newlogin.php'. While testing this with Safari on my iPhone and iPad, I have tried almost everything to get this to work, but the link keeps going to the home page in the app, which is mydomain.com.

In the Associated Domains section of Xcode, I entered: applinks:mydomain.com.

Here is my apple-app-site-association file, which is in both the root directory and the .well-known directory:

{
  "applinks": {
    "apps": [],
    "details": [
    {
      "appID": "TEAMID.com.-mydomain.MyDomain",
      "paths": ["/newlogin.php"]
    }
    ]
  }
} 

And here is the function for the AppDelagate file that I have been struggling with for several days:

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

At this point, I have no clue what to put in the function where the question marks (???) are entered, in order to make this go to domain.com/newlogin.php. Please, someone help me. I've been at this for several days, and it's starting to get to me. Thank you very much in advance.

CodePudding user response:

Assuming you're not using SceneDelegate's, You need to register both delegate functions in your AppDelegate.swift file.:

// MARK: - URL
    func application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: @escaping ([UIUserActivityRestoring]?) -> Void) -> Bool {
        guard let incomingURL = userActivity.webpageURL else { return false }
        // Do something with `incomingURL`
    }

    func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey: Any] = [:]) -> Bool {
        // Do something with `url`
        return true
    }

CodePudding user response:

First you will check what is the activity type where you will know you are coming from web or not.

Then get url from where you come and do necessary actions based on url.

Check below code.

func application(
    _ application: UIApplication,
    continue userActivity: NSUserActivity,
    restorationHandler: @escaping ([UIUserActivityRestoring]?
    ) -> Void) -> Bool {
    
    
    if userActivity.activityType == NSUserActivityTypeBrowsingWeb {
        let url = userActivity.webpageURL!
        print("The url : " ,url)
        
        // now do what you want with url
        check if url contains .login.php, take user to login page in your app.

    }
    return false
}
  • Related