iphone 如何實現html5頁面,自動提示添加到主屏幕

雖然沒有能力開發Native App,但仍是能夠利用iOS中Safari瀏覽器的特性小小的折騰一下,作一個僞Web App知足下小小的虛榮心的。web


既然是在iOS中的Safari折騰的,那麼代碼中利用到的也基本上都是Safari的私有屬性。瀏覽器


添加圖標到主屏幕是Web App的第一步:app


<link rel="apple-touch-icon-precomposed" sizes="57x57" href="icon-57.png">測試

<link rel="apple-touch-icon-precomposed" sizes="72x72" href="icon-72.png">動畫

<link rel="apple-touch-icon-precomposed" sizes="114x114" href="icon-114.png">spa

<link rel="apple-touch-icon-precomposed" sizes="144x144" href="icon-144.png">scala


添加圖標到屏幕裏的有兩種屬性值apple-touch-icon和apple-touch-icon-precomposed,區別就在因而否會應用iOS中自動給圖標添加的那層高光。orm


因爲iPhone以及iPad都有兩種分辨率的設備存在,圖標的尺寸就須要作4個:144×144(iPad Retina)、72×72(iPad)、114×114(iPhone Retina)、57×57(iPhone)。圖片


能夠使用sizes尺寸來告訴設備該調用哪一個圖標。開發


有了圖標還不夠像,還須要加上啓動畫面:


<link rel="apple-touch-startup-image" sizes="2048x1496" href="icon-2048x1496.png" media="screen and (min-device-width:481px) and (max-device-width:1024px) and (orientation:landscape) and (-webkit-min-device-pixel-ratio: 2)">

<link rel="apple-touch-startup-image" sizes="1536x2008" href="icon-1536x2008.png" media="screen and (min-device-width:481px) and (max-device-width:1024px) and (orientation:portrait) and (-webkit-min-device-pixel-ratio: 2)">

<link rel="apple-touch-startup-image" sizes="640x920" href="icon-640x920.png" media="screen and (max-device-width: 480px) and (-webkit-min-device-pixel-ratio: 2)">

<link rel="apple-touch-startup-image" sizes="320x460" href="icon-320x460.png" media="screen and (max-device-width: 320)">


apple-touch-startup-image是用來標示啓動畫面的,但它不像apple-touch-icon能夠指定sizes來告訴設備該使用哪一個圖片(也有一種說法是在iOS5中已經支持sizes識別了,但沒有測試成功),因此只能經過media裏的設備分辨率的判斷值來識別,而iPhone Retina的分辨率值界於取值之間,因此須要經過webkit的私有屬性-webkit-min-device-pixel-ratio:2來鑑別像素密度以進行識別。


啓動畫面的圖片尺寸並不是徹底等於設備的尺寸,好比iPad2的尺寸是1024×768,但它的啓動畫面尺寸橫向是1024×748,豎向尺寸是768×1004,由於須要減去系統狀欄的高度20px,而使用的Retina屏幕的iPhone4以及iPad3則須要減去狀態欄的高度40px。


Web App運行起來要像Native App,那麼就要去掉Safari的一些默認控件,好比地址欄、狀態欄之類的。


<meta name="apple-mobile-web-app-capable" content="yes">

<meta name="apple-mobile-web-app-status-bar-style" content="black">

<meta name="format-detection" content="telephone=no">

<meta name="viewport" content="width=device-width,initial-scale=1, minimum-scale=1.0, maximum-scale=1, user-scalable=no">


apple-mobile-web-app-capable是用來定義應用全屏展現的;在定義了apple-mobile-web-app-capable的前提下,設置狀態欄的屬性值apple-mobile-web-app-status-bar-style纔有效;format-detection的值用於啓用或禁用自動檢測在網頁中可能出現的電話號碼;


viewport並不是Safari的私有屬性,width用於指定寬度,initial-scale指定初始化的縮略比例,minimum-scale指定縮小的比例,而maximum-scale則是放大的比例,固然這些縮放都取決於user-scalable——決定用戶是否能縮放頁面。

相關文章
相關標籤/搜索