|
| 1 | +/* |
| 2 | + Modified MIT License |
| 3 | + |
| 4 | + Copyright 2025 OneSignal |
| 5 | + |
| 6 | + Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | + of this software and associated documentation files (the "Software"), to deal |
| 8 | + in the Software without restriction, including without limitation the rights |
| 9 | + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | + copies of the Software, and to permit persons to whom the Software is |
| 11 | + furnished to do so, subject to the following conditions: |
| 12 | + |
| 13 | + 1. The above copyright notice and this permission notice shall be included in |
| 14 | + all copies or substantial portions of the Software. |
| 15 | + |
| 16 | + 2. All copies of substantial portions of the Software may only be used in connection |
| 17 | + with services provided by OneSignal. |
| 18 | + |
| 19 | + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 20 | + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 21 | + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 22 | + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 23 | + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 24 | + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 25 | + THE SOFTWARE. |
| 26 | + */ |
| 27 | + |
| 28 | +import WidgetKit |
| 29 | +import ActivityKit |
| 30 | +import SwiftUI |
| 31 | + |
| 32 | +@available(iOS 16.1, *) |
| 33 | +extension DynamicIsland { |
| 34 | + /// Sets the URL that opens the corresponding app of a Live Activity when a user taps on the Live Activity. |
| 35 | + /// Sets OneSignal activity metadata. See Important callout below on usage. |
| 36 | + /// |
| 37 | + /// By setting the URL with this function, it becomes the default URL for deep linking into the app |
| 38 | + /// for each view of the Live Activity. However, if you include a |
| 39 | + /// <doc://com.apple.documentation/documentation/swiftui/link> in the Live Activity, |
| 40 | + /// the link takes priority over the default URL. When a person taps on the `Link`, it takes them to the |
| 41 | + /// place in the app that corresponds to the URL of the `Link`. |
| 42 | + /// |
| 43 | + /// - Parameters: |
| 44 | + /// - url: The URL that opens the app. |
| 45 | + /// - context: The activity view context. |
| 46 | + /// |
| 47 | + /// - Returns: The configuration object for the Dynamic Island with the specified URL. |
| 48 | + /// |
| 49 | + /// > Important: Use instead of`.widgetURL`. Requires handling from your app's URL handling code |
| 50 | + /// (e.g., `application(_:open:options:)` in AppDelegate or `onOpenURL` in SwiftUI) using the |
| 51 | + /// `OneSignal.LiveActivities.trackClickAndReturnOriginal(url)` method. |
| 52 | + public func onesignalWidgetURL<T: OneSignalLiveActivityAttributes>( |
| 53 | + _ url: URL?, |
| 54 | + context: ActivityViewContext<T> |
| 55 | + ) -> DynamicIsland { |
| 56 | + return self.widgetURL(generateTrackingDeepLink(originalURL: url, context: context)) |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +@available(iOS 16.1, *) |
| 61 | +extension View { |
| 62 | + /// Sets the URL to open in the containing app when the user clicks the widget. |
| 63 | + /// Sets OneSignal activity metadata. See Important callout below on usage. |
| 64 | + /// |
| 65 | + /// - Parameters: |
| 66 | + /// - url: The URL to open in the containing app. |
| 67 | + /// - context: The activity view context. |
| 68 | + /// - Returns: A view that opens the specified URL when the user clicks |
| 69 | + /// the widget. |
| 70 | + /// |
| 71 | + /// Widgets support one `onesignalWidgetURL` modifier in their view hierarchy. |
| 72 | + /// If multiple views have `onesignalWidgetURL` modifiers, the behavior is undefined. |
| 73 | + /// |
| 74 | + /// > Important: Use instead of`.widgetURL`. Requires handling from your app's URL handling code |
| 75 | + /// (e.g., `application(_:open:options:)` in AppDelegate or `onOpenURL` in SwiftUI) using the |
| 76 | + /// `OneSignal.LiveActivities.trackClickAndReturnOriginal(url)` method. |
| 77 | + public func onesignalWidgetURL<T: OneSignalLiveActivityAttributes>(_ url: URL?, context: ActivityViewContext<T>) -> some View { |
| 78 | + return self.widgetURL(generateTrackingDeepLink(originalURL: url, context: context)) |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +// MARK: - Helper Function |
| 83 | + |
| 84 | +@available(iOS 16.1, *) |
| 85 | +private func generateTrackingDeepLink<T: OneSignalLiveActivityAttributes>(originalURL: URL?, context: ActivityViewContext<T>) -> URL? { |
| 86 | + // Generate a unique click ID |
| 87 | + let clickId = UUID().uuidString |
| 88 | + |
| 89 | + // Get activity metadata |
| 90 | + let activityId = context.attributes.onesignal.activityId |
| 91 | + let activityType = String(describing: T.self) |
| 92 | + |
| 93 | + // Build OneSignal tracking URL |
| 94 | + var components = URLComponents() |
| 95 | + components.scheme = "onesignal-liveactivity-click" |
| 96 | + components.host = "track" |
| 97 | + components.path = "/click" |
| 98 | + |
| 99 | + var queryItems = [ |
| 100 | + URLQueryItem(name: "clickId", value: clickId), |
| 101 | + URLQueryItem(name: "activityId", value: activityId), |
| 102 | + URLQueryItem(name: "activityType", value: activityType) |
| 103 | + ] |
| 104 | + |
| 105 | + if let originalURL = originalURL { |
| 106 | + queryItems.append(URLQueryItem(name: "redirect", value: originalURL.absoluteString)) |
| 107 | + } |
| 108 | + |
| 109 | + components.queryItems = queryItems |
| 110 | + |
| 111 | + return components.url |
| 112 | +} |
0 commit comments