Notifications
Article
Deep Linking on Android & iOS with Unity
Published 2 years ago
6.8 K
2
Introduction
So you want to setup Mobile Deep Linking in Unity? I recently implemented deep linking for my Unity Asset Plugin Spotify4Unity which allows users to go through the Implicit Authorization of OAuth using Deep Linking as it's redirect url. With this method, you remove the need of having an external client or site that redirects back to your app.

Before you begin

Depending on which mobile platform you use, you'll need to know how to make a Deep Link URL. A url is created using two parts. A scheme and a unique path. The scheme is a consistent name in lower case that identifies your application. The unique path is just a specific and unique path we can use after the scheme to identify where the user is entering our application from. Both of these pieces of information are joined with a "://" to identify that this is a deep link url.
As an example, here are what I will use through this tutorial • Scheme: "spotify4unity" • Unique path: "oauth/callback/spotify/" Together, my deep link url would be "spotify4unity://oauth/callback/spotify/".
Before moving onto the platform specific steps, make sure an external element can redirect users to your app. Whether that is a button on a website with your Deep Link URL or the "redirect_url" parameter of OAuth to your Deep Link URL

Android Deep Linking

Let's start with Android, which is definitely the easiest to configure. All you need to have is an intent filter inside your "AndroidManifest.xml" file which contains your Deep Link URL and a couple of intent categories and actions.
If you don't already have an "AndroidManifest.xml" file, create one in the "/Assets/Plugins/Android/" folder of your project. Also, if you have overridden the UnityPlayerActivity with your own custom .jar, you need to replace the "com.unity3d.player.UnityPlayerActivity" activity name with your namespace and class name.
Below is the code needed for this, with comments. You need to replace the second intent filter scheme with your scheme name.
<!-- Deep Link Android Manifest https://github.com/JoshLmao --> <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android"> <application android:icon="@drawable/app_icon" android:label="@string/app_name"> <!-- Activity "android:name" refering to Unity's class which handles opening the app on default start If you have created your own class that overrides the UnityPlayerActivity, you need to replace the name property with your namespace and class for it--> <activity android:name="com.unity3d.player.UnityPlayerActivity" android:label="@string/app_name" android:launchMode="singleTask"> <!-- Intent to open the application normally --> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> <!-- Intent to open the app through a scheme--> <intent-filter> <!-- Enter your scheme here, must be all lower case--> <data android:scheme="myscheme" /> <action android:name="android.intent.action.VIEW" /> <category android:name="android.intent.category.DEFAULT" /> <category android:name="android.intent.category.BROWSABLE" /> </intent-filter> </activity> </application> </manifest>
That's all there is to it! Simple and easy, right? 😉

iOS Deep Linking

iOS has some more setup required but doesn't require too much more work. For this, you need to have a custom script inside Unity that can call a method and then receive data. (Don't worry, examples below 😊)
First thing you need is to create a "bridge file" inbetween iOS and your Unity application. To do this, create a new file at the path "/Assets/Plugins/iOS/" called "DeepLinkDelegate.mm". Open that file and copy and paste all the code from below inside and save.
/* DeepLinkDelegate - iOS Bridge https://github.com/JoshLmao */ #import "UnityAppController.h" @interface DeepLinkDelegate : UnityAppController @property (nonatomic, copy) NSString* gameObjectName; @property (nonatomic, copy) NSString* methodName; - (void) configure: (const char*) gameObjName: (const char*) methodName; @end // Set Spotify4UnityDelegate to be the loaded UnityAppController IMPL_APP_CONTROLLER_SUBCLASS(Spotify4UnityDelegate) @implementation DeepLinkDelegate // Override the openURL method to send the url and parameters to Spotify4Unity service -(BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options { const char *URLString = [url.absoluteString UTF8String]; const char *GameObjString = [_gameObjectName cStringUsingEncoding:NSASCIIStringEncoding]; const char *MethodNameString = [_methodName cStringUsingEncoding:NSASCIIStringEncoding]; UnitySendMessage(GameObjString, MethodNameString, URLString); return [super application:app openURL:url options:options]; } // Sets the deep link callback to be sent to this specific game object and method - (void) configure: (const char*)gameObjName: (const char*)methodName { _gameObjectName = CreateNSString(gameObjName); _methodName = CreateNSString(methodName); } NSString* CreateNSString(const char* str) { if(str) return [NSString stringWithUTF8String: str]; else return [NSString stringWithUTF8String: ""]; } @end extern "C" { // Delegate the method down to the appDelegate class void configure(const char* gameObjName, const char* methodName) { DeepLinkDelegate *appDelegate = (DeepLinkDelegate *)[UIApplication sharedApplication].delegate; [appDelegate configure: gameObjName: methodName]; } }
After that comes the custom Unity script I talked about earlier. For this example, let's create a new empty script. (You can add the code into one of your scripts after)
Create a new C# script and open it up. Then, we need to add the "configure" method (with lower case) from the bridge. To do that, copy and paste the following code and place it in your script.
[System.Runtime.InteropServices.DllImport("__Internal")] private static extern void configure(string gameObjName, string methodName);
Then we need to call that method to setup the callback from the bridge. Inside "Start()", paste the following
configure(this.gameObject.name, "OnDeepLinkURLOpened");
Then, we need to create the "OnDeepLinkURLOpened" method to actually receive the data. So lets do that! Paste the following
public void OnDeepLinkURLOpened(string deepLinkUrl) { // Callback method for recieveing Deep Link on iOS }
The "deepLinkURL" parameter of the method will be any deep link url that get's called into your application so you will need to check the string for your unique path. Below is a full example script if you had any problems following along
/// Deep Link iOS Listener /// https://github.com/JoshLmao/ using UnityEngine; public class ExampleDeepLinkListener : MonoBehaviour { /// Name of your scheme public string Scheme = "myscheme"; /// Your unique URL path that is after "scheme://" public string UniquePath = "callback/oauth/"; [System.Runtime.InteropServices.DllImport("__Internal")] private static extern void configure(string gameObjName, string methodName); private void Start() { // Configure bridge to callback to this game object and the OnDeepLinkURLOpened method configure(this.gameObject.name, "OnDeepLinkURLOpened"); } public void OnDeepLinkURLOpened(string deepLinkUrl) { // Callback method for recieveing Deep Link on iOS if (string.IsNullOrEmpty(deepLinkUrl)) return; if(deepLinkUrl.Contains(Scheme + "://" + UniquePath)) { // This string is your deep link url // Code goes here to handle it } } }
Finally, set your build settings to iOS and open your Player Settings. Then find "Other Settings" and inside, find the "Supported URL schemes" property and add your "Scheme" to the list. After that, you are complete! (phew)

Conclusion

Credit to Team QuadraTron for their article helped me originally figure it out for iOS. Also credit to Bennibayer on Github who helped me figure out how to do it all through the AndroidManifest.xml
If you have any questions about this, ask me on Twitter @JoshLmao
Josh Shepherd
Games Programmer - Programmer
5
Comments
PD
Piyush Damania
9 months ago
Hey Josh ,is the android deep link still working ?
0
ina
a year ago
incessant creator
IMPL_APP_CONTROLLER_SUBCLASS will generally only work if this is your only iOS plugin . If you are using Vuforia and other plugins in addition to your url scheme plugin, you'll have to extend those classes or do method switching.
0