iOS Universal link not working in react-native iOS

4.6k Views Asked by At

Hi I know this question being asked before still I cannot figure out what am doing wrong.

  1. Added apple-app-site-association file in our server under https://ourserver.com/.well-known/apple-app-site-association
  2. The file content is as follows

`

{
   "applinks":{
      "apps":[],
      "details":[
         {
            "appID":"<teamId>.<bundleId>",
            "paths":[ "*",  "/" ]
         }
      ]
   }
}

` 3. Associated Domain is added as capability with

 applinks:servername.com
 applinks:dev.servername.com
  1. Added code in AppDeletegate.m

    - (BOOL)application:(UIApplication *)application
     openURL:(NSURL *)url
     options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options
    {
      return [RCTLinkingManager application:application openURL:url options:options];
     }
    
    
    - (BOOL)application:(UIApplication *)application continueUserActivity:(nonnull NSUserActivity *)userActivity
     restorationHandler:(nonnull void (^)(NSArray<id<UIUserActivityRestoring>> * _Nullable))restorationHandler
    {
     return [RCTLinkingManager application:application
                  continueUserActivity:userActivity
                    restorationHandler:restorationHandler];
    }
    

EDIT 1

Noticed something i tried universal linking in iOS versions 12.4.2 iPhone and it redirected correctly. Where as in iOS version 14 it redirected to our web page. So I updated my apple-app-site-association file as follows

{
   "applinks": {
      "details": [
         {
            "appIDs": [
               "teamid.bundleid"
            ],
            "components": [
               {
                  "/": "/*",
                  "comment": "Matches any URL whose path starts with URL_PATH and instructs the system not to open it as a universal link"
               },
               {
                  "/": "*",
                  "comment": "Matches any URL whose path starts with URL_PATH and instructs the system not to open it as a universal link"
               }
            ],
            "paths": [
               "*",
               "/"
            ]
         }
      ]
   }
}

oru web url is validated in this link

All the testing is done in real devices.

EDIT 2

All below is tested in Simulator - iPhone 11 Pro iOS 14.4

  1. Tested the same in a simulator with developer mode

    applinks:servername.com?mode=developer applinks:dev.servername.com?mode=developer

it opened correctly in SIMUALTOR

  1. then i misspelled our server name and clicked from iMessages it redirected to Safari in SIMULATOR -

  2. I tried again with correct server name after clearing Safari history and cache - STILL IT REDIRECTES TO BROWSER NOT APP

EDIT 3

On Simulator

Went through this link and saw about Smart App banner since i couldn't find anything like that. After searching found this SO Post and tried Settings - Safari - Advanced - Website Data - In the page showed my domain name - Clear Website Data. Tried again on Simulator and app opened correctly

On iPhone with release adhoc build

On Real Device did the same Settings - Safari - Advanced - Website Data - In the page showed my domain name - Clear Website Data but it didn't work. Still it opens the browser in app

EDIT 4

the AASA file in EDIT 2 make the redirection fail in iOS version 12.4.2. Hence updated it like this

{
   "applinks":{
      "apps":[
         
      ],
      "details":[
         {
            "appID":"teamid.bundleid",
            "paths":[
               "*",
               "/"
            ],
            "appIDs":[
               "teamed.bundleid"
            ],
            "components":[
               {
                  "/":"/*"
               },
               {
                  "/":"*"
               }
            ]
         }
      ]
   }
}

After updating to this redirects to app in version 12.4.2 but fails in 14 and above versions. Modified AASA file after going through this link

Any Help is Appreciated

Thanks in advance

2

There are 2 best solutions below

1
On

Try replacing "appID":".bundleid" with "appID":".bundleid" in your aasa file.

1
On

I was having the same issue and saw above @suja haven't resolved it yet so I thought to post the answer here.

First we need to double-check our <teamID>.<bundleID> pattern. Because if our association file doesn't recognize this if it's wrong (even though if we got all other steps correctly).

I found this useful official link and saw this video carefully. Also, the video slides can be seen here in PDF

Actually, they've updated the contents regarding association file, like

  1. appID: string_value key now turns into appIDs: array_of_strings
  2. the paths key has now been replaced by components
  3. If you want to test in Simulator during development, then you should use a query parameter mode=developer at the end of the matching URL in the Association Domains capability in XCode. enter image description here
  4. You cannot use http:// (like if you are launching your website locally using localhost:PORT then you should use some external service to make your localhost available as a public link). In my case, I did use ngrok to mock my localhost as https://, also don't forget to allow that ngrok URL in CORS policy, if web services (APIs) are being called from your web pages.
  5. apple-app-site-association must be in a public folder of your website under subdirectory .well-known

Now combine all steps. I was having the same issue since yesterday then I found the above links from the official Apple site to get this done and to understand how exactly Universal link works.

Most importantly, if you are testing on Simulator, then please make sure that from your Apple developer Account that your development appID.bundleID should be exact in the association file.

below is my association file as an example

{
    "applinks": {
        "details": [
            {
                "appIDs": [
                    "YOUR_PRODUCTION_TEAM_ID.com.yourapp_bundleID",
                    "YOUR_DEV_TEAM_ID.com.yourapp_bundleID.dev"
                ],
                "components": [
                    {
                        "/": "*",
                        "comment": "Matches any URLs"
                    }
                ]
            }
        ]
    }
}

And for production, don't forget to add the production URLs in association domains capability, as well as PROD_appID.PROD_bundleID in apple-app-site-association file. Remember this file should be accessible by apple at

https://your-web.com/.well-known/apple-app-site-association

That's it, and further to play around with query parameters as well as dynamic paths, I would suggest seeing the above PDF link and especially the video on the official Apple site!