2016-09-02 11 views
0

developers.google.comによれば、Web Appインストールバナーを延期することはできますが、完全に取り消すことはできません。遅延Webインストールバナー

window.addEventListener('beforeinstallprompt', function(e) { 
    console.log('beforeinstallprompt Event fired'); 
    e.preventDefault(); 
    return false; 
}); 

つのユースケースは、(彼らはあなたに従事していることを示すために何かを、ユーザーがアクションを実行した直後に、たとえば、後でページのライフサイクルにおけるまで、プロンプトを延期、またはページの一番下を打つことですサイト)。

web-appバナーをどのように延期することができますか?

答えて

2

これがあなたに役立つ場合はdocumentationをチェックしてみてください。

以下は、プロンプトを延期する際に使用する完全なコードです。

var deferredPrompt; 

window.addEventListener('beforeinstallprompt', function(e) { 
    console.log('beforeinstallprompt Event fired'); 
    e.preventDefault(); 

    // Stash the event so it can be triggered later. 
    deferredPrompt = e; 

    return false; 
}); 

btnSave.addEventListener('click', function() { 
    if(deferredPrompt !== undefined) { 
    // The user has had a postive interaction with our app and Chrome 
    // has tried to prompt previously, so let's show the prompt. 
    deferredPrompt.prompt(); 

    // Follow what the user has done with the prompt. 
    deferredPrompt.userChoice.then(function(choiceResult) { 

     console.log(choiceResult.outcome); 

     if(choiceResult.outcome == 'dismissed') { 
     console.log('User cancelled home screen install'); 
     } 
     else { 
     console.log('User added to home screen'); 
     } 

     // We no longer need the prompt. Clear it up. 
     deferredPrompt = null; 
    }); 
    } 
}); 

詳しくは、linkにもチェックしてください。

+1

'deferredPrompt == undefined'のときに何をするのが面白いです... – 4esn0k

+0

ユーザがカスタムボタンをクリックしたときにその値が' null'になると、 'if(deferredPrompt)'になるはずです。 –