Web Development for the iPhone July 8, 2007
Web Development for the iPhone: Targeting the iPhone Safari browser
Developing for the iPhone
- The Safari iPhone user agent string is:
Mozilla/5.0 (iPhone; U; CPU like Mac OS X; en) AppleWebKit/420+
(KHTML, like Gecko) Version/3.0 Mobile/1A543a Safari/419.3 - Platform string:
(iPhone; U; CPU like Mac OS X; en) - Version string:
Version/3.0 Mobile/1A543a Safari/419.3
To target the iPhone with CSS, use:
<!--[if !IE]>-->
<link media="only screen and (max-device-width: 480px)"
href="iPhone.css" type="text/css" rel="stylesheet" />
<!--<![endif]-->
The logic of this is that only browsers that understand screen understand only, and of these, only the iphone has a max-device-width of 480px. The reason for the anti-IE comments is that some versions of IE render CSS regadless of media type declarations.
To target the iPhone server-side with PHP you can use:
if (stristr($_SERVER['HTTP_USER_AGENT'], 'iPhone')) {}
iPhone Viewport Orientation
The iPhone supports both landscape and portrait views. You can specify CSS based on viewport orientation which you determine via javascript and update the orient attribute of the body element. Target the browser with body[orient="landscape"] or body[orient="portrait"]
iPhone ViewPort Orientation JavaScript
The following javascript snippet detects and sets the iPhone’s viewport orientation by evaluating the innerWidth property of the window object and setting the orient attribute of the body element at regular intervals:
var updateLayout = function() {
if (window.innerWidth != currentWidth) {
currentWidth = window.innerWidth;
var orient = (currentWidth == 320) ? "profile" : "landscape";
document.body.setAttribute("orient", orient);
window.scrollTo(0, 1);
}
};
iPhone.DomLoad(updateLayout);
setInterval(updateLayout, 500);
Hiding the iPhone toolbar
With one line of JavaScript you can hide the big toolbar: window.scrollTo(0, 1); . Include this line of code to your snippet of detecting the phone’s orientation, as the toolbar will reappear when the orientation is changed. Remove it from the snippet above if you want the toolbar to show.
iPhone Viewport Meta Tag
The viewport meta tag properties include width, height, initial-scale, user-scalable, minimum-scale and maximum-scale. The height is calculated based on the width and aspect ratio. The initial-scale is the scale to render when the page first loads; with the default to fit the screen. Unless user scalable is set to no, th user can change the scale through pinching and double tapping of the iPhone.
| Property | Default Value | Minimum Value | Maximum Value |
|---|---|---|---|
| width | 980 | 200 | 10000 |
| height | based on aspect ratio | 223 | 10000 |
| inital-scale | fit to screen | minimum-scale | maximum-scale |
| user-scalable | yes | no | yes |
| minimum-scale | 0.25 | > 0 | 10 |
| maximum-scale | 1.6 | >0 | 10 |
Examples:
<meta name="viewport" content="initial-scale=2.3, user-scalable=no" /> <meta name="viewport" content="width=320; initial-scale=1.0; maximum-scale=1.0; user-scalable=0;" />
The iPhone automatically adjusts font size for readability. This feature can be overridden with -webkit-text-size-adjust. The values for -webkit-text-size-adjust are none (default) | auto | %value.
Comparisons of Safari on the desktop versus the iPhone.
Safari on iPhone supports:
- Safari supports cookies on both
- Safari on iPhone allows up to 8 user initiated browser pages to be open at once.
- Default user preference is set to block pop-up windows.
- Safari on iPhone supports many MIME types and rich media, including PDF and media file types
- Images: Safari supports
.gif, .jpg, .png,and.tiff - Fonts: The iPhone comes with American Typewriter, Arial, Arial Rounded MT Bold, Courier, Courier New,Georgia, Helvetica, Helvetica New, Marker Felt, Times New Roman, Trebuchet MS, Verdana, and Zapfino; so that’s what Safari on the iPhone supports.
- Other than the :hover pseudoclass which isn’t supported on the iPhone since mouseover effects aren’t supported, Safari on the iPhone supports CSS1, CSS2 and several selectors and attributes of CSS3
Safari on iPhone does not support:
- Events:
mouseoverandmouseout, including:hoverstyles and tool tips, (but it does supportonclickandevent listeners). Safari on the iPhone does not support the document events of onkeydown, onkeypress and onkeyup, the form-field events of ondblclick, onmouseenter, onmouseleave, onmousemove, and onSelect, and the window events of onresize and onScroll. This is not an exhaustive list, so test your event handlers before listening to me. window.showModalDialog()- Plug-ins: Flash or Java, and plug-in installations. Do not ask users to download Flash.
- File-size: Non-streaming files of over 10MB. CSS, JavaScript and HTML files are limited to 10MB per file. JavaScript is limited to 5 seconds of execution time. Safari for the iPhone does support gzip compression, so compress!
- The iPhone does not support gifs, png or tiffs over 8 MB and jpgs over 128 MB (but does support larger streaming media files).
- You can use iFrames, but avoid framesets.
Other iPhone Safari features
Phone numbers are automatically converted to phone links. You should convert them instead of letting Safari control it for you.
<a href="tel:4155551212">415.555.1212</a>.
Notes:
- Check out the application we (Ryan, Richard, Seto and I) developed at iPhoneDevCamp - PickleView - http://mxis.com/pickleview.
- I don’t own an iPhone. Feel free to send me one! :)
“You can specify CSS based on vieport orientation with the dynamically changed orient attribute which Safari for the iPhone adds dynamically to the body element. Target the browser with body[orient=”landscape”] or body[orient=”portrait”]”
Safari for the iPhone does not add this attribute. It is obtained using a JS hack. Example: http://www.iphonewebdev.com/examples/liquified/liquified.html
-Kalle.
Thanks Kalle. I updated the blog with a snippet from the Pickleview Code.
Check out Joe Hewitt’s iUI at http://www.joehewitt.com/iui/
[…] to be two specially designed formats for each of the orientations of the phone. Thanks to some special javascript action, there is a whole handful of conditional CSS that displays differently when the iPhone is in […]
[…] Web Development for the iPhone » CSS, JavaScript and XHTML Explained Good coverage of iPhone web development basics, including detecting orientation, and clean iPhone specific JavaScript and CSS definitions (tags: iphone webdev css javascript html safari) […]
Don’t forget to check out my blog post on CSS support on the iPhone. I tested all teh CSS1, CSS2 and CSS3 selectors on the iPhone and posted the results at http://www.evotech.net/blog/2007/07/safari-30-css-support/ .
If you want to compare the iPhone to web browsers, I ran the same tests on all Grade-A browsers and posted those at http://www.evotech.net/blog/2007/04/css-selectors-and-browser-support/
Thank you for the article. I will be referring to it alot.
I have a question… Has anyone seen any information on the pinch-in and pinch-out gestures? Does Safari trigger an event and if so any examples?
Thank you in advance.
-Al
The application i developed with Seto, Ryan and Richard is hilited on the Apple iPhone Webapp home page. Woohoo! Check out http://www.apple.com/iphone/webapps/ third from left!
Hi, I’m wondering what the line:
“iPhone.DomLoad(updateLayout);”
does in the ViewPort Orientation script. I can’t find any documentation on this anywhere… could you clarify/let me know where you got this from?
Thank
Ali
iPhone Web Developer Tool
http://www.manifestinteractive.com/iphone/
This application allows you to use your iPhone for Web Developing. You can View Source, Find on Page, Outline Divs & Tables, etc. Similar to the Firefox Web Developer Extension. Simply drag bookmarks into the web browser that you have your iPhone or iPod Touch synced to and you’re good to go. Works on any site.
Let me know what you think. If you have stuff you want added just let me know.
- Peter Schmalfeldt
[…] Navegador Web Safari (iPhone) soporta cookies y Flash (con ciertas restricciones), pero no soporta la mayoría de eventos como mouseover o mouseout. Además, los ficheros JavaScript tienen limitada su ejecución a 5 segundos. […]
When using window.scroll to hide the Safari tool bar, does the javascript need to be part of an onload event? Or can it just be inline javascript? -Tony
[…] window.scrollTo(0, 1); to hide the Safari toolbar. For more, go to Web Development for the Iphone […]
[…] technische Details und Beschreibungen findet ihr im Beitrag Web Development for the iPhone aus dem evotech […]