You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在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).
  • Next, head to the Signing & Capabilities tab (enable it via Editor > Add Capability if you don't see it).
  • Add the Associated Domains capability, then click + under Domains and enter your domain in the format applinks:yourdomain.com (replace yourdomain.com with the exact domain you want to intercept, like applinks: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_ID with your Apple Developer Team ID (find it in your Developer Account or Xcode's Signing & Capabilities tab).
  • Swap com.yourcompany.yourapp with your app's actual bundle ID.
  • The paths array defines which links to intercept: use /* to block all links on the domain, /protected/* for a specific path, or /critical-link for an exact URL.
  • Host this file at either https://yourdomain.com/apple-app-site-association or https://yourdomain.com/.well-known/apple-app-site-association. Ensure it's served over HTTPS with the application/json Content-Type header.

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 paths configuration (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), use application(_:open:options:) in AppDelegate or scene(_:openURLContexts:) in SceneDelegate.

内容的提问来源于stack exchange,提问作者NiteIsAFK

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 04:23:34