#3 iOS — Reinventing view controller navigation
Handle Universal Links Like a Boss.
URLs are everywhere. On our screens, billboards, in the systems, in our minds. Literally everywhere. They contain valuable information needed to perform certain operations.
In iOS, Apple added the support for universal linking. This is a very cool feature but so far it required a lot of custom code being written. Some remarks to be shared:
- URLs are changeable which can raise issues with URL matching inside an app (requires an app update)
- URLs can contain some pieces of information in their paths
- URLs can be shortened and search engine friendly
Your Swift code has a high probability to be bloated if the website that has to be linked with an app has complex URL formats that are weirdly generated.
We are gonna make an DeepLinkingExample app, which is an extended PassingDataExample app. It will demonstrate how to deal with these issues using CoreNavigation framework. Let’s get started!
1. Download and prepare DeepLinkingExample project
2. Create mockable account
mockable is a simple configurable service to mock out RESTful API or SOAP web-services.
3. Create apple-app-site-association
on mockable
{
"applinks": {
"apps": [],
"details": [
{
"appID": "YOUR_TEAM_ID.org.corenavigation.DeepLinkingExample",
"paths": ["*"]
}
]
}
}
Replace YOUR_TEAM_ID
with your Apple Dev team id.
4. Configure target capabilities
Add mockable domain to target’s Associated Domains
capabilities section in the following format:
applinks:MOCKABLE_DOMAIN
Note: Domain defined in this screenshot won’t work because it does not return correct apple-app-site-association
file that matches your team id. You have to configure your own domain and ensure apple-app-site-association
file is available on the network.
Use case
- App has one view controller
ColorViewController
which can receiveUIColor
& has color picker which triggers navigation to the same view controller type and passes chosen color as data to the new instance. - App can open a deep-link URL which contains hex string in its path.
- App should map hex string to
UIColor
and pass it toColorViewController
. - App should open
ColorViewController
with appropriate color displayed in background using following URL formats:
https://DOMAIN/color/COLOR_HEX_STRING
http://DOMAIN/colour/COLOR_HEX_STRING
https://DOMAIN/color/COLOR_HEX_STRING
http://DOMAIN/colour/COLOR_HEX_STRING
Destination
It’s nothing more than a simple view controller wrapper that provides some additional features and simplifies enrichment of truncated data (the one extracted from URL).
Destination
‘s resolve method is a place where parameters from URL are mapped to data destination view controller can receive. Such destinations need to be registered along with URL pattern strings. These are used in CoreNavigation’s route matching mechanism.
App delegate
Pay attention to registerRoutes()
method in AppDelegate
class. It says that any URL matching any of the strings in the passed array should resolve to Color
destination type.
All the parameter placeholders starting with :
(colon) character will be replaced with extracted data from the URL and passed to Color
‘s resolove(context:)
method.
Formats to define param placeholder:
:PARAMETER_NAME
or
:PARAMETER_NAME(REGEX_MATCH_PATTERN)
Accessing extracted parameters:
let parameter = context.parameters?["PARAMETER_NAME"] as? String
In this example, route patterns are hardcoded. However, they can be generated on the server side and provided to an app so it will always stay in sync with the website and its URLs.
NSUserActivity navigation extension
CoreNavigation supports navigation to URL
type, so just navigate to it.
Happy coding!