iOS应用如何监听浏览器内按钮点击?能否使用Universal Links或Deep Linking?
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 } }
Can You Use Universal Links or Deep Linking?
Absolutely—Universal Links are actually a better alternative to custom URL schemes for many cases. Here’s why and how:
Why Universal Links?
- 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.
Setting Up Universal Links for Your Button
- 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. - Create the Apple App Site Association File: Host a file named
apple-app-site-association(no extension) athttps://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 vs Universal Links
- 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

