iOS Universal Links and GET parameters
Asked Answered
A

3

19

we're trying to implement app indexing on iOS using the Apple Universal Links (I'm looking at https://developer.apple.com/library/ios/documentation/General/Conceptual/AppSearch/UniversalLinks.html#//apple_ref/doc/uid/TP40016308-CH12-SW2).

In the "Creating and Uploading the Association File" section I see I can limit the indexing to specific pages, which is good.

I'd like to limit the indexing to https://www.mywebsite.com?parameter=something, how can I?

I was thinking about something like that:

{
  "applinks": {
    "apps": [],
    "details": [
      {
        "appID": "MYID",
        "paths":[ "*?parameter=*" ]
      }
    ]
  }
}

Do you think it could work? I can't test it yet because it takes time to get the authorization for uploading files on the website root directory, that's why I'm asking you if you think it could work, I'd like to upload the file just once if I can.

Thank you

Alizaalizarin answered 17/2, 2016 at 13:29 Comment(0)
B
21

NO, Currently #(inline-links) and ?(query-parmeter) not supported by Universal Links. Apple not provided any format to support Inline-Links & Query-Parmeter in apple-app-site-association file.

In order to do indexing to https://www.mywebsite.com?parameter=something, I'm using the following JSON file.

{
  "applinks": {
    "apps": [],
    "details": [
      {
        "appID": "TEAMID.BUNDLEID",
        "paths":[ "*" ]
      }
    ]
  }
}

If you want to limit the indexing only to some parameter for example query_parmeter1 and query_parmeter2 then you need to handle this in UIApplicationDelegate method [UIApplicationDelegate application: continueUserActivity: restorationHandler:] something like this

Objective-C:

-(BOOL)application:(UIApplication *)application continueUserActivity:(NSUserActivity *)userActivity restorationHandler:(void (^)(NSArray * _Nullable))restorationHandler{
    if ([userActivity.activityType isEqualToString: NSUserActivityTypeBrowsingWeb]) {
        NSURL *url = userActivity.webpageURL;
        if ([url.query containsString:@"query_parmeter1"]) {
           //handle code for query_parmeter1
        }else if ([url.query containsString:@"query_parmeter2"]){
           //handle code for query_parmeter2
        }

        return YES;
    }
    return NO;
} 

Swift:

func application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: @escaping ([Any]?) -> Void) -> Bool {
    if userActivity.activityType == NSUserActivityTypeBrowsingWeb {
        let url = userActivity.webpageURL!
        let query = url.query ?? ""

        if query.contains("query_parmeter1") {
            // handle code for query_parmeter1
        } else if query.contains("query_parmeter2") {
            // handle code for query_parmeter2
        }

        return true
    }

    return false
}

Note: This trick won't prevent the app from opening when a link to the website is clicked. But you can check If URL meets your requirement or not if not then you can open your URL in the web browser again. Similar to Amazon App -

enter image description here

References - Handle query parameters in universal links

Barker answered 5/3, 2016 at 10:6 Comment(7)
This is an awesome answer for handling parameters in Universal Links. It might be worth mentioning the difference between Universal Links, App Indexing and indexing content for Spotlight Web Search, since the author apparently wants to do the latter (which is not automatic in a basic Universal Links implementation)Ardell
Thank you Vineet Choudhari. This is a good trick but it won't prevent the app from opening when a link to the website is clicked, right? How do you handle the website redirection in the default case, when none of the parameters is set? Thank youAlizaalizarin
@AlexBauer thank you for your comment and the links. Actually I don't care much about applebot but I'd like to make our app compliant with google App Indexing (developers.google.com/app-indexing). Fact is, I'm going crazy trying to achieve that. And the confusion between universal links, custom url, app indexing, mark up web content is growing every day :-/Alizaalizarin
@MarcoZanetti at Branch.io, we just started supporting app indexing via auto-generated content maps. Might be worth a look?Ardell
That is a great answer, thanks. But by specifying wildcard "*" the app launches all the time for every path. I would like to the app to be launched only if our internal param "app_deeplinking=true" is appended to the url. Currently I can't stop the app from launching even by specifying continueUserActivity function return as false. Any ideas?Disbar
@Disbar currently there is no way to do thatBarker
@Disbar i need the solution as well, please let me know if you have.Acrodont
P
18

In iOS 13, it's now possible to specify query and fragment values when defining universal link URLs. Sadly this isn't well documented by Apple, but it was mentioned in WWDC 2019: https://developer.apple.com/videos/play/wwdc2019/717/.

For your original example where you'd just like to match https://www.mywebsite.com?parameter=something, your apple-app-site-association file should look like:

{
  "applinks": {
    "apps": [],
    "details": [
      {
        "appID": "MYID",
        "components": [
          {
            "/" : "",
            "?": { "parameter": "something" }
          }
        ]
      }
    ]
  }
}

Note that you'll still need to specify a paths array if you want to continue supporting universal links in iOS 12, and iOS 13 devices will ignore the paths array if you specify a components array.

Praenomen answered 18/12, 2019 at 18:31 Comment(0)
W
3

For query parameters appended to a path off of the base domain (i.e. https://www.mywebsite.com/pathOffOfTheBaseDomain?parameter=something) use:

{
  "applinks": {
    "apps": [],
    "details": [
      {
        "appID": "TEAMID.BUNDLEID",
        "paths":[ "/pathOffOfTheBaseDomain" ]
      }
    ]
  }
}

According to Apple Universal Link documentation:

Note that only the path component of the URL is used for comparison. Other components, such as the query string or fragment identifier, are ignored.

The full URL will be ripe and ready for parsing in AppDelegate's continueUserActivity method.

Whitecap answered 3/3, 2017 at 21:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.