如何在Xcode、Swift 4中设置iOS APP拦截指定链接并自动启动
Hey there! Let's walk through setting this up exactly like YouTube does—so your app automatically launches when users tap specific HTTP/HTTPS links, preventing them from opening in Safari by accident. This relies on Apple's Universal Links feature, which is the same mechanism YouTube uses for its domain. Here's your step-by-step guide:
1. Configure Xcode Project Settings
First, link your app to your target domain in Xcode:
- Open your project, select your app target, and go to the Info tab.
- Scroll down to URL Types and click the
+button:- For Identifier, use a unique string (usually your app's bundle ID, e.g.,
com.yourcompany.yourapp). - For URL Schemes, add a custom scheme like
yourapp(this is a fallback for testing, not strictly required for Universal Links).
- For Identifier, use a unique string (usually your app's bundle ID, e.g.,
- Next, head to the Signing & Capabilities tab (enable it via
Editor > Add Capabilityif you don't see it). - Add the Associated Domains capability, then click
+under Domains and enter your domain in the formatapplinks:yourdomain.com(replaceyourdomain.comwith the exact domain you want to intercept, likeapplinks:yourapp.com).
2. Create & Host the Apple App Site Association (AASA) File
This JSON file proves you own the domain and tells Apple which links your app should handle:
- Make a file named
apple-app-site-association(no file extension) with this structure:
{ "applinks": { "apps": [], "details": [ { "appID": "YOUR_TEAM_ID.com.yourcompany.yourapp", "paths": [ "/protected/*", "/critical-link" ] } ] } }
- Replace
YOUR_TEAM_IDwith your Apple Developer Team ID (find it in your Developer Account or Xcode'sSigning & Capabilitiestab). - Swap
com.yourcompany.yourappwith your app's actual bundle ID. - The
pathsarray defines which links to intercept: use/*to block all links on the domain,/protected/*for a specific path, or/critical-linkfor an exact URL. - Host this file at either
https://yourdomain.com/apple-app-site-associationorhttps://yourdomain.com/.well-known/apple-app-site-association. Ensure it's served over HTTPS with theapplication/jsonContent-Type header.
3. Handle Links in Your Swift 4 Code
Now make your app respond when launched via a Universal Link:
For iOS 12 & Earlier (AppDelegate):
Open AppDelegate.swift and add these methods:
func application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: @escaping ([UIUserActivityRestoring]?) -> Void) -> Bool { if userActivity.activityType == NSUserActivityTypeBrowsingWeb { guard let interceptedURL = userActivity.webpageURL else { return false } // Do something with the URL—e.g., navigate to a specific screen, or just log it print("Stopped user from opening: \(interceptedURL)") // Return true to confirm your app handled the link return true } return false }
For iOS 13+ (SceneDelegate):
If your app uses Scenes (common in newer iOS versions), add this to SceneDelegate.swift:
func scene(_ scene: UIScene, continue userActivity: NSUserActivity) { if userActivity.activityType == NSUserActivityTypeBrowsingWeb { guard let interceptedURL = userActivity.webpageURL else { return } // Handle the intercepted URL here print("Intercepted link: \(interceptedURL)") } }
4. Test the Setup
- You can only test Universal Links on a physical iOS device (simulators don't support this feature).
- Install your app on the device using a valid developer certificate.
- Open Safari and type a link that matches your
pathsconfiguration (e.g.,https://yourdomain.com/protected/test). - You'll see a banner asking to open the link in your app—tap "Open" to confirm it works.
Quick Notes
- If the app isn't installed, the link will open in Safari as usual (there's no way to block that entirely).
- Validate your AASA file using Apple's official App Search API Validation Tool to catch any formatting issues.
- If you need to handle custom scheme links (like
yourapp://settings), useapplication(_:open:options:)in AppDelegate orscene(_:openURLContexts:)in SceneDelegate.
内容的提问来源于stack exchange,提问作者NiteIsAFK

