2013-10-31 32 views
35

我一直在尋找一種方法來從瀏覽器中打開本機iOS應用程序。 我在這裏找到了一個不錯的解決方案:Is it possible to register a http+domain-based URL Scheme for iPhone apps, like YouTube and Maps?如何在嘗試打開未安裝的原生應用程序時阻止iOS Safari瀏覽器警報?

這個解決方案在安裝應用程序時效果很好。但是當用戶沒有安裝此應用程序時 - safari會觸發一條錯誤消息,指出「由於地址無效,Safari無法打開頁面」。

有沒有辦法阻止這種行爲,而是提示用戶下載應用程序?

+0

也許這個答案可以幫助你http://stackoverflow.com/a/16720093/2291363 – zbMax

+0

這正是我所做的,這一點:window.location的= 「APPNAME://」 ;當應用程序沒有安裝在設備上時會觸發一個醜陋的錯誤 –

+0

它會觸發一個錯誤,但在超時重定向時它會消失 – SomeGuy

回答

-1

做到這一點,正確的方法是使用智能應用橫幅: https://developer.apple.com/library/ios/documentation/AppleApplications/Reference/SafariWebContent/PromotingAppswithAppBanners/PromotingAppswithAppBanners.html

如果未安裝應用程式,橫幅廣告就會讓用戶安裝它。如果安裝了它,它將打開應用程序,並且您可以指定自定義URL參數傳遞給應用程序。

+6

但是你不能讓它自動啓動應用程序的權利? –

+2

添加智能應用程序橫幅有其缺點。例如,如果用戶關閉它(按下X按鈕),則無法再顯示它。 – caisah

5

使用iframe。

$('<iframe />').attr('src', "appname://").attr('style', 'display:none;').appendTo('body'); 
+0

也許你知道:否則,當應用程序安裝,我回到了ipad的safari(例如) - 我看到1-2秒的警報:「在應用程序商店打開這個應用程序? - 隱藏它是真的嗎? – brabertaser19

26

這裏是爲我工作的解決方案:

var timeout; 

function preventPopup() { 
    clearTimeout(timeout); 
    timeout = null; 
    window.removeEventListener('pagehide', preventPopup); 
} 

function openApp() {  
    $('<iframe />') 
    .attr('src', appurl) 
    .attr('style', 'display:none;') 
    .appendTo('body'); 

    timeout = setTimeout(function() { 
      document.location = appstore; 
    }, 500); 
    window.addEventListener('pagehide', preventPopup); 
} 
+1

這個解決方案對我來說也很好。 – jpcamara

+2

它不適用於Android的Crome。 – confile

+1

嘿,你可以替換 document.location = appstore;與: window.open(appstore,「_system」); 它會同時適用於 – ElizaS

2

爲了解決這一點,並避免任何希望iOS的Safari瀏覽器警告我用不同的方法處理也iframe中,但沒有jQuery和監聽器事件。它完美的作品。

var iframe = document.createElement("iframe"); 

    iframe.style.border = "none"; 
    iframe.style.width = "1px"; 
    iframe.style.height = "1px"; 
    iframe.onload = function() { 
     document.location = alt; 
    }; 
    iframe.src = nativeSchemaUrl; //iOS app schema url 

    window.onload = function(){ 
     document.body.appendChild(iframe); 
    } 

    setTimeout(function(){ 
     window.location = fallbackUrl; //fallback url 
    },300); 
+1

「document.location = alt;'中的」alt「變量是什麼? –

+1

什麼是「alt」變量? – ayjay

+0

備選/備用URL,也許 – CatalinBerta

0

我使用元刷新作爲後備,因爲這工作沒有JavaScript。此代碼適用於iOS和Android。

<html> 
    <head> 
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> 
    <meta http-equiv="refresh" content="1; url=http://www.facebook.com"> 
    <title>Redirecting..</title> 
    <script> 
    function tryOpenApp() { 
       var iframe = document.createElement("iframe"); 
       iframe.style.border = "none"; 
       iframe.style.width = "1px"; 
       iframe.style.height = "1px"; 
       iframe.src = "fb://feed/"; 
       document.body.appendChild(iframe); 
    } 
    </script> 
    </head> 
    <body onload="tryOpenApp()"> 
    </body> 
</html> 

測試http://static-pmoretti.herokuapp.com/deep-link/

相關問題