Deep Link 流程
- 頁面載入,解析 URL 參數 (如
?url=target_page)。
- 檢測裝置平台 (iOS / Android / 桌面)。
- 嘗試喚起 App (Custom URI Scheme / Universal Links / App Links)。
- 延遲後若未喚起成功,導引至 App Store / Google Play / 網頁備用頁。
完整通用實作代碼
(function() {
// 1. 取得目標 URL 參數
var openUrl = window.location.search;
try {
openUrl = openUrl.substring(1); // 移除開頭 '?'
} catch (e) { openUrl = ''; }
// 2. 平台檢測
var ua = navigator.userAgent;
var isIOS = /iPad|iPhone|iPod/.test(ua);
var isAndroid = /Android/.test(ua);
var isDesktop = !isIOS && !isAndroid;
// 3. 配置參數 (需根據實際 App 修改)
var config = {
ios: {
scheme: 'myapp', // URI Scheme: myapp://
host: 'open',
appStoreUrl: 'https://itunes.apple.com/app/id123456789?mt=8'
},
android: {
scheme: 'myapp',
host: 'open',
packageName: 'com.example.myapp',
playStoreUrl: 'https://play.google.com/store/apps/details?id=com.example.myapp'
},
desktop: {
fallbackUrl: 'https://myapp.com/download'
}
};
// 4. 執行喚起邏輯
if (isIOS) {
// iOS: Universal Links 優先,Scheme 兜底
// 先嘗試 Universal Link (需 Associated Domains 配置)
// window.location = 'https://myapp.com/open?url=' + encodeURIComponent(openUrl);
// Scheme 兜底 (延遲跳轉 App Store)
setTimeout(function() {
window.location = config.ios.appStoreUrl;
}, 2000); // 2秒後跳轉
window.location = config.ios.scheme + '://' + config.ios.host + '?url=' + encodeURIComponent(openUrl);
} else if (isAndroid) {
// Android: Intent URI 格式 (Chrome 25+ 支援)
var intentUrl = 'intent://' + config.android.host + '/'
+ '?url=' + encodeURIComponent(openUrl)
+ '#Intent;scheme=' + config.android.scheme
+ ';package=' + config.android.packageName
+ ';end';
window.location = intentUrl;
// 兜底:延遲跳轉 Play Store
setTimeout(function() {
window.location = config.android.playStoreUrl;
}, 2000);
} else {
// 桌面/其他:直接開啟網頁或下載頁
window.location.href = config.desktop.fallbackUrl + '?url=' + encodeURIComponent(openUrl);
}
})();
關鍵技術細節
| 技術 | iOS | Android |
| URI Scheme | myapp:// (舊版通用) | myapp:// (舊版通用) |
| Universal Links | https://myapp.com/... (iOS 9+,需配置 apple-app-site-association) | — |
| App Links | — | https://myapp.com/... (Android 6+,需 assetlinks.json) |
| Intent URI | — | intent://...#Intent;scheme=...;package=...;end (Chrome 25+) |
| 喚起失敗兜底 | setTimeout 跳轉 App Store | setTimeout 跳轉 Play Store / 網頁 |
注意事項與最佳實踐
- Universal Links / App Links 為首選:無彈窗確認、支援 HTTPS、安全性高、可直接開網頁作為兜底。
- Scheme 喚起會彈窗確認:用戶體驗較差,且 iOS 9+ 若安裝 App 會優先走 Universal Links。
- 延遲時間調整:2000ms 為經驗值,視網路/裝置效能調整 (1500-3000ms)。
- 參數傳遞編碼:
encodeURIComponent 確保特殊字元正確傳遞。
- 測試建議:真機測試各版本 OS、各瀏覽器 (微信/支付寶內置瀏覽器常屏蔽 Scheme,需引導用戶外部瀏覽器打開)。
進階:使用成熟庫