[英]How to open an Android App link from a webview
我有一個可以處理應用程序鏈接的應用程序。
例如,如果您有一個鏈接(例如https://my-app-domain.com/something)並單擊它,它將啟動我的應用程序。
但是,如果鏈接是在一個在 web 視圖中打開鏈接的應用程序中發送的,我的應用程序將不會啟動。 例如,Facebook Messenger、Instagram 和 Snapchat 都在他們自己的網頁視圖中打開鏈接,將用戶帶到我的網站而不是啟動我的應用程序。
我想要做的是讓這個鏈接啟動我的應用程序,即使它是在一個在 web 視圖中打開鏈接的應用程序中發送的。
謝謝,
就像你說的,Facebook Messenger 不處理應用程序鏈接/通用鏈接。
我一直在嘗試,似乎自定義 uri 方案樣式鏈接(my-app://something)工作。 您可以做的是在https://my-app-domain.com/something上實現 Web 回退,它會嘗試將瀏覽器重定向到您的自定義 uri,如果這不起作用,則顯示一個不錯的網頁。 這就是 Spotify 等大公司的做法。
在 Android 上,您可以通過指定多個意圖過濾器來支持應用程序鏈接和自定義 uri 方案;
<intent-filter android:autoVerify="true">
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="https"
android:host="my-app-domain"
android:pathPrefix="/something" />
</intent-filter>
<!-- Google claims that one intent-filter can handle multiple <data> elements, this seems to be untrue however -->
<intent-filter android:autoVerify="true">
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="my-app" />
</intent-filter>
然后在https://my-app-domain.com/something上的 Web 回退中,您會遇到這種令人作嘔的黑客攻擊。
<script>
window.location = 'my-app://something'
// optional secondary fallback
setTimeout(() => {window.location = 'https://secondary-fallback'}, 1000)
</script>
結果是,如果您安裝了應用程序,您最終會按預期進入您的應用程序,但如果沒有,您最終會進入您的輔助后備頁面。
同樣的原理也適用於 iOS。 我正在使用 react native,並且在 AppDelegate.m 中添加了以下內容:
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url
sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
return [RCTLinkingManager application:application openURL:url
sourceApplication:sourceApplication annotation:annotation];
}
然后在 Info.plist 中指定一個 uri 方案:
<key>CFBundleURLTypes</key>
<array>
<dict>
<key>CFBundleTypeRole</key>
<string>Editor</string>
<key>CFBundleURLSchemes</key>
<array>
<string>my-app</string>
</array>
</dict>
</array>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.