mobile web development: the challenges & how to meet them

Post on 27-Jan-2015

102 Views

Category:

Technology

0 Downloads

Preview:

Click to see full reader

DESCRIPTION

The slides from my presentation for MinneWebCon 2012 at the University of Minnesota St. Paul Campus.

TRANSCRIPT

Mobile Web Development

The Challenges and How To Meet ThemTony Thomas - Student Unions & Activities at The University of Minnesota

Tuesday, April 17, 12

Student Unions & Activities 2011 Poll

50% have a smartphone

7% plan to get one

Tuesday, April 17, 12

Our unscientific student poll.

Tuesday, April 17, 12

Recognize?

Yavin

Tuesday, April 17, 12

This is Yavin, a red giant planet.

Yavin 4 Yavin

Tuesday, April 17, 12

This is Yavin 4, the location of the rebel base

Student Unions & Activities Website

2011: 4.34%

Tuesday, April 17, 12

A snapshot from SUA a year ago.

Student Unions & Activities Website

2012: 9.03%

Tuesday, April 17, 12

This year.

Homecoming Website

2010: 3.8%

Tuesday, April 17, 12

Homecoming 2010 during the event.

Homecoming Website

2011: 11.64%

Tuesday, April 17, 12

Homecoming 2011 during the event.

Spring Jam Website

2010: 5.46%

Tuesday, April 17, 12

Spring Jam 2011 during the event.

Spring Jam Website

2011: 13.44%

Tuesday, April 17, 12

Spring Jam 2011 during the event.

Mobile Operating Systems

0

21500

iOSAndroid

Blackberr

y

Windows Phone

SymbianOS

Samsung

Nokia LG

Danger Hiptop

21458

10330

76782 56 37 31 6 4

Tuesday, April 17, 12

Our users are predominantly iOS & Android. Blackberry distant 3rd. Long tail of others.

Android

Android

Device Dimensions

Android

Android

Android Android Samsung Nokia

AndroidAndroidBlackberry

Android

Nokia

Samsung

AndroidAndroid iPhone iPod Android

Android

Blackberry

Blackberry SymbianOS

Android

Blackberry

Android

Android Blackberry

BlackberryAndroid Windows

AndroidAndroid

AndroidAndroid

Android

SymbianOSBlackberry

Android

Android

Tuesday, April 17, 12

This is just a small sample of device dimensions taken from 1 month of SUA mobile traffic.

Responsive Design

Photo: Peter J. Hart

flickr.com/photos/40054618@N03/5140728113/

Ethan Marcotte

Tuesday, April 17, 12

This is Ethan Marcotte who introduced the concept of responsive design in 2010.

A Dao of Web Design

Photo: drewm flickr.com/photos/drewm/

flickr.com/photos/drewm/4732738890/

John Allsopp

Tuesday, April 17, 12

It’s in part inspired by John Allsopp’s 2000 A List Apart article called “A Dao of Web Design.”

A Dao of Web Design

alistapart.com/articles/dao/

“It’s time to throw out the rituals of the printed page, and to engage the medium of the web and its own nature.”

Tuesday, April 17, 12

[Read quote.] Back in 2000, Allsopp was advocating for embracing the inherent flexible nature of the web.

Responsive Architecture

Photo: Evangelos Kotsioris

“If a building could change its posture, tighten its muscles and brace itself against the wind, its structural mass could literally be cut in half.” --Guy Nordenson

Tuesday, April 17, 12

Responsive design is also inspired by Responsive, or adaptive architecture where buildings respond and change according to their environment. [read quote] Think of markup, CSS, JavaScript & media as the structural mass of a website.

Flexible, Fluid Layouts

widths, margins, padding in relative units

Photo: Sabrina Campagna

flickr.com/photos/mar1lyn84/5649648853/

Tuesday, April 17, 12

The foundation of responsive design are layouts in relative units. That means widths, margins & padding are defined in ems or percents.

Flexible, Fluid Layouts

Target / Context = Result

Photo: ansik

flickr.com/photos/ansik/304526237/

Tuesday, April 17, 12

This is Ethan Marcotte’s formula for calculating widths in ems. The assumption here is that most browsers maintain their default setting of a 16px font-size.

Flexible, Fluid Layouts

24 / 16 = 1.5

Photo: ansik

flickr.com/photos/ansik/304526237/

Tuesday, April 17, 12

In this example, we’re looking for a 24 pixel font-size. 24/16 is 1.5, so our font size is 1.5em.

Context Changes

13 / 24 = 0.54166666666666

Photo: ansik

flickr.com/photos/ansik/304526237/

Tuesday, April 17, 12

Beware of context changes. If our font is 1.5em and thus, 24px and we want a 13px font in that context, we have to divide 13 by 24 (our new context). Notice the number of decimal points in this example. Marcotte advocates for keeping all those decimal places. I haven’t been able to find any information as to how many places a different browsers will calculate.

Media Queries

Smartphone

Tablet Netbook Desktop

Tuesday, April 17, 12

Back to media queries. After we’ve established our grids, margins and padding in relative units, media queries are the next important step in making a design responsive.

Media Queries

@media screen and (min-width: 480px) {

/* CSS goes here */

}

Tuesday, April 17, 12

First a browser reads the media type, then evaluates the query. In this case, if the media is ‘screen’ and the browser viewport is at least 480px, the enclosed styles will be rendered.

Media Queries

@media screen and (min-width: 480px) {

/* CSS goes here */

}

Media

Tuesday, April 17, 12

First a browser reads the media type, then evaluates the query. In this case, if the media is ‘screen’ and the browser viewport is at least 480px, the enclosed styles will be rendered.

Media Queries

@media screen and (min-width: 480px) {

/* CSS goes here */

}

Media Query

Tuesday, April 17, 12

First a browser reads the media type, then evaluates the query. In this case, if the media is ‘screen’ and the browser viewport is at least 480px, the enclosed styles will be rendered.

Media Queries

@import url(“css/phone.css”) screen and min-width: 480px);

Tuesday, April 17, 12

Here’s an alternate syntax that will load a separate style sheet based on the parameters of the media query.

Media Queries

@import url(“css/phone.css”) screen and min-width: 480px);

Media

Tuesday, April 17, 12

Here’s an alternate syntax that will load a separate style sheet based on the parameters of the media query.

Media Queries

@import url(“css/phone.css”) screen and min-width: 480px);

MediaQuery

Tuesday, April 17, 12

Here’s an alternate syntax that will load a separate style sheet based on the parameters of the media query.

Media Query Options• width (browser width)

• height (browser height)

• device-width (device-width)

• device-height (device-height)

• orientation (portrait or landscape)

• aspect-ratio (browser aspect ratio)

• device-aspect-ratio (device aspect ratio)

• color (bits of color component of device)

• color-index (number of entries in the color lookup profile)

• monochrome (similar to color but for monochrome devices)

• resolution (pixel-density in DPI)

• scan (TV’s-whether scanning process is progressive or scan)

• grid (grid-based display, like feature phone)

Tuesday, April 17, 12

Here’s a complete list of media query options--including some related to television scanning.

Media Query Options

• width (browser width)

• height (browser height)

• device-width (device-width)

• device-height (device-height)

• orientation (portrait or landscape)

Tuesday, April 17, 12

In reality these are the most common. I want to point out a few distinctions here. ‘width’ & ‘height’ refer to the browser viewport. ‘device-width’ & ‘device-height’ refer to the height & width of the device. Device orientation is another handy option.

There are no standard devices!

Photo: Oliver White

flickr.com/photos/nuage_de_lait/6580839781/

Tuesday, April 17, 12

Until recently, it’s been popular to write queries based on the dimensions of popular devices. The list on the left of this slide is taken from SUA’s analytics. You can see the incredible diversity of screen sizes in our mobile analytics. There are no standard devices. Instead of trying to keep up w/ new device dimensions, you should evaluate the breakpoints of your design. Look at where the layout gets awkward and set a breakpoint. Then let the devices fall where they may. New devices appear almost every day. Your design is the constant in the equation. Plan accordingly.

Set media query

breakpoints where the

design breaks.

Photo: Ballookey Klugeypop

flickr.com/photos/ballookey/136450028/

Tuesday, April 17, 12

“CSS Pixels” & “Hardware Pixels”

Apple iPhone 4: 326 ppi

LG KM900 Arena: 311 ppi

Samsung S8000 Jet: 301 ppi

Canon EOS 550D (T2i): 289 ppi

HTC Touch Diamond: 286 ppi

Samsung S8500 Wave: 283 ppi

HTC Desire: 252 ppi

Google Nexus One: 252 ppi

Sony Ericsson XPERIA X10: 245 ppi

Samsung I9000 Galaxy S: 233 ppi

HTC HD2: 217 ppi

Nokia N8: 210 ppi

Apple iPhone 3GS: 165 ppi

Apple iPad: 132 ppi

Source: blog.gsmarena.com/how-does-the-iphone-4-pixel-density-stand-in-comparison/

Tuesday, April 17, 12

Here are points per inch for several mobile devices. Notice the iPhone 4 at the top with the most points per inch.

Some Pixels Are More Equal Than Others

Hardware Pixel CSS Pixel

Tuesday, April 17, 12

The iPhone 4 has a pixel-density of 2. That means there are 2 “hardware” pixels on the screen for every “CSS pixel.” That makes fonts and images render very crisply on the screen, but it’s a problem when we’re trying to calculate CSS. Thus we level the field by setting the zoom-level to 100%. When the zoom level is 100%, the ratio of hardware pixels to CSS is 1.

Some Pixels Are More Equal Than Others

Hardware Pixel CSS Pixel

Tuesday, April 17, 12

By putting “initial-scale=1” in the viewport meta tag forces the browser to make hardware pixels equal CSS pixels. The problem is, this will apply to text and layout, but images will zoom up and appear pixelated. You need to figure out the pixel density so the image quality matches the density of the screen. Which leads us to...

Take Control of The Screen

<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">

Tuesday, April 17, 12

It’s a good idea to use a “viewport” meta tag in the head of your document to gain some control and provide better consistency between mobile devices.

Take Control of The Screen

width=device-widthWithout this, mobile browsers will zoom the page out to 980px.

Tuesday, April 17, 12

First in that meta tag, I’m telling the browser to make the content width the device width. device-width

Take Control of The Screen

initial-scale = 1Controls the zoom level when the page is loaded. (1 = 100%)

Tuesday, April 17, 12

Take Control of the Screen

maximum-scale = 1Controls zooming. In my example, the maximum scale is set to 100%. Other options: minimum-scale & user-scalable.

Tuesday, April 17, 12

Take Control of the Screen

Tuesday, April 17, 12

Left: before setting the viewport meta tag. Right: after.

Responsive Images & Media

Photo: Vicky Somma

flickr.com/photos/tgaw/2556511663/

Tuesday, April 17, 12

After after fluid layouts and media queries come responsive images. The talking phones are outraged that we’re trying to send them a gigantic image.

Responsive Images & Media

#container img {

max-width: 100%;

}

Tuesday, April 17, 12

At their simplest, responsive images respond to the layout by never exceed the width of their container. Unfortunately HTML alone is lacking in terms of providing a way to place the smallest possible image on the page. Remember, mobile devices are often lacking in bandwidth. Sending a large image to a small screen is an unnecessary burden. JavaScript does provide some relief.

Start with Basic HTML

<a id=”home-link” href=”http://sua.umn.edu”>Home</a>

Tuesday, April 17, 12

Start with the lowest common denominator. In this example, I’m using a link, but this could be any container. This HTML will work with any feature phone.

Use JavaScript to Detect Screen Dimensions

h = screen.height;

Tuesday, April 17, 12

Next we have to try to evaluate the screen height. I use height because smart phones could be turned to landscape mode at any time, making the height the new width.

Use JavaScript to Detect Screen Dimensions

h = screen.height;

// Pixel ratio (WebKit Only)

r = window.devicePixelRatio;

Tuesday, April 17, 12

With the advent of high density screens, we have to account for pixel-density. The iPhone 4 has a pixel density of 2 and many Android phones have a pixel density of 1.5. Depending on meta tags in the head of your document, an iPhone might report dimensions of 320x480 even though it’s true resolution is 640x960. Caution: devicePixelRatio is WebKit-only. Fortunately this scenario if pure enhancement and won’t affect the functionality or information on the page. A non-WebKit browser will simply report a lower resolution and so the image may seem a little pixelated.

Use JavaScript to Detect Screen Dimensions

/* if devicePixelRatio is supported, calculate the device height */

if (typeof r !== 'undefined')

{

   /* Calculate the true number of pixels on the screen */

    height = h * r;

}

Tuesday, April 17, 12

We can test for this by making sure the result of devicePixelRatio is not ‘undefined’. If it is, we simply use the original height reading. If not, we can get the true height by multiplying the reported height by the pixel-density.

Use JavaScript to Detect Screen Dimensions

// Set the image directory accordingly

if (height <= 320)

{

    // load 320 header

    d = 320;

}

Tuesday, April 17, 12

In this example, I have directories with names matching a few key heights. A copy of the image is stored in each directory that matches it’s width. So a 320px wide image resides in the 320 directory.

Use JavaScript to Detect Screen Dimensions

$('a#home-link').html('<img src="/images/mobile/' + d + '/header.png" alt="This is alt text." id="logo" class="logos" />');

Tuesday, April 17, 12

I can then use JQuery write in this HTML with the directory I want. The image will then load that is large enough for the device, but somewhat optimized for the dimensions of the screen. This method is not perfect. For instance, the image will not load until after the DOM has fully loaded--since I’m using JQuery and the ready() method. Still it’s a fairly good compromise to minimize the bandwidth necessary to load the page.

Mobile First, Mobile Separate & RESS

Tuesday, April 17, 12

Now that I’ve touched on a few techniques, I want to go over three approaches to developing for mobile. Those are Mobile First, a separate mobile experience and RESS which stands for Responsive and Server Side.

Mobile FirstLuke Wroblewski

Photo: foltzwerk

flickr.com/photos/foltzwerk/2779814860/

Tuesday, April 17, 12

Mobile First is a concept advocated by Luke Wroblewski in his book of the same name. The basic approach is that sites should be first designed for mobile with desktop versions coming after. This approach begins with a number of constraints that filter through to the desktop experience.

Mobile First

• One site for all devices

• Focus on content that’s truly important

• No User Agent sniffing

Tuesday, April 17, 12

Some of the advantage of this approach are that you have one site for all devices. The constraints force you focus on what’s truly important. You avoid User Agent sniffing that some other techniques rely upon.

Mobile First

• Potential for unnecessary overhead

• Convincing stakeholders to embrace constraints may be a barrier

• Requires polyfills for older browsers

Tuesday, April 17, 12

Some disadvantages: Mobile networks are often slower, and sometimes users will have a cap on their data plan. You have to be careful to truly embrace the constraints through the entire stack so you can minimize the size of pages and media. Along with that, it’s not always easy to convince stakeholders to embrace those constraints. Finally, for the site to work in all browsers, you’re going to need to use a few JavaScript polyfills.

PolyfillsRespond: github.com/scottjehl/Respond

(media queries)

Overthrow: github.com/filamentgroup/Overthrow(overflow: auto)

html5shiv: code.google.com/p/html5shiv(HTML5)

Tuesday, April 17, 12

Respond for media queries in older versions of IEOverthrow for using overflow: autohtml5shiv if you’re using html5

Feature DetectionModernizr modernizr.com

Photo: Patrick Ciebilski

flickr.com/photos/mccieb/

Tuesday, April 17, 12

Modernizr is a JavaScript library for detecting browser features. It offers a lot of flexibility in that you can enhance your site based on the capabilities of each browser.

Modernizr• @font-face

• background-size

• border-image

• border-radius

• box-shadow

• Flexible Box Model

• hsla & rgba color

• Multiple backgrounds

• opacity

• text-shadow

• CSS Animations

• CSS Columns

• CSS generated content

• CSS gradients

• CSS reflections

• CSS 2D transforms

• CSS 3D transforms

• CSS transitions

• applicationCache

• Canvas

• Canvas text

• Drag and drop

• haschange Event

• History management

• HTML5 audio & video

• IndexedDB

• Input attributes

• Input types

• localStorage

• Cross-window messaging

• sessionStorage

• Web Sockets

• Web SQL DB

• Web Workers

• Geolocation

• Inline SVG

• SMIL

• SVG

• SVG clip pathes

• Touch events

• WebGL

Tuesday, April 17, 12

Modernizr has a long list of features that it detects.

Mobile First

Flexible, Fluid Layout

Media Queries

Responsive Images

Tuesday, April 17, 12

Encompasses all aspects of a responsive design to deliver one site that works for all devices.

Mobile First

Flexible, Fluid Layout

Media Queries

Responsive Images

Web Standards

Tuesday, April 17, 12

And responsive design is built upon web standards. If you’re already used to developing with standards, responsive design is just reaching a little further with the same concept.

Tuesday, April 17, 12

Here’s an example of a responsive design in action. Notice how he’s designed the layout and images to work across different screen widths.

Separate Mobile Experience

Tuesday, April 17, 12

Remember these guys? Both have also done sites with a totally separate mobile experience.

Separate Mobile Experience

• Site tailored to use case

• Eliminate unnecessary markup, CSS & JavaScript

• May be easier to convince stakeholders to embrace constraints

Tuesday, April 17, 12

Some of the advantages of providing a separate mobile experience: The site can be tailored the use case. You have a lot of control over reducing unnecessary markup, CSS & JavaScript. It may be easier to convince stakeholders to embrace constraints if they get to keep there content somewhere else.

Separate Mobile Experience

• Two sites

• Might not meet all users’ needs

• Relies on device detection

Tuesday, April 17, 12

Some of the disadvantages: You maintain two sites. A separate experience might not meet all your users’ needs. You’re going to have to rely on some sort of device detection.

Device Detection

Photo: Brad Buchanan

flickr.com/photos/frozenisles/3443607776/

detectmobilebrowsers.comdetector.dmolsen.com

Tuesday, April 17, 12

For the projects we’ve done at SUA so far, we’ve relied on the script at detectmobilebrowsers.com. I have more recently come accross Detector and it looks quite promising.

detectmobilebrowsers.com

Apache

ASP

ASP.NET

ColdFusion

C#

IIS

JSP

JavaScript

jQuery

Lasso

nginx

node.js

PHP

Perl

Python

Rails

Photo: Herr Olsen

flickr.com/photos/herrolsen/6199045384/

Tuesday, April 17, 12

Returns boolean. Available is several languages.

detectmobilebrowsers.com

<?php

is_mobile();

?>

Photo: Jeff Kubina

flickr.com/photos/kubina/170938326/

Tuesday, April 17, 12

Using the script is very simple.

Detector

• detector.dmolsen.com

• PHP only

• Beta

• Uses same database of tests as Modernizr

Photo: Jason Ahrns

flickr.com/photos/musubk/5578348098/

Tuesday, April 17, 12

Detector, by comparison, is only available in PHP. It’s still in beta. It does a lot more than detect whether a device is mobile or not. It also has the same database of feature detection as Modernizr.

Detector

<?php

if ($ua->isMobile)

{

// Code goes here

}

?>Photo: DigiTaL~NomAd

flickr.com/photos/urban-spaceman/3947680057/

Tuesday, April 17, 12

Here’s an example of how you might use Detector to detect mobile devices.

Preserve The URI When Redirecting Photo: Chris Campbell

flickr.com/photos/cgc/3809196941/

Tuesday, April 17, 12

If you’re using a method to detect and redirect mobile devices, remember to preserve the URL. That is, if they’re trying to get to a specific page or resource, make sure you redirect them to the equivalent page or resource on the mobile site. If you don’t have a mobile version, you shouldn’t be redirecting.

if (is_mobile() && !isset($_COOKIE['mobile'])){ $redirect = '/mobile/'; if (preg_match('/event\/(\d+)/', $_SERVER['REQUEST_URI'], $matches) > 0) { $redirect .= $matches[0]; } setcookie('mobile'); header('Location: ' . $redirect); exit;}

Tuesday, April 17, 12

JQuery Mobile

Tuesday, April 17, 12

We’ve used the JQuery Mobile framework for all of your mobile projects so far.

JQuery Mobile

Tuesday, April 17, 12

For one reason: it’s thoroughly tested. This is an image of the JQuery Mobile testing table of devices.

JQuery Mobile Setup<div data-role="page">

<div data-role="header">

<h1>Page Title</h1>

</div><!-- /header -->

<div data-role="content">

<p>Page content goes here.</p>

</div><!-- /content -->

<div data-role="footer">

<h4>Page Footer</h4>

</div><!-- /footer -->

</div><!-- /page -->

Tuesday, April 17, 12

Setting up the framework is fairly simple. First, it does a require an HTML5 doctype. After that, it makes extensive use of the HTML5 data attribute. You ca see several “roles” defined in the markup here.

Ajax Page Loads

<a itemprop="url" data-transition="slide" href="/path/">link</a>

Tuesday, April 17, 12

The framework also does a lot of heavy lifting for you. For instance, HTML page links are automagically turned into AJAX page loads with nice transitions defined in a “data-transition” attribute.

Ajax Page Loads

<a itemprop="url" data-transition="slide" href="/path/">link</a>

Tuesday, April 17, 12

The framework also does a lot of heavy lifting for you. For instance, HTML page links are automagically turned into AJAX page loads with nice transitions defined in a “data-transition” attribute.

JQuery Mobile Page Transition Options• fade

• pop

• flip

• turn

• flow

• slide

• slideup

• slidedown

• none

Tuesday, April 17, 12

The framework also offers several transition options.

JQuery Mobile Themes

Tuesday, April 17, 12

There are also 6 built-in themes available. You also have the option of designing and coding styles for your own theme.

JQuery Mobile Themes

<div data-theme="a"></div>

Tuesday, April 17, 12

Themes are defined using a “data-theme” attribute.

JQuery Mobile Grid Framework

<div class="ui-grid-a">

<div class="ui-block-a">

</div>

<div class="ui-block-b">

</div>

</div><!-- /grid-a -->

Tuesday, April 17, 12

There is also a grid framework available. Here’s an example of a two-column grid. There’s a wrapper with the class “ui-grid-a”. The child elements have classes of “ui-block-a” and “ui-block-b” respectively to place them as two columns on the page.

JQuery Mobile Grid Framework

• two-column: ui-grid-a

• three-column: ui-grid-b

• four-column: ui-grid-c

• five-column: ui-grid-d

Photo: Mike Smail

flickr.com/photos/smailtronic/2214772479/

Tuesday, April 17, 12

You can create grids all the way up to 5 columns.

Binding JavaScript Events in JQuery Mobile

Use pageInit() instead of$(document).ready()

Tuesday, April 17, 12

Since AJAX is used to load page content, $(document).ready() is only called with the first page load. To successfully trigger events when pages load, use JQuery Mobile’s pageInit instead.

REsponsive & Server Side (RESS)

Photo: foltzwerk

flickr.com/photos/foltzwerk/2779814860/

Tuesday, April 17, 12

Luke Wroblewski, who authored Mobile First, also has written about RESS, which is a combination of responsive design techniques and server-side feature detection.

• Heavy lifting handled on server side

• Able to provide a finely-tuned experience

• Removes need for redirects

REsponsive & Server Side (RESS)

Tuesday, April 17, 12

Some of RESS’ advantages: The work of feature detection is handled on the server side, making the browser on the phone do less work. It offers a way to create a very finely-tuned experience. No more redirects to a different mobile site. Preserves what Phil Hawksworth calls the contract of the URI for providing content.

• Harder to execute

• Requires some user agent sniffing

REsponsive & Server Side (RESS)

Tuesday, April 17, 12• Harder to execute

◦ more resources needed in terms of client and server side coding skills and time◦ A lot of thought and planning needs to go into components and device classes (phone, tablet, desktop, TV, etc.)

• Requires some user agent sniffing

There is no single solution and I will stand on Jeffery Zeldman’s coffee table and say that.

Tuesday, April 17, 12

Tools & ResourcesTesting

• Comprehensive list of emulators: mobilexweb.com/emulators

• springbox.com/mobilizer

Books• Responsive Web Design - Ethan

Marcotte

• Mobile First - Luke Wroblewski

Links• MobilePerf (z.umn.edu/mobileperf)

• Adobe Shadow (labs.adobe.com/technologies/shadow)

• lukew.com (@lukew)

• ethanmarcotte.com (@beep)

• jquerymobile.com

• detectmobilebrowsers.com

• detector.dmolsen.com

• mobilizr.com

Tuesday, April 17, 12

top related