iOS Web App: Showing content only if the application is standalone

前端 未结 3 613
Happy的楠姐
Happy的楠姐 2020-12-15 13:55

If a user visits my websites example, from Safari Mobile how could I place there a blank page that says \"Add To Homescreen.\"? Once added it would show different content.

3条回答
  •  感动是毒
    2020-12-15 14:41

    You'll want to check two things. First, is it running on an iOS device? Second, is window.navigator.standalone == true?

    window.navigator.standalone is primarily used by Webkit browsers to indicate the app is in fullscreen (or standalone) mode. Plenty of devices (like phones running Android), support this property, but don't have the option to 'Add to Homescreen' like iOS devices do, so you need to check both.

    Demo:

    Javascript:

    function isIOS() {
        var userAgent = window.navigator.userAgent.toLowerCase();
        return /iphone|ipad|ipod/.test( userAgent );
    };
    
    function isStandalone() {
        return ( isIOS() && window.navigator.standalone );
    };
    
    window.onload = function () {
        if( isStandalone() || !isIOS() ) { //either ios+standalone or not ios
            //start app
        } else {
            //display add to homescreen page
        };
    };
    

提交回复
热议问题