A Word About Deep Linking

Something that I wanted to capture today, and has been around for quite some time, is this little feature called ‘deep linking’. If you already know everything about deep linking in iOS please feel free to go ahead and skip the below investigation I was able to dig up.

Deep linking specifically refers to the ability to reference specific app content purely through special URI schemes. In a nutshell, if I’ve went and engineered something with deep linking, I’d be able to click a link , have it open the app, and execute some behavior – rendering content, awarding some points, something of that nature.

In theory it’s straightforward, but it actually has quite a number of different paths that need to be fully baked. See below for some lessons of the day:

1. Deep Linking for iOS

The first thing to think about is what the iOS part looks like for deep linking to work. It really comes down to two parts:

Part 1 – Your AppDelegate file needs to handle the openURL scheme. It’s important to remember that this gets executed only if your app is running in the background. If not, then this is not a reliable place to put in actions and handlers that need to be executed when a link is activated.

  func application(application: UIApplication, openURL url: NSURL, sourceApplication: String?, annotation: AnyObject?) -> Bool {

    let parsedURL : BFURL = BFURL(inboundURL: url, sourceApplication: sourceApplication)

    let targetURL : String = parsedURL.targetURL.absoluteString!

    handleURLRedirect(targetURL) // Or whatever

Part 2 – Your AppDelegate file needs to handle the didFinishLaunchingWithOptions function. It’s here that, when your app is launched from a clean state, you can listen and catch the URLs coming . See below:

  func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

if launchOptions != nil {

let opts : NSDictionary = launchOptions!

let url : AnyObject? = opts.objectForKey(UIApplicationLaunchOptionsURLKey)

if url != nil {

let urlParsed : NSURL = url as! NSURL

let targetURL : String = urlParsed.absoluteString!

handleURLRedirect(targetURL)

I’ll certainly be the first to say that the above reeks of the Swift 1.2 Optional Pyramid of Doom – but really, this is just for illustrative purposes. The key takeaway is that your URL scheme is hidden away under launchOptions.

Part 3 – Setup your schemes in your project file

You need to configure your project to handle a custom scheme. I pulled the steps from this SO question, but it essentially is:

  • Open “Supporting Files” (folder) on left and click the “YourAppName-Info.plist”
  • Choose a row like “Bundle creator OS Type Code” and mouse over row and click the (+) symbol
  • This creates a new row and type “URL types”
  • Click the arror to left and see Item 0 and you’ll rename the value in Item 0 to “URL Schemes” as shown
  • Then edit the field in Item 0 and type in your prototocol; I typed in “whatever” as shown

This will effectively create a scheme for whatever://

http://stackoverflow.com/questions/8201724/how-to-register-a-custom-app-opening-url-scheme-with-xcode-4

2. Know your Schemes

This is really the simple part. I don’t need to say much on it. Stuff would look like:

earbits://something/{track-id}

rooster://something/else/{book-id}

Something like the above.

https://developer.apple.com/library/ios/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/Inter-AppCommunication/Inter-AppCommunication.html

https://developers.google.com/app-indexing/android/app?hl=en

3. Don’t Forget to Setup Facebook!

Hey, did you know the Facebook Developer Center has a whole section on App Links? Turns out App Links is Facebook’s terminology for dealing with deep linking.

https://developers.facebook.com/docs/applinks/ios

While the documentation isn’t entirely clear. Be rest assured of this:

  • Go to your App Settings
  • Look for the toggle that says “Enable Deep Linking” and switch that to on
  • Fetch your iOS App Store IDs and populate them
  • Voila

4. Last and Certainly Not Least – Route your Links

The final piece of the puzzle lies on the opposite end. If you have a bunch of links you can already share – how do you manage it so that users who view the page on mobile AND have the app installed, can automagically be redirected to your app’s content page? Well, one JavaScript way can be found here. I won’t go into the details since JS isn’t my strength, but you can find the code snippet easily enough here:

http://stackoverflow.com/questions/9688319/how-to-redirect-from-mobile-safari-to-native-ios-app-like-quora

Conclusion

AAaaand…there you have it. With the above, you hopefully have enough resources at your disposal to get your deep linking off the ground. Oh yeah – the good news is it pretty much works the same way against Android too – URI scheme and all. As I learn more, I’ll write more – but hopefully this is a sufficient start for knowing exactly what you need to do.