求助:如何用cordova-universal-links-plugin实现浏览器跳转至Cordova应用
Absolutely, you can use cordova-universal-links-plugin to achieve this deep linking flow from Dropbox to your Cordova app—and it’s a far more reliable replacement for cordova-plugin-customurlscheme now that Firebase is part of your stack. Firebase often relies on universal links/App Links under the hood, which can conflict with custom URL schemes (especially on newer OS versions like Android 12+). Here’s a step-by-step breakdown to make this work:
Step 1: Install and configure the plugin
First, add the plugin to your project:
cordova plugin add cordova-universal-links-plugin --save
Next, define your universal link setup in config.xml. You’ll need to map a controlled HTTPS domain (one you own) to a custom event that your app can listen for. For example:
<universal-links> <host name="your-custom-domain.com" scheme="https"> <path event="dropboxFileSelected" url="/dropbox-callback/*" /> </host> </universal-links>
The event attribute lets you target this specific Dropbox callback flow later in your code.
Step 2: Set up Dropbox to redirect to your universal link
In your Dropbox Developer Console app settings, set the redirect URI to your universal link (e.g., https://your-custom-domain.com/dropbox-callback). When a user selects a file, Dropbox will redirect to this URI—triggering your app to open if it’s installed.
Make sure to pass file metadata (like file ID, path, or download URL) as query parameters in the redirect. Dropbox’s API lets you include these parameters when initiating the file selection flow, so they’ll be carried over to your app.
Step 3: Handle the deep link in your Cordova app
In your main JavaScript file (e.g., www/js/index.js), listen for the dropboxFileSelected event you defined to extract and use the file info:
document.addEventListener('deviceready', function() { // Subscribe to the custom deep link event universalLinks.subscribe('dropboxFileSelected', function(eventData) { // Parse the URL to get file parameters const linkUrl = new URL(eventData.url); const fileId = linkUrl.searchParams.get('file_id'); const filePath = linkUrl.searchParams.get('file_path'); // Use the file info (e.g., fetch the file via Dropbox API) console.log('Received Dropbox file:', fileId, filePath); // Add your app logic here to process the file }); }, false);
Step 4: Verify domain ownership (critical for iOS)
For iOS to recognize your app as the handler for these links, upload an apple-app-site-association file to the root (or .well-known directory) of your domain. This file proves you own the domain and maps it to your app. Example content:
{ "applinks": { "apps": [], "details": [ { "appID": "YOUR_APPLE_TEAM_ID.YOUR_APP_BUNDLE_ID", "paths": [ "/dropbox-callback/*" ] } ] } }
Replace YOUR_APPLE_TEAM_ID and YOUR_APP_BUNDLE_ID with your actual Apple Developer team ID and app bundle ID.
Step 5: Android App Links configuration
The plugin will auto-generate most of the required intent filters in AndroidManifest.xml, but double-check to ensure they target your HTTPS domain and path. You’ll also need to verify your domain via Google Search Console to let Android know your app is the official handler for these links.
Why this works better than custom URL schemes with Firebase
Universal links use standard HTTPS, which aligns with modern OS security requirements and plays nicely with Firebase’s own use of App Links/Dynamic Links. Custom URL schemes often run into conflicts with Firebase’s deep linking infrastructure, especially on Android 12+ where non-HTTPS schemes are restricted.
Quick troubleshooting tips
- Always test on real devices—emulators often have issues with universal link detection.
- Use Apple’s App Search API Validation Tool to check your
apple-app-site-associationfile. - For Android, test the link directly with:
adb shell am start -W -a android.intent.action.VIEW -d "https://your-custom-domain.com/dropbox-callback?file_id=test123"
内容的提问来源于stack exchange,提问作者user2570135

