官术网_书友最值得收藏!

Polyfiling the homescreen experience on iOS and other legacy browsers

A common question developers and business owners ask is how to enable progressive web application features on iOS and older browsers like Internet Explorer. While all features cannot be hacked in these browsers, much of it can.

When the iPhone was released, the initial application model was the web. They created an advanced experience for web apps that included the add to homescreen experience. Unfortunately, they did not make an automatic prompt experience. Who knows how advanced this might be today if developers did not cry out for the native app model.

What we can do is still leverage this capability and use Matteo Spinelli's add to homescreen library (http://cubiq.org/add-to-home-screen) in combination with Apple's guidelines. Doing so allows your web apps to launch from user's home screens, with or without Chrome. This is shown in the following screenshot:

It is important that you avoid duplicating homescreen prompts by not loading the Add to Home Screen library unless needed. The simplest way I have found to determine if the polyfil is needed is by using feature detecting service worker support. I chose this since browsers supporting service workers have some sort of add to homescreen experience. This may or may not remain true in the future, so be ready to change criteria if things change.

Without going into details, I like to dynamically load JavaScript references when a page is loaded. This process involves a series of feature detections to polyfil various requirements like Promises and the Fetch API:

        if (!'serviceWorker' in navigator) { 
            //add to homescreen polyfil 
            scripts.unshift("js/libs/addtohomescreen.min.js"); 
        } 
You can read more about dynamically loading scripts in Jake Archibald's article  ( https://www.html5rocks.com/en/tutorials/speed/script-loading ).

You will also need to dynamically add the add to homescreen stylesheet. This time, add a feature detection script to your document's HEAD:

    <script> 
        if ('serviceWorker' in navigator) { 
 
            // add addToHomeScreen CSS 
            cssLink = document.createElement("link"); 
 
            cssLink.id = "addToHomeScreen"; 
            cssLink.rel = "stylesheet"; 
            cssLink.type = "text/css"; 
            cssLink.href = "css/libs/addtohomescreen.css"; 
            document.head.appendChild(cssLink); 
 
        } 
    </script> 

Users have been able to install web apps like this since the iPhone was released, but the process is manual and largely unknown by end users and developers alike. The lack of an automated prompt has been a key missing component of this feature. The experience it creates seems to be a model that the Chrome team and other platforms modeled to surface the progressive web application homescreen prompt.

Matteo's library only prompts the user and starts them down the manual process. but there are still a few extra steps that aren't intuitive that users must complete. The new native add to homescreen process has a pseudo automatic pipeline you can integrate. I think the add to homescreen library may serve as a good reference for designing your experience, so it is worth your time to look at it.

主站蜘蛛池模板: 盱眙县| 东山县| 绥化市| 黔江区| 嘉善县| 托克逊县| 衡阳县| 泰来县| 江川县| 商河县| 辽阳县| 巴林右旗| 留坝县| 内黄县| 隆化县| 临泽县| 舒兰市| 绿春县| 郧西县| 汝阳县| 涟水县| 永和县| 博野县| 彩票| 崇州市| 伊川县| 图们市| 文水县| 岑溪市| 诸暨市| 循化| 新乐市| 永吉县| 格尔木市| 桂平市| 会同县| 隆化县| 禄丰县| 遂溪县| 会昌县| 山阴县|