23.01.2018 Views

MICROSOFT_PRESS_EBOOK_PROGRAMMING_WINDOWS_8_APPS_WITH_HTML_CSS_AND_JAVASCRIPT_PDF

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Network Information and Connectivity<br />

In the previous chapter, I mentioned in a footnote how at the very time I was writing on the subject of<br />

live tiles and all the connections that Windows 8 apps can have to the Internet, my home and many<br />

thousands of others in Northern California were completely disconnected due to a fiber optic<br />

breakdown. The outage lasted for what seemed like an eternity by present standards—36 hours!<br />

Although I wasn’t personally at a loss for how to keep myself busy, there was a time when I opened one<br />

of my laptops, found that our service was still down, and wondered for a moment just what the<br />

computer was really good for without connectivity! Clearly I’ve grown, as I suspect you have too, to take<br />

constant connectivity completely for granted.<br />

As developers of great apps, however, we cannot afford to be so complacent. It’s always important to<br />

handle errors when trying to make connections and draw from online resources, because any number of<br />

problems can arise within the span of a single operation. But it goes much deeper than that. It’s our job<br />

to make our apps as useful as they can be when connectivity is lost, perhaps just because our customers<br />

got on an airplane and switched on airplane mode. That is, don’t give customers a reason to wonder<br />

about the usefulness of their device in such situations! A great app will prove its worth through a great<br />

user experience even if it lacks connectivity.<br />

Connectivity can also vary throughout an app session, where an app can often be suspended and<br />

resumed, or suspended for a long time. With mobile devices especially, one might switch between any<br />

number of networks without necessarily knowing about it. Windows 8, in fact, tries to make the<br />

transition between networks as transparent as possible, except where it’s important to inform the user<br />

that there may be costs associated with the current network. It’s required by Window Store policy, in<br />

fact, for apps to be aware of data transfer costs on metered networks and to prevent “bill shock” from<br />

not-always-generous mobile broadband providers. Just as there are certain things an app can’t always<br />

do when the device is offline, the characteristics of the current network might also cause it to defer or<br />

avoid certain operations as well.<br />

Let’s now see how to retrieve and work with connectivity details, starting with the different types of<br />

networks represented in the manifest, followed by obtaining network information, dealing with metered<br />

networks, and providing for an offline experience.<br />

Network Types in the Manifest<br />

Nearly every sample we’ve worked with so far in this book has had the Internet (Client) capability<br />

declared in its manifest, thanks to Visual Studio turning that on by default. Once before I mentioned<br />

how this wasn’t always the case: early app builders within Microsoft would occasionally scratch their<br />

heads wondering just why something really obvious—like making a simple XmlHttpRequest to a<br />

blog—failed outright. Without this capability, there just isn’t any Internet!<br />

Still, Internet (Client) isn’t the only player in the capabilities game. Some networking apps will also<br />

want to act as a server to receive incoming traffic from the Internet, and not just make requests to other<br />

631

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!