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

iOS应用如何监听浏览器内按钮点击?能否使用Universal Links或Deep Linking?

Listening to Browser Button Clicks from Your iOS App & Exploring Alternatives

First off, let's get your custom URL scheme setup working properly to catch that browser button tap. Here's what you need to adjust:

Step 1: Make the Browser Button Trigger Your Custom Scheme

The button in your web page needs to navigate to a URL using your app's custom scheme. For example, if your scheme is myapp, the button's HTML should look like this:

<button onclick="window.location.href='myapp://button-tapped'">Tap to Notify App</button>

This tells the browser to open your app (or prompt the user if it’s installed) with that specific URL path.

Step 2: Handle the URL in Your iOS App

Your current openURL method just returns true—you need to parse the incoming URL to detect it’s the button click event.

For iOS 12 and below (AppDelegate):

func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {
    // Check if the URL matches your button trigger path
    if url.scheme == "myapp" && url.host == "button-tapped" {
        // Do something when the button is clicked—like show an alert or update UI
        print("Browser button was tapped!")
        return true
    }
    return false
}

For iOS 13+ (SceneDelegate):

If your app uses scenes, handle this in the SceneDelegate instead:

func scene(_ scene: UIScene, openURLContexts URLContexts: Set<UIOpenURLContext>) {
    guard let url = URLContexts.first?.url else { return }
    if url.scheme == "myapp" && url.host == "button-tapped" {
        print("Browser button clicked detected!")
        // Perform your app logic here
    }
}

Absolutely—Universal Links are actually a better alternative to custom URL schemes for many cases. Here’s why and how:

  • Seamless experience: No "Open in [App]" prompt if the app is installed; it opens directly.
  • Fallback to web: If the app isn’t installed, the link opens your web page instead of showing an error.
  • Secure: Apple verifies the link comes from your domain, so no other app can hijack your links.
  1. Enable Associated Domains: In your Xcode project, go to your target’s "Signing & Capabilities" tab, add the "Associated Domains" capability, and add a domain like applinks:yourdomain.com.
  2. Create the Apple App Site Association File: Host a file named apple-app-site-association (no extension) at https://yourdomain.com/.well-known/apple-app-site-association. It should look like this:
{
    "applinks": {
        "apps": [],
        "details": [
            {
                "appID": "TEAM_ID.BUNDLE_ID",
                "paths": [ "/button-trigger/*" ]
            }
        ]
    }
}

Replace TEAM_ID and BUNDLE_ID with your actual Apple Team ID and app bundle ID.
3. Update Your Web Button: Make the button link to a path on your domain covered by the Universal Link, e.g., https://yourdomain.com/button-trigger/tapped.
4. Handle the Link in Your App:

  • In AppDelegate (iOS 12-):
    func application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: @escaping ([UIUserActivityRestoring]?) -> Void) -> Bool {
        if userActivity.activityType == NSUserActivityTypeBrowsingWeb {
            guard let url = userActivity.webpageURL else { return false }
            // Check if the URL is your button trigger path
            if url.path.contains("/button-trigger/tapped") {
                print("Universal Link button tap detected!")
                // Your logic here
                return true
            }
        }
        return false
    }
    
  • In SceneDelegate (iOS 13+):
    func scene(_ scene: UIScene, continue userActivity: NSUserActivity) {
        if userActivity.activityType == NSUserActivityTypeBrowsingWeb {
            guard let url = userActivity.webpageURL else { return }
            if url.path.contains("/button-trigger/tapped") {
                print("Universal Link button clicked!")
                // Perform app actions here
            }
        }
    }
    
  • Custom Scheme: Easier to set up, but has a user prompt and no fallback if the app isn’t installed.
  • Universal Links: More work to configure, but provides a smoother user experience and better security.

Choose based on your needs—if you want the best user experience, go with Universal Links. If you need a quick setup, custom schemes work perfectly fine.

内容的提问来源于stack exchange,提问作者Suvarna Ratna iOS

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.27 07:18:54