Custom URL Schemes

The scheme name (or protocol) of a URL is the first part of a URL - e.g. schemename://. For web pages, the scheme is usually http (or https). The iPhone supports these URL schemes:

  • http, https, ftp    Web links* (Safari)
  • mailto    E-mail links (launches the Mail app)
  • tel    Telephone Numbers (launches the phone app)
  • sms    Text Messages (launches the SMS app)
  • Web links that point to http://maps.google.com are redirected to the Maps app.
  • Web links that point to http://www.youtube.com are redirected to the YouTube app.
  • iTunes store links (http://phobos.apple.com/etc..) are sent to the iTunes (or App store) app.

iPhone apps can also specify their own custom URL scheme (for example, myapp://doStuff). When might you want to use a custom URL scheme for your app?

  • To transfer data from lite to paid versions of your app
  • To allow other apps (or even web pages) to call your app (and send data to it)
  • To handle callbacks for custom authentication (such as OAuth) and third party API's

Implementing a Custom URL Scheme

First of all, you need to define custom URL schemes for your app. With IOS Native you can achieve this in a few steps.

  1. Open IOS Native Editor Settings.  Window -> Stan's Assets -> IOS Native -> Edit Settings
  2. Open IOS Deploy Tab
  3. Add as much schemes as you want fro your app as shown on a screenshot bellow:

Handling Custom URL Calls

To be able to subscribe for a native IOS App events you need to enable App Events API.

  1. Open IOS Native Editor Settings.  Window -> Stan's Assets -> IOS Native -> Edit Settings
  2. Enable App Events API as showed on a screenshot bellow

Since IOS APP controllers (App Delegate) is started before Unity player, you need to check if an app was started via URL in your game / app initialisation script. See the code snippet bellow:

var url = SA.IOSNative.Core.AppController.LaunchUrl;
if(!url.IsEmpty) {
	Debug.Log("App was started with URL: " + url.AbsoluteUrl);
}

You also need to subscribe to an OpenURL event in case app will be restored from a background with URL:

SA.IOSNative.Core.AppController.OnOpenURL += OnOpenURL;

void OnOpenURL(SA.IOSNative.Models.LaunchUrl url) {
	Debug.Log("Open URL Detecetd: " + url.AbsoluteUrl);
}

The lunch URL is represented as SA.IOSNative.Models.LaunchUrl model.

LaunchUrl

namespace SA.IOSNative.Models {
	public class LaunchUrl {

		public LaunchUrl (string absoluteUrl, string sourceApplication);

		//--------------------------------------
		//  Get / Set
		//--------------------------------------

		public bool IsEmpty {get;}
		public Uri URI {get;}
		public string Host {get;}
		public string AbsoluteUrl {get;}
		public string SourceApplication {get;}
	}
}