|
| 1 | +// |
| 2 | +// SceneDelegate.swift |
| 3 | +// AppClip-sample |
| 4 | +// |
| 5 | +// Created by Ihar Yalavoi on 18.12.25. |
| 6 | +// Copyright © 2025 Dmitry Smolyakov. All rights reserved. |
| 7 | +// |
| 8 | + |
| 9 | +import UIKit |
| 10 | + |
| 11 | +class SceneDelegate: UIResponder, UIWindowSceneDelegate { |
| 12 | + |
| 13 | + // MARK: - Constants |
| 14 | + |
| 15 | + private static let pendingURLKey = "AppClipPendingURL" |
| 16 | + private static let maxRetries = 10 |
| 17 | + private static let initialRetryDelay: TimeInterval = 0.1 |
| 18 | + |
| 19 | + // MARK: - Properties |
| 20 | + |
| 21 | + var window: UIWindow? |
| 22 | + private var pendingURL: URL? |
| 23 | + |
| 24 | + // MARK: - UIWindowSceneDelegate |
| 25 | + |
| 26 | + func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) { |
| 27 | + if let userActivity = connectionOptions.userActivities.first, |
| 28 | + userActivity.activityType == NSUserActivityTypeBrowsingWeb, |
| 29 | + let url = userActivity.webpageURL { |
| 30 | + pendingURL = url |
| 31 | + } |
| 32 | + connectionOptions.urlContexts.first.map { pendingURL = $0.url } |
| 33 | + } |
| 34 | + |
| 35 | + func sceneDidBecomeActive(_ scene: UIScene) { |
| 36 | + if let url = pendingURL { |
| 37 | + pendingURL = nil |
| 38 | + handleURLWithRetry(url, in: scene, retryCount: 0) |
| 39 | + } else if let qrCodeURLString = UserDefaults.standard.string(forKey: Self.pendingURLKey), |
| 40 | + let qrCodeURL = URL(string: qrCodeURLString) { |
| 41 | + UserDefaults.standard.removeObject(forKey: Self.pendingURLKey) |
| 42 | + handleURLWithRetry(qrCodeURL, in: scene, retryCount: 0) |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + func scene(_ scene: UIScene, continue userActivity: NSUserActivity) { |
| 47 | + guard userActivity.activityType == NSUserActivityTypeBrowsingWeb, |
| 48 | + let url = userActivity.webpageURL else { return } |
| 49 | + handleURLWithRetry(url, in: scene, retryCount: 0) |
| 50 | + } |
| 51 | + |
| 52 | + private func handleURLWithRetry(_ url: URL, in scene: UIScene, retryCount: Int) { |
| 53 | + DispatchQueue.main.async { |
| 54 | + guard let windowScene = scene as? UIWindowScene, |
| 55 | + let window = windowScene.windows.first, |
| 56 | + let rootViewController = window.rootViewController else { |
| 57 | + return self.retryIfNeeded(url: url, scene: scene, retryCount: retryCount) |
| 58 | + } |
| 59 | + |
| 60 | + let viewController: ViewController? = { |
| 61 | + if let navController = rootViewController as? UINavigationController { |
| 62 | + return navController.viewControllers.first as? ViewController |
| 63 | + } |
| 64 | + return rootViewController as? ViewController |
| 65 | + }() |
| 66 | + |
| 67 | + guard let viewController = viewController, viewController.isViewLoaded else { |
| 68 | + UserDefaults.standard.set(url.absoluteString, forKey: Self.pendingURLKey) |
| 69 | + return self.retryIfNeeded(url: url, scene: scene, retryCount: retryCount) |
| 70 | + } |
| 71 | + |
| 72 | + UserDefaults.standard.removeObject(forKey: Self.pendingURLKey) |
| 73 | + viewController.setConfigURL(url) |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + private func retryIfNeeded(url: URL, scene: UIScene, retryCount: Int) { |
| 78 | + guard retryCount < Self.maxRetries else { return } |
| 79 | + let delay = Self.initialRetryDelay * pow(2.0, Double(retryCount)) |
| 80 | + DispatchQueue.main.asyncAfter(deadline: .now() + min(delay, 1.0)) { |
| 81 | + self.handleURLWithRetry(url, in: scene, retryCount: retryCount + 1) |
| 82 | + } |
| 83 | + } |
| 84 | +} |
0 commit comments