XPages Mobile Application Development
- Apr 30, 2014
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.
Listing 14.1 Display User Agent
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
Mozilla/5.0 (iPhone; CPU iPhone OS 6_1 like Mac OS X) AppleWeb- Kit/536.26 (KHTML, like Gecko) Version/6.0 Mobile/ 10B143 Safari/8536.25
Figure 14.1 shows how the page displays on an iPhone.
Figure 14.1 iPhone User Agent
Straight away I can see problems:
- 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.
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:
Mozilla/5.0 (iPhone; U; CPU iPhone OS 4_3_3 like Mac OS X; en-us) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/ 8J2 Safari/6533.18.5
The Chrome browser provides similar functionality as follows:
- Go to the Tools menu and select Developer tools.
- 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.
Figure 14.3 Chrome Developer tools Overrides tab
There is an add-on for FireFox called User Agent Switcher (addons.mozilla.org/en-US/firefox/addon/user-agent-switcher/), which provides the equivalent functionality. One nice feature of this add-on includes the capability to define your own User Agent string.
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.
Listing 14.2 User Agent Device Detection
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.
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.
Listing 14.3 DeviceBean
Figure 14.4 shows the values that are displayed when accessing this page with the User Agent set to that of an iPad.
Figure 14.4 iPad Device Bean Values
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.