Custom URL schemes provide a way to reference inside your app. Users can open a custom URL in the browser or tap in any link. Other apps can also trigger your app to launch with specific context data; for example, a photo library app might display a specified image.
Custom URL scheme is one of the ways of deep linking, but apple strongly recommended universal deep linking. For more information on universal links, see Allowing Apps and Websites to Link to Your Content. Custom URL schemes are also being used to check if the application is installed or not.
Apple also supports common schemes associated with system apps, such as mailto
, tel
, sms
, and facetime
. You can define your own custom scheme and register your app to support it. URLs must start with your custom scheme name. Add parameters for any options your app supports.
To support a custom URL scheme:
- You have to define the format for your app’s URL.
- Register your scheme so that the system directs appropriate URLs to your app.
- Need to handle the URLs that your app receives and perform the smooth functionality.
Register Custom URL Scheme in the Project
URL scheme registration specifies which URLs to redirect to your app. Just follows below steps to add URL scheme:
- Open the project and then select the target.
- Select Info and go to the URL Types and click on ‘+’
- Now in the ‘URL Schemes’ field enter your custom URL in small letters and don’t use space.
Let’s suppose the example and app name is “CustomURLBlog” and use the “customurlblog” as a custom URL scheme.
Handle Incoming URLs
When the user click on the link or open it in any other broswer, it launches your app on the device in the foreground.
From iOS 13 and later below function will no longer work.
1 2 3 |
func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool { return true } |
Deep linking will work even your application is killed. This function will get called “openURLContexts”.
Let’s add changes in the SceneDelegate class file as:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
import UIKit class SceneDelegate: UIResponder, UIWindowSceneDelegate { var window: UIWindow? func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) { // Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`. // If using a storyboard, the `window` property will automatically be initialized and attached to the scene. // This delegate does not imply the connecting scene or session are new (see `application:configurationForConnectingSceneSession` instead). guard let _ = (scene as? UIWindowScene) else { return } let storyboard = UIStoryboard(name: "Main", bundle: nil) guard let rootVC = storyboard.instantiateViewController(identifier: "ViewController") as? ViewController else { print("ViewController not found") return } let rootNC = UINavigationController(rootViewController: rootVC) self.window?.rootViewController = rootNC self.window?.makeKeyAndVisible() } func scene(_ scene: UIScene, openURLContexts URLContexts: Set<UIOpenURLContext>) { if let url = URLContexts.first?.url { print(url) let urlStr = url.absoluteString //1 // Parse the custom URL as per your requirement. let component = urlStr.components(separatedBy: "=") // 2 if component.count > 1, let param = component.last { // 3 print(param) let topViewController = self.window?.rootViewController as? UINavigationController let currentVC = topViewController?.topViewController as? ViewController currentVC?.label.text = "Param Data : " + param } } } } |
Now just run the app and then open Safari and type your URL scheme “customurlblog://”. It works as
Now also check with passing parameters as:
Conclusion
I hope this blog will help you in understanding the behaviour of pan gestures, if you have any comments, questions, or recommendations, feel free to post them in the comment section below!
For other blogs, please click here.