Add custom URL scheme support to your app target
As already mentioned in Integration requirements, we use custom URL schemes to unlock the restricted functionality of your app for Setapp users. To add a URL scheme, follow these steps:
- In Xcode, go to the Info tab of your target settings.
- Expand the URL Types section.
- Add a new URL Type with the following parameters:
- Identifier:
Setapp
- URL Schemes: your bundle identifier
- Role:
None
- Identifier:
Handle requests to open URL
Once the URL scheme setup is complete, you can proceed with adding the necessary code to handle the process of opening URLs in your app.
If you have UIApplicationDelegate
in your app, add the following code to the application(_:, open:, options:)
function:
import Setapp
class AppDelegate: UIResponder, UIApplicationDelegate {
func application(
_ app: UIApplication,
open url: URL,
options: [UIApplication.OpenURLOptionsKey: Any] = [:]
)
-> Bool
{
if SetappManager.shared.canOpen(url: url) {
return SetappManager.shared.open(url: url, options: options) { result in
switch result {
case let .success(setappSubscription):
print("Successfully unlocked new features!")
print("Setapp subscription:", setappSubscription)
case let .failure(error):
print("Failed to unlock new app features due to the error:", error)
}
}
}
return false
}
}
If you have UIWindowSceneDelegate
in your app, add the following code to these functions:
scene(_:, willConnectTo:, options:)
scene(_:, openURLContexts:)
import Setapp
class SceneDelegate: UIResponder, UIWindowSceneDelegate {
func scene(
_ scene: UIScene,
willConnectTo session: UISceneSession,
options connectionOptions: UIScene.ConnectionOptions
)
{
SetappManager.shared.start(with: .default)
if SetappManager.shared.canOpen(urlContexts: connectionOptions.urlContexts) {
SetappManager.shared.open(urlContexts: connectionOptions.urlContexts) { result in
switch result {
case let .success(setappSubscription):
print("Successfully unlocked new features!")
print("Setapp subscription:", setappSubscription)
case let .failure(error):
print("Failed to unlock new app features due to the error:", error)
}
}
}
}
func scene(
_ scene: UIScene,
openURLContexts URLContexts: Set<UIOpenURLContext>
)
{
if SetappManager.shared.canOpen(urlContexts: URLContexts) {
SetappManager.shared.open(urlContexts: URLContexts) { result in
switch result {
case let .success(setappSubscription):
print("Successfully unlocked new features!")
print("Setapp subscription:", setappSubscription)
case let .failure(error):
print("Failed to unlock new app features due to the error:", error)
}
}
}
}
}
If you have SwiftUI app lifecycle, add the following code to your root view's onOpenURL
view modifier:
import SwiftUI
import Setapp
@main
struct SampleApp: App {
var body: some Scene {
WindowGroup {
NavigationStack {
ContentView()
}
#if os(iOS)
.onOpenURL { url in
if SetappManager.shared.canOpen(url: url) {
let result = SetappManager.shared.open(url: url, options: [:])
switch result {
case let .success(setappSubscription):
print("Successfully unlocked new features!")
print("Setapp subscription:", setappSubscription)
case let .failure(error):
print("Failed to unlock new app features due to the error:", error)
}
}
}
#endif
}
}
}
Updated 3 months ago