This chapter focuses on best practices and design patterns for XPages Mobile Application Development. So even if you are familiar with building mobile applications with XPages, this chapter contains some discussion that you will find interesting.

This chapter is from the book

The mobile features from the XPages Extension Library were promoted into the core XPages runtime in Domino 9.0. This reflects the importance of mobile support in application development as mobile devices (phones and tablets) move to outsell desktop systems and are becoming the norm for how people access web applications. XPages has implemented a Mobile Web Development strategy—that is, it uses web technologies to provide mobile access to your applications. Mobile devices feature powerful web browsers; however, the web interface you have built for desktop clients just won’t cut it for mobile clients. If you have ever accessed the full version of a website from a mobile device, you will have experienced first-hand the type of problems encountered when there is no mobile version of a site. These include

Limited resources: Device processor power, memory, and network bandwidth all tend to be limited on a mobile device.

User experience: Users have particular expectations when using a mobile device—for example, fast response times, navigation to most important features, minimal data entry, UI adapts to device orientation, and many more.

Limited functionality: Users typically need only a subset of functionality and expect applications to reuse functionality from other applications on their mobile device.

The XPages Extension Library book provides an introductory description of the XPages mobile controls and the pattern to be used to develop a Create, Read, Update, Delete (CRUD) mobile sample application. The approach this chapter takes is to focus on best practices and design patterns for XPages Mobile Application Development. So even if you are familiar with building mobile applications with XPages, this chapter contains some discussion that you will find interesting. While writing the second edition of this book, Domino 9.0.1 had just been released. It includes some important enhancements for mobile developers, which will be covered in this chapter. For an excellent description of the best practices for Mobile Web Applications, visit www.w3.org/TR/mwabp/. Some of the best practices outlined in this document are referred to later in the chapter. Be sure to download the chp14ed2.nsf file provided online for this book to run through the exercises throughout this chapter. You can access these files at www.ibmpressbooks.com/title/9780133373370.

Getting Started with Mobile Application Development

Start with a simple XPage that displays the browser User Agent string. This enables you to detect which device is accessing your application. When you preview the XPage shown in Listing 14.1 in a browser, it displays the User Agent string for your browser, which for Firefox version 18.02 is Mozilla/5.0 (Windows NT 6.1; WOW64; rv:18.0) Gecko/20100101 Firefox/18.0.

As I write this, I’m using my home wireless network—so now I can enter the URL for this page into the Safari browser on my iPhone, and I can see the page rendered there. The User Agent string displayed is

Typing a URL on my iPhone is painful; I don’t want to have to do a lot of typing using this device.

I need to pinch and zoom to see the text in the browser. By default I’m seeing the full page with some tiny text at the top, which is not readable.

This is going to slow down my development if I have to keep switching between my development machine and device to test my changes.

What if I don’t have an iPhone, an iPad, or an Android device?

The first thing to know is that you don’t need a device to get started with Mobile Application Development. There are a number of alternative options to testing on a real device:

Using a device emulator, these are typically part of a mobile platform SDK and are available for Mac, Android, Microsoft, and Blackberry devices.

You can modify the User Agent in your desktop browser.

Most of the demonstrations in this chapter use the technique of overriding the User Agent your desktop browser sends with each request. User Agent spoofing doesn’t provide 100 percent fidelity with the actual device but is a quick way to get your application built before you begin testing on real devices. The remainder of this chapter uses the Safari and Chrome browsers to emulate Apple and Android devices, respectively.

Safari Browser

The Windows versions of the Safari browser are available from the Apple support site. The WebKit engine used by the Safari for Windows browser is similar to the one on the Apple iPhone and iPad, so this browser is a good option for basic emulation of the Apple mobile devices. Use the following steps to override the User Agent string sent by the browser:

If you do not have menus enabled by default, do so via the Show Menu Bar from the General Safari Settings toolbar drop-down.

Open Preferences and go to the Advanced tab.

Select the option to Show Develop menu in menu bar.

Select the User Agent override you want to use from the Develop -> User Agent menu.

Figure 14.2 shows the Safari User Agent choices. You can now select one of these. If you access the XPage in the Safari browser, the page displays the appropriate User Agent string.

Figure 14.2 Safari User Agent choices

For example, I selected Safari iOS 4.3.3—iPhone, and the XPage displayed the following User Agent:

Chrome Browser

Select the Settings (cogged wheel) icon in the bottom-right corner of the Developer tools panel.

Select the Overrides tab.

Figure 14.3 shows the Chrome developer tools Overrides tab. You can override the User Agent and also other settings like the device metrics and orientation. The device metrics and orientation and useful for giving you that immediate feedback on how your page will be rendered on the device.

User Agent Device Detection

Detecting that your application is being accessed using a mobile device is important because it allows you to use server-side logic to adapt the content for the requesting client. It is preferable to do the adaptation on the server-side because this will improve the user experience and prevent the transfer of unnecessary data. The User Agent is typically used to detect the device in use. Listing 14.2 shows an example of how to detect if the device is an iPhone, iPad, Android, or BlackBerry device.

Given the large number of devices in use and that new devices come to the market frequently, this type of coding can become complex. The best practice for mobile applications is to use broader device classification to simplify the process of adapting your content. For example, you might want to generate different content for mobile phones versus tablet devices. In Domino 9.0.1, a new managed bean called the deviceBean has been added to the XPages runtime to simplify this process and allow you to implement a device classification strategy.

Device Bean

The Device Bean is used to identify the most common mobile and tablet devices—that is, Android; Apple iPhone or iPad; Blackberry; or Windows Mobile devices. The heavy lifting of parsing the User Agent string is handled for you. The most commonly used methods are deviceBean.isMobile() and deviceBean.isTablet(). For tablet devices, the method deviceBean.isMobile()returns false, which means you often see the two values being OR’d to determine if any mobile device is used. Listing 14.3 shows a list of the values available from the Device Bean.

Now we have an easy to use technique to identify that a mobile or tablet device is accessing our application we can use this information to adapt the presentation of our application to a form that is suitable for the device being used. The design pattern used to present content suitable for use in mobile applications is the Single Page Application pattern and this is the topic for the next section.

TIP

The device bean is cached for the lifetime of a user session, which means if you are using a browser plug-in to change the user agent Agent you actually need to restart the browser for a change to take effect. Starting in Domino 9.0.1 there is a new mobile property in the Xsp properties called Debug user agent. This allows you to specify either an iOS or Android User Agent, and the change will take effect the next time you load a page from that application.