Android
https://www.zengenuity.com/
enCreating an iPhone App for a Drupal Website
https://www.zengenuity.com/blog/2011-05/creating-iphone-app-drupal-website
<span class="field field--name-title field--type-string field--label-hidden">Creating an iPhone App for a Drupal Website</span>
<div class="paragraph html">
<div class="container">
<p><img src="https://www.zengenuity.com/sites/default/files/migrated/LA2M-iphone-app-1_0_0.png" alt="iPhone App for Drupal" title="iPhone App for Drupal" width="150" height="299" />Recently, Zengenuity released the <a href="https://zengenuity.com/portfolio/iphone-app-drupal-website">LA2M to Go iPhone app</a>, a companion to the <a href="https://zengenuity.com/portfolio/non-profit-marketing-group-website">Lunch Ann Arbor Marketing website</a>. The LA2M website is one we built on Drupal 6, and it uses custom content types, <a href="https://drupal.org/project/views">Views</a> and <a href="https://drupal.org/project/imagecache">Imagecache</a> for displaying upcoming and archived event information for the group. In creating our app, I wanted to build a system to synchronize the mobile device with the website, since event information changes on at least a weekly basis. It took a lot of research and testing various techniques (many of which of which didn’t end up working), but finally I worked out how to have the app to pull all its information automatically from the <a href="https://drupal.org">Drupal</a> site. I hope that by publishing this blog post, I can speed up this process for you, and save you some frustration. There isn’t room here to go into every detail, but I’ll discuss the major frameworks I used, and I’ll point out some important issues that wasted a lot of my time.</p>
<h2>The Titanium Mobile Framework</h2>
<p><img src="https://www.zengenuity.com/sites/default/files/migrated/titanium.png" alt="Titanium SDK" title="Titanium SDK" width="295" height="200" />LA2M to Go is built with the <a href="https://www.appcelerator.com/products/titanium-mobile-application-development/">Titanium Mobile Framework</a> by <a href="https://www.appcelerator.com/">Appcelerator</a>. The Titanium SDK is free, and it allows you to write native iPhone apps in web developer friendly Javascript, instead of Objective-C. It also allows you to compile the same source code as a native Android app, though this is something I haven’t tried yet. To get started with Titanium, <a href="https://www.appcelerator.com/products/download/">download the SDK here</a>. This will provide you with the Titanium Developer application you need to compile your app. You will also need to have developer tools for the target mobile platform you plan to work with. So, if you are writing an iPhone app, you need to have an <a href="https://developer.apple.com/programs/register/">Apple iOS developer account</a>, and download XCode and the iOS SDK from Apple. Android developers will need to get the <a href="https://developer.android.com/sdk/index.html">Android SDK from Google</a>.</p>
<p>When coding for Titanium, you have to use your own text editor or IDE, and then load your project into the Titanium Developer application. Titanium-based apps are compiled in a two-step process. First, your code is rewritten by Titanium Developer as native iPhone or Android source code. Then, command line tools provided by the iPhone and Android SDKs are used to compile the native code and install it into the simulator or directly onto your mobile device. </p>
<p><strong>One word of warning: This process absolutely sucks.</strong> It’s the worst compilation and debugging system I’ve ever experienced. When working with Titanium, be prepared to write a lot of output log statements, because that’s how you’ll be doing much of your debugging. Also, the Titanium platform is seriously lacking in documentation, and the docs that do exist are difficult to use and search. There are very few example code snippets in the <a href="https://developer.appcelerator.com/apidoc/mobile/latest">API docs</a>, which means you need to search the <a href="https://developer.appcelerator.com/questions/created">developer forums</a>. In the forums, code is often wrong or out-of-date, as there’s no way to know which version of the Titanium API it’s designed for. (The API has changed significantly from its initial release.) Appcelerator recently purchased <a href="https://aptana.com/">Aptana</a>, the makers of the excellent Aptana Studio IDE. So, I’m praying they ditch the Titanium Developer application for a more fully-featured IDE, with debugging tools and built-in documentation. This would help developers immensely. (<strong>Update (5/17/11):</strong> They have now done exactly this: <a href="https://developer.appcelerator.com/blog/2011/05/major-titanium-updates.html">https://developer.appcelerator.com/blog/2011/05/major-titanium-updates.html</a>)</p>
<p>That said, I’ve written code in Objective-C with XCode before, and while XCode has both excellent debugging features and documentation, the speed at which you I create robust apps with Titanium, in a language that is more familiar to me as a web developer, means I’ll coming to back to it for my next app, despite its failings. </p>
<h2>The DrupalCon Chicago App</h2>
<p><img src="https://www.zengenuity.com/sites/default/files/migrated/drupalcon-chicago.jpeg" alt="DrupalCon Chicago App" title="DrupalCon Chicago App" width="175" height="175" />If you were at <a href="https://chicago2011.drupal.org/">DrupalCon Chicago</a>, you probably used the <a href="https://itunes.apple.com/us/app/drupalcon/id421074805?mt=8">conference app</a> that was available for iPhone and Android. That app was created in Titanium, by the folks at <a href="https://www.palantir.net/">Palantir</a>. Fortunately for all of us developing Drupal-connected apps, they have made <a href="https://github.com/palantirnet/drupalcon_mobile">the app’s source code available</a>, and <a href="https://www.palantir.net/blog/mobilizing-drupalcon-chicago">their blog post about it is very informative</a>.</p>
<p>There isn’t a lot of technical documentation about how expand or change the app, but after spending some time with it, I was able to figure out the key details:</p>
<h3>Schema Definitions</h3>
<p>The app is designed to import Drupal nodes and store them in an SQLite database on the device. This way the app can be used offline. In the code, there is a schema definition designed for the DrupalCon event node type in the <span><strong>Resources/drupalcon/entity.js</strong></span> file. (You will also find a schema for Drupal.org users in there, too.) If you are building a new app from this source code, you need to modify their schema to match whatever fields your content types have. They have chosen to call their event schema “node”, but you can rename this to whatever you want, and add additional schemas for more types. If you are adding new schema types (or changing the name of them), you need to make entries for them in the <strong><span>Resources/drupal/entity.js</span></strong> file. There’s a section at the top that starts with </p>
<p><strong><span>main: {<span> <br /></span> ...</span></strong><br /><strong><span> types {</span></strong><br /><strong><span> <em>[a list of types and primary keys is here]</em></span></strong></p>
<p>Add your new schema names to this list, following the pattern of the others in that section.</p>
<p>One thing I found useful when modifying the schema were the two commented out lines in app.js that clear the DB on every app launch. </p>
<p><strong><span>//Drupal.entity.db('main', 'node').initializeSchema();</span></strong><br /><strong><span>//Drupal.entity.db('main', 'user').initializeSchema();</span></strong></p>
<p>These obviously need to be commented out in production, but are useful when changing the schema. (Adjust the parameters to fit your schema names.) Another helpful tool you will want to have is some sort of SQLite database browser. I ended up using the <a href="https://addons.mozilla.org/en-US/firefox/addon/sqlite-manager/">SQLite Manager Firefox extension</a>, though, I didn’t especially enjoy it. It works, but it’s kind of clunky. If you are building and iPhone app and working in the simulator, the SQLite database is difficult to find. Here’s where it’s at:</p>
<p><span><strong>/Users/[your_username]/Library/Application Support/iPhone Simulator/[sdk_version]/Applications/[application_guid]/Library/Application Support/database/main.sql</strong></span></p>
<p>Yeah...it’s really buried down there. One of those folders is hidden too, so you will not find that database file with Finder. You just have to know where it is.</p>
<h2>Renderers</h2>
<p>If you look in <strong><span>Resources/drupalcon/drupalcon.js</span></strong>, you will see a series of sections that start like this:</p>
<p><strong><span>DrupalCon.renderers.session = function(session) {</span></strong></p>
<p>These sections define “renderers” for different entities that you have in the SQLite database. They don’t do anything on their own. You have to call it yourself after you query the DB. But, when you look around in the app code, you’ll see that having a function that renders an SQLite database row as a TableViewRow is very handy. It’s sort of like defining a theme template for a particular node type. If you have added fields and types to the database schema, you will need to update the renderers to display your new fields.</p>
<h2>Windows</h2>
<p>In <strong><span>Resources/windows</span></strong> are a bunch of files that define the various screens of the app. I don’t have space in this post to go through them all, but if you look at each one, you’ll get an idea of what you need to do to make similar windows that match your project. Most of them are TableViews, which is often what you want. There’s also a MapDetailWindow that shows a static image, and other windows to render HTML content. Be sure to include any new window files you create in your <strong><span>Resources/app.js</span></strong> file. (There’s a section in it with all the includes in a list.) Also, you need to add your new windows to the app’s tab group. That can be done by following the pattern in <strong><span>Resources/main.js</span></strong>. </p>
<h2>Connecting the App to Drupal</h2>
<p>When I started this project, I expected to use the <a href="https://drupal.org/project/services">Services module </a>to access the Drupal data over JSON. My plan was to create a view for each content type that I needed to download and expose those views with the Views Service module. However, when trying this approach, I came across a big limitation of Views Service. This module only exposes the Views data that is directly returned by the query. It completely skips the normal rendering step of Views. This means if you want to include the URL for a specific Imagecache-processed version of an image, it can’t be done with Views Service. You also can’t rewrite the output of fields to combine two fields into one. Pretty much everything that is awesome about Views you can’t do with Views Service. </p>
<p>Views Datasource to the rescue! The day after I hit this limitation, Palantir released their code for their DrupalCon app. In their post, you will see they reference the <a href="https://drupal.org/project/views_datasource">Views Datasource module</a> as an alternative to Services. Views Datasource allows you to expose Views as JSON, and it DOES send the output through the normal Views rendering engine. So, this means you can rewrite fields, get Imagecache urls, change field labels, and probably re-theme fields if you need to, though I didn’t try this last one. With this module, I was able to get exactly the JSON output I needed to download data into the app. I’m not going to go into a full detailed explanation, because actually, if you know how to use Views, it’s pretty straightforward. It works exactly like you expect it to.</p>
<h2>Putting Everything Together</h2>
<p><img src="https://www.zengenuity.com/sites/default/files/migrated/la2m-to-go-icon.jpeg" alt="LA2M to Go" title="LA2M to Go" width="175" height="175" />Once I had the JSON-formatted view on the Drupal site and the schemas defined in my application, everything else was just a matter of getting of the user experience and workflow that I wanted. (Ever app is different, so you’ll have to study the Titanium API and figure out what works for your project.) After only a couple weeks of development, I was able to go from conception to launching <a href="https://itunes.apple.com/us/app/la2m-to-go/id433060438?mt=8">LA2M to Go in the App Store</a>. While it was frustrating at times deciphering both the Titanium debug output and the DrupalCon app’s design, I’m really happy with the outcome. I’ll definitely use both of these technologies in future for Drupal-connected iPhone apps, and hopefully, this post will make it a little less frustrating for you to do the same.</p>
<h2>Useful Resources</h2>
<p><a href="https://developer.appcelerator.com/get_started">Getting Started with Titanium Mobile Platform</a></p>
<p><a href="https://developer.appcelerator.com/blog/2010/05/how-to-create-a-tweetie-like-pull-to-refresh-table.html">How to Create a Pull-to-Refresh Table in Titanium</a> - Everyone loves this UI control.</p>
<p><a href="https://github.com/workhabitinc/drupal-ios-sdk">Drupal-iOS SDK</a> - A separate project that connects an iPhone app to a Drupal site using only native code in XCode. Have not tried this one out. </p>
</div>
</div>
<span>Wayne Eaker</span>May 10, 2011 <div class="tags">
<div class="container">
<span class="tag"><a href="https://www.zengenuity.com/blog/tags/android" hreflang="en">Android</a></span>
<span class="tag"><a href="https://www.zengenuity.com/blog/tags/drupal" hreflang="en">Drupal</a></span>
<span class="tag"><a href="https://www.zengenuity.com/blog/tags/drupal-planet" hreflang="en">Drupal Planet</a></span>
<span class="tag"><a href="https://www.zengenuity.com/blog/tags/drupalcon-chicago" hreflang="en">DrupalCon Chicago</a></span>
<span class="tag"><a href="https://www.zengenuity.com/blog/tags/iphone" hreflang="en">iPhone</a></span>
<span class="tag"><a href="https://www.zengenuity.com/blog/tags/mobile" hreflang="en">Mobile</a></span>
<span class="tag"><a href="https://www.zengenuity.com/blog/tags/services-module" hreflang="en">Services module</a></span>
<span class="tag"><a href="https://www.zengenuity.com/blog/tags/titanium-mobile-framework" hreflang="en">Titanium mobile framework</a></span>
<span class="tag"><a href="https://www.zengenuity.com/blog/tags/views-module" hreflang="en">Views module</a></span>
</div>
</div>
Tue, 10 May 2011 12:00:00 +0000Wayne Eaker143 at https://www.zengenuity.com