11

I implemented Universal Links in our iOS 9 app and they work by calling a method in AppDelegate.swift, in which I get an NSUserActvity with an URL attached to it.

Is there a way to get the (HTTP-) referer? I need to know on which website the user has tapped the link that opened the app.

Evgeny Karkan
  • 8,782
  • 2
  • 32
  • 38
scrrr
  • 5,135
  • 7
  • 42
  • 52

4 Answers4

5

You should be able to use NSUserActivity > referrerURL instance property. See https://developer.apple.com/documentation/foundation/nsuseractivity/2875762-referrerurl.

Art
  • 88
  • 1
  • 8
  • This seems to work! Thanks Art. Note to others: The property is not available before iOS 11. – scrrr Aug 16 '18 at 13:23
  • 1
    This only works when redirecting from a website from a broswer? Is there a solution when coming from another app? Like Source Application in open url method. – Shivam Mishra Mar 11 '19 at 07:44
  • 4
    This does not seem to work. The property is there, but it is always `nil`, whether coming from a website or another app. – chadmoone May 29 '19 at 00:23
  • @chadmoone from my experience it works for some web pages and doesn't work for others at the same time. Eg - I was able to get `referrerURL` from Safari Gmail where I put my universal link, but doing so in Safari Yahoo Mail, Safari Telegram - gives me `nil`. Also was able to get it from Facebook and Twitter web pages. Have no clue how it works under the hood. – Evgeny Karkan Jul 21 '21 at 22:57
3

No, there is no way to get the referrer. An Apple employee made that pretty clear on the developer forums: https://forums.developer.apple.com/thread/65423

They say that the only way to get some kind of referrer would be to append it to the URL. Maybe take a look Google's campaign tracking URLs (utm_...).

Cornelius
  • 4,214
  • 3
  • 36
  • 55
1

I found another interesting solution if you need to support iOS 9 and 10 - https://appmetrica.yandex.com/blog/referrer-based-tracking-dlya-ios-tochnaya-atributsiya-dlya-lyubogo-istochnika-trafika.

Since iOS 11 I guess can be used NSUserActivity > referrerURL.

In short:

  • SFSafariViewController uses the same cookies as Safari app.
  • You can open invisible SFSafariViewController inside your app.
  • If your site contains some cookie - use Universal Link to pass it back to the app.

Though it requires to implement something like tracking link on your site.

Mikhail
  • 4,271
  • 3
  • 27
  • 39
0

Yes, as said above you can get the referrer url through an NSUserActivity via the referrerURL.

But you may found nil values for this referrerURL depending the referrer policy established by the website you used for the redirection.

To understand we need to have a look at the Referrer policies. These policies are used to secure the way of how the referrer urls are passed from a website A to another website B.

For example in some cases, website A don't want that website B be able to get the details parameters of the referrer URL so they can defined a particular type of policy. Sometimes they can completely make the referrer url null to website B by setting the no-referrer policy type for example.

This policy is defined into an HTTP header named Referrer policy and currently by default have the following type: strict-origin-when-cross-origin. You can check actually what policy is used by the source website by opening the inspector console > network > select the website url and check the Referrer policy HTTP header.

You can have a look at the different kinds of policies and what rules they apply here

For example in our case, we were not able to get the referrer URL using the safari top banner that allows users to be redirected into our app. But using a specific link such as a link from Twitter or Slack or whatever, was providing a referrer URL.

AnthonyR
  • 3,485
  • 1
  • 18
  • 41