|
1 | 1 | # Universal Links |
2 | 2 |
|
3 | | -- [Universal Links](#universal-links) |
4 | | - - [What are Universal Links?](#what-are-universal-links) |
5 | | - - [Setting Up an Entitlements File for Universal Links](#setting-up-an-entitlements-file-for-universal-links) |
6 | | - - [Steps to Configure the Entitlements File](#steps-to-configure-the-entitlements-file) |
7 | | - - [Points to Consider](#points-to-consider) |
8 | | - - [Configuring Universal Links for your Storefront](#configuring-universal-links-for-your-storefront) |
9 | | - - [Important: The catch-all route `*`](#important-the-catch-all-route-) |
10 | | - - [Handling Universal Links in your app](#handling-universal-links-in-your-app) |
11 | | - - [Security Considerations](#security-considerations) |
12 | | - - [Testing](#testing) |
13 | | - - [Troubleshooting](#troubleshooting) |
14 | | - - [Further Reading and Resources](#further-reading-and-resources) |
| 3 | +- [What are Universal Links?](#what-are-universal-links) |
| 4 | +- [Setting Up an Entitlements File for Universal Links](#setting-up-an-entitlements-file-for-universal-links) |
| 5 | + - [Steps to Configure the Entitlements File](#steps-to-configure-the-entitlements-file) |
| 6 | + - [Points to Consider](#points-to-consider) |
| 7 | +- [Configuring Universal Links for your Storefront](#configuring-universal-links-for-your-storefront) |
| 8 | +- [Important: The catch-all route `*`](#important-the-catch-all-route-) |
| 9 | +- [Handling Universal Links in your app](#handling-universal-links-in-your-app) |
| 10 | +- [Security Considerations](#security-considerations) |
| 11 | +- [Testing](#testing) |
| 12 | +- [Troubleshooting](#troubleshooting) |
| 13 | +- [Further Reading and Resources](#further-reading-and-resources) |
15 | 14 |
|
16 | 15 | ## What are Universal Links? |
17 | 16 |
|
@@ -141,121 +140,7 @@ by "NOT" rules. |
141 | 140 |
|
142 | 141 | ## Handling Universal Links in your app |
143 | 142 |
|
144 | | -```swift |
145 | | -func handleUniversalLink(url: URL) { |
146 | | - let storefrontUrl = StorefrontURL(from: url) |
147 | | - |
148 | | - switch true { |
149 | | - /// Checkout URLs |
150 | | - case storefrontUrl.isCheckout() && !storefrontUrl.isThankYouPage(): |
151 | | - presentCheckout(url) |
152 | | - /// Cart URLs |
153 | | - case storefrontUrl.isCart(): |
154 | | - navigateToCart() |
155 | | - /// Open everything else in Safari |
156 | | - default: |
157 | | - if UIApplication.shared.canOpenURL(url) { |
158 | | - UIApplication.shared.open(url) |
159 | | - } |
160 | | - } |
161 | | -} |
162 | | - |
163 | | -// This code is meant as example only. |
164 | | -public struct StorefrontURL { |
165 | | - public let url: URL |
166 | | - |
167 | | - init(from url: URL) { |
168 | | - self.url = url |
169 | | - } |
170 | | - |
171 | | - public func isThankYouPage() -> Bool { |
172 | | - return url.path.range(of: "/thank[-_]you", options: .regularExpression) != nil |
173 | | - } |
174 | | - |
175 | | - public func isCheckout() -> Bool { |
176 | | - return url.path.contains("/checkout") |
177 | | - } |
178 | | - |
179 | | - public func isCart() -> Bool { |
180 | | - return url.path.contains("/cart") |
181 | | - } |
182 | | -} |
183 | | -``` |
184 | | - |
185 | | -<details> |
186 | | -<summary><strong>Handling Universal Links in SwiftUI</strong></summary> |
187 | | - |
188 | | -To handle universal links in a SwiftUI application, you can use the `onOpenURL` |
189 | | -modifier provided by SwiftUI. This will allow you to specify an action to |
190 | | -perform when a URL is opened by your app. |
191 | | - |
192 | | -```swift |
193 | | -// App.swift |
194 | | -import SwiftUI |
195 | | - |
196 | | -@main |
197 | | -struct MyApp: App { |
198 | | - var body: some Scene { |
199 | | - WindowGroup { |
200 | | - ContentView() |
201 | | - .onOpenURL { url in |
202 | | - handleUniversalLink(url: url) |
203 | | - } |
204 | | - } |
205 | | - } |
206 | | - |
207 | | - private func handleUniversalLink(url: URL) { |
208 | | - // Handle the incoming universal link URL |
209 | | - print("Universal link opened: \(url)") |
210 | | - |
211 | | - // Use the URL to navigate within your app, update state, etc. |
212 | | - } |
213 | | -} |
214 | | -``` |
215 | | - |
216 | | -</details> |
217 | | - |
218 | | -<details> |
219 | | -<summary><strong>Handling Universal Links in a UIViewController-based App</strong></summary> |
220 | | - |
221 | | -In an app using UIViewController, handling universal links can be done by |
222 | | -implementing the application`(_:continue:restorationHandler:)` method in your |
223 | | -`AppDelegate`. This method gets triggered when a universal link is opened. |
224 | | - |
225 | | -```swift |
226 | | -// AppDelegate.swift |
227 | | -import UIKit |
228 | | - |
229 | | -@UIApplicationMain |
230 | | -class AppDelegate: UIResponder, UIApplicationDelegate { |
231 | | - |
232 | | - var window: UIWindow? |
233 | | - |
234 | | - func application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: @escaping ([UIUserActivityRestoring]?) -> Void) -> Bool { |
235 | | - if userActivity.activityType == NSUserActivityTypeBrowsingWeb, |
236 | | - let url = userActivity.webpageURL { |
237 | | - handleUniversalLink(url: url) |
238 | | - return true |
239 | | - } |
240 | | - return false |
241 | | - } |
242 | | - |
243 | | - private func handleUniversalLink(url: URL) { |
244 | | - // Handle the incoming universal link URL |
245 | | - print("Universal link opened: \(url)") |
246 | | - |
247 | | - // You can use the URL to navigate within your app or perform relevant actions |
248 | | - let rootViewController = window?.rootViewController as? UINavigationController |
249 | | - |
250 | | - // Replace the following content with relevant actions for your app |
251 | | - let storyboard = UIStoryboard(name: "Main", bundle: nil) |
252 | | - let viewController = storyboard.instantiateViewController(withIdentifier: "DetailViewController") |
253 | | - rootViewController?.pushViewController(viewController, animated: true) |
254 | | - } |
255 | | -} |
256 | | -``` |
257 | | - |
258 | | -</details> |
| 143 | +See https://github.com/Shopify/checkout-sheet-kit-react-native/blob/main/sample/src/App.tsx for sample code to implement Universal Links in your app. |
259 | 144 |
|
260 | 145 | ## Security Considerations |
261 | 146 |
|
|
0 commit comments