Swift iOS应用实现点击带哈希参数URL唤起并传参
Hey there! Let's get your forgot password deep linking sorted out for your Swift iOS app. It sounds like you've already tinkered with Info.plist but haven't hit the right setup yet—no worries, let's break this down step by step.
First, let's clarify: your link uses a # fragment (the #/user/token part) which needs special handling, and you want either a custom URL scheme or Universal Links to trigger your app. Universal Links are the better choice here since they work seamlessly with HTTPS URLs like your example, so we'll focus on that first, plus cover URL schemes as a fallback.
Step 1: Set Up Universal Links (Recommended)
Universal Links let users tap your HTTPS link and open directly into your app without any "Open in App" prompts. Here's how to configure them:
1.1 Add Associated Domains in Xcode
- Open your project, select your target, go to the Signing & Capabilities tab.
- Click the
+ Capabilitybutton and search for Associated Domains. - Add a domain entry in the format
applinks:example.com(replaceexample.comwith your actual domain).
1.2 Host the Apple App Site Association File
You need to place a file named apple-app-site-association (no file extension) on your server, either in the root directory or the .well-known subdirectory. The file should look like this:
{ "applinks": { "apps": [], "details": [ { "appID": "YOUR_TEAM_ID.YOUR_BUNDLE_ID", "paths": [ "/forgotPassword/*" ] } ] } }
- Replace
YOUR_TEAM_IDwith your Apple Developer Team ID (find it in your Developer Account), andYOUR_BUNDLE_IDwith your app's bundle identifier. - Make sure the file is served over HTTPS, has no redirects, and uses the
application/jsonMIME type.
Step 2: Handle the Link in Your App
Now we need to extract the user and token from the fragment when the app is opened. The code differs slightly depending on iOS version:
For iOS 13+ (SceneDelegate)
Add this to your SceneDelegate.swift:
func scene(_ scene: UIScene, openURLContexts URLContexts: Set<UIOpenURLContext>) { guard let incomingURL = URLContexts.first?.url else { return } processForgotPasswordLink(incomingURL) } private func processForgotPasswordLink(_ url: URL) { // Grab the fragment part after the # guard let fragment = url.fragment else { print("No fragment found in the link") return } // Split the fragment into components (e.g., "/user/token" becomes ["", "user", "token"]) let components = fragment.components(separatedBy: "/") // Extract user and token from the split components if components.count >= 3 { let userID = components[1] let resetToken = components[2] // Now do something with these values—like navigate to your reset password screen print("Received user: \(userID), token: \(resetToken)") // Example: Dispatch to main thread to update UI DispatchQueue.main.async { // Navigate to reset password view controller and pass userID/resetToken } } else { print("Invalid fragment format in link") } }
For iOS 12 and Below (AppDelegate)
Add this to your AppDelegate.swift:
func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool { processForgotPasswordLink(url) return true } private func processForgotPasswordLink(_ url: URL) { // Same fragment extraction logic as above guard let fragment = url.fragment else { print("No fragment found in the link") return } let components = fragment.components(separatedBy: "/") if components.count >= 3 { let userID = components[1] let resetToken = components[2] print("Received user: \(userID), token: \(resetToken)") // Handle navigation to reset password screen } else { print("Invalid fragment format in link") } }
Step 3: Fallback with URL Schemes (Optional)
If you need to support older setups or test locally, you can use a custom URL scheme:
3.1 Configure the Scheme in Info.plist
- Open your
Info.plistas source code, or use the property list editor. - Add a
CFBundleURLTypesarray, then add a dictionary inside it with:CFBundleURLSchemes: An array containing your custom scheme (e.g.,["myapp"])CFBundleURLName: Your app's bundle identifier (optional but recommended)
3.2 Test with a Custom Link
Use a link like myapp://forgotPassword#/testuser/testtoken—tap this in Safari, and your app should open, with the code above extracting the user and token correctly.
Testing Tips
- Universal Links require a real device—simulators don't fully support them.
- Verify your
apple-app-site-associationfile is accessible by visiting its URL directly in a browser to ensure it loads correctly. - If the app doesn't open, double-check your Team ID, Bundle ID, and the paths specified in the association file.
That should do it! Once you set this up, tapping your forgot password link will open your app and pass the user and token right to your code.
内容的提问来源于stack exchange,提问作者Adilson Sanches

