Inflammation Factor App

Looking Glass Software is pleased to announce an agreement with Monica Reinagel, noted nutritional researcher and creator of the IF Rating™ system, to jointly develop an iPhone app utilizing her proprietary formula used to calculate the IF Rating™.

Based on Ms. Reinagel’s book, “The Inflammation Free Diet Plan”, the app will provide the unique ability to dynamically calculate an IF Rating™ based on serving size for thousands of foods. Further, it will allow users to record daily intakes and visually understand the nutritional balances of their diets to make healthy consumption choices.

For more information on the IF Rating™ system and its benefits, please visit: http://www.inflammationfactor.com or visit Ms. Reinagel’s web site, http://www.stayhealthy-livewell.com.

Updated Site

Just a quick note that we’ve updated our website. Sorry for any bookmarks or links that got confused in the process, but the new layout and organization is not only a lot cleaner, but allows for greater flexibility as we move forward.

Adding a TextField to an AlertView

Today, I was looking at how to add password protection to an app.  The basic design was the user could set a password via the options, then when they relaunched the app, would be asked to re-enter the numeric sequence.  If they got it right, the app would proceed as normal otherwise, terminate.

Ultimately, I went with the solution proposed by Jeff LaMarche on his blog.  But, many of the other solutions I found via Google appeared to use private APIs (such as addTextFieldWithValue:) which is a sure fire way to get rejected these days.

If you haven’t read the blog, or even better – his book, they are both great.  The book offers a very easy introduction to the iPhone SDK using practical examples.  It’s a great starting place for developers new to the SDK.

Managing Assets & Viewing Bundle Contents

Managing all of the assets that go into an iPhone app via Xcode can sometimes be tedious.  It seems somewhat counter intuitive to me to have the IDE manage these resources too.  Maybe I’m in the minority, but I’d prefer to just work with source code and NIBs in Xcode and keep track of the “other stuff” on my own.

The other day I was looking around for a solution to remove the non-Interface Builder assets from Xcode, organize these files into folders within my bundle, and remove the tons of Groups or Folder References from the IDE.  Happily, the answer wasn’t too bad.

Xcode Options

When you drag a resource into Xcode, it presents you with a dialog asking how to treat it:

asset_dialog

Starting from the top, the checkbox will copy the assets (and preserve the folder hierarchy, if present) while placing the resources at the root folder of the destination group.  The default radio box is to “Recursively create groups for any added folders” which is generally what you’ll want, especially if you’re using Interface Builder too.  In your bundle, these resources are placed at the “root” meaning to access them in your code, you’ll just need to specify a name.

The other radio option, “Create Folder References…”, creates a folder tree both inside Xcode and in your bundle.  However, the resources cannot be seen by Interface Builder and you’ll need to use a path if you load them at run-time.

Build Phase Copy

If you like the folder hierarchy in your bundle, or just want to move this management largely out of Xcode, it’s pretty easy to do it on your own by adding a new build phase, as shown below.  Right click on your Target and follow the cascading menus.

build_phase

In the build phase, add the following script

PBXCP=${DEVELOPER_DIR}/Library/PrivateFrameworks/DevToolsCore.framework/Resources/pbxcp
BASE=${PROJECT_DIR}/assets
DST=${BUILT_PRODUCTS_DIR}/${UNLOCALIZED_RESOURCES_FOLDER_PATH}
${PBXCP} -exclude .svn ${BASE} "${DST}"

The script calls an iPhone tool to recreate your folder structure inside your build directory before the final bundle is created.  With the script, I can remove any links or asset associations within Xcode except those I want to use in IB.  I maintain a folder “assets” off my product directory where I place any files that I’d like bundled in my app.

Checking Bundle Contents

Perhaps a trick most folks know, but I thought it was worth mentioning, is you can inspect and check the contents of your app bundle by following either navigating there via the Terminal or right clicking the app and selecting “Show Package Contents”.

I had a question about the database format of iGive Blood that shipped in version 1.0 and managed to find an old build from Apple.  For products that you’ve shipped, it’s almost the same.

  1. In iTunes, right click the app and select “Show in Finder”
  2. Copy the IPA file to your desktop
  3. Rename the extension to ZIP
  4. Double click the ZIP to create an uncompressed folder.
  5. Open the Payload folder which has the app.

It’s actually a bit disappointing that it’s so easy to get to the package contents especially as the contents aren’t obscured.  Maybe that’s a subject for another post…

Sometimes It’s the Obvious

Between working on a couple updates for our existing apps, I’ve been working on learning some new stuff with the iPhone SDK.  Sometimes it’s great to just wipe the canvas and start fresh!  So, I’m playing around with which UI* framework elements support custom artwork and creating some custom controls.  It’s fun, especially now that I’ve got some solid footing on the default appearance and behaviors of the common controls.

I tossed together a UITabBar app template and started filling out the custom UIViewControllers.  The code to push the view controller is pretty standard:

   if (myView == nil)
   {
      myView = [[UIViewController alloc] initWithNibName:@"testView" bundle:nil];
   }

   [self.navigationController pushViewController:myView animated:YES];

 

I created a corresponding Objective-C UIViewController class, testView, and associated the class in Interface Builder, including the “view” connection. 

testView 

The test view controller had a couple IBOutlets which I also created in IB and associated too. 

Save all… recompile in Xcode, debug – and Boom!  “NSUnknownException”, reason: “[<UIView 0x47a3e1>  setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key testControl.”

Nothing like a helpful error message, eh?

So – a quick return to Interface Builder where I double check the connections, Class Identity, and assorted properties that might trigger the exception.  Save all… recompile in Xcode, debug, and Boom!  Crashed again. 

Okay… time to get more serious.  Since these were empty classes anyway, I blew them away and started fresh.  Recreated the classes, the controls, the connections, and tried again.  No difference.  Ugh.  I swear I’ve done this before – a lot!

Well, I decided to pull out all of the controls except a UITableView and toss breakpoints throughout the UIViewController class to try to trap and step through exactly where things might be going amiss.  Recompile, debug, and things worked great!  The view appeared, including the table, but none of my breakpoints caught.

This was very odd, as I had breaks in initWithNibName:bundle:, loadView, viewDidLoad, and – just to be thorough – viewWillAppear:.  A little head scratching and I came to the quick conclusion that my custom class, despite being set in Interface Builder, isn’t actually being used.  And, after a few more minutes of triple-checking my settings, I discovered the source of the problem – I’d created a UIViewController instead of a “testView” (see above).  Changing the code fixed the problem – big surprise.

While I overlooked my declaration initially (as perhaps you did too!) – it just goes to show that sometimes it’s the obvious and before nuking work that probably is okay – give a quick inspection all around.

Security Updates, Xcode & Subversion

In addition to my full time job as a developer, I get occasional opportunities to moonlight as a system administrator.  Recently, we updated our Macs to the latest security patch Apple pushed out – and lo, our subversion integration with Xcode broke.  Sigh.

Back when we were initially setting up boxes for development, I very dimly recall there was some “shell magic” via the Terminal that needed to happen to get all the parts working together.  I think this was largely because Xcode preferred to work with an earlier version of subversion, perhaps 1.4.x – and it was no longer readily available at the time.

None the less, it’s easy to forget all the configuration steps in setting up a developer box – especially stuff that takes only a couple hours to figure out a nearly a year ago.  We’re not yet to Snow Leopard, so our tools may be a little dated: Xcode is 3.1.4 and Subversion is 1.5.5.

Symptoms

After the security upgrade, we fired up Xcode and noticed that there were:

  • No modified files in the project.
  • Right clicking a source file showed only “Get Annotations” in the Groups & Files pane.
  • Viewing the SCM | Repositories dialog (from the main menu) gave an error: 180001 unable to connect to the repository.
  • Even more scary was the Terminal message: svn: Expected FS format ‘2’; found format ‘3’

Solutions

Unfortunately, determining the solution wasn’t particularly clear.  Doing some Google searches on the error messages produced limited results.  The initial reaction was that something went wrong with the repository, but it was just working!  Running, “svnadmin verify” against the repository produced the File System format error message which was ominous.

The first thought was that perhaps the security patch affected the PATH variable.  Mac OS X ships with an older version of the subversion binaries and maybe we were picking up the wrong one.  On the Mac, various shell variables including your path are set in the .profile file found in your home directory.  A quick in the Terminal showed that /usr/bin was first in the path and /usr/local/bin (home to our newer svn) was last.  No problem – a quick edit and all was better.  Well… not quite.

Adjusting the path restored subversion from the Terminal and even the svnX app.  But, Xcode still stoutly refused to acknowledge the existence of the repository.  Grrr!  But, a step in the right direction and at least our repository was okay.

The breakthrough was trying to “Get Annotations” on a file.  There was a much more descriptive error – “155021 (Unsupported working copy format) please get a newer Subversion client.”  This jogged something loose in my mind and I dimly recall having to change some of the system libraries to get Xcode working with the newer Subversion.

After another quick search I found two other folks reported the same thing (Paul Solt and Blind Genius).  I’ll restate the solution here, but effectively Xcode is dynamically linking with the older Subversion libraries found in /usr/lib.  To work with newer builds of Subversion, you’ll need to create symbolic links to the newer dynamic libraries as follows:

cd /usr/lib

sudo mkdir svn.orig

sudo mv libap*-1.dylib svn.orig

sudo mv libsvn*-1.dylib svn.orig

sudo mv libap*-1.0.dylib svn.orig

sudo mv libsvn*-1.0.dylib svn.orig

sudo ln –s /opt/subversion/lib/*-1.0.dylib .

sudo ln –s /opt/subversion/lib*-1.dylib .

After adding the symbolic links, restart Xcode and things should be back to normal.

Cautions

One thing worth reverting is the PATH change.  Modifying the search order for executables might be considered a security risk as often 3rd party binaries (particularly shell ones) traditionally reside in /usr/local/bin and you might end up running something unexpected.  Perhaps safer is to create symbolic links to the binaries in /usr/bin, or alternatively command aliases in your .profile to run the correct version.

Common SQLite Operations

In nearly any program with persistent data that can be manipulated, I find myself writing three types of routines: insert, update, and delete. In the example below, I focus on using SQLite to perform these operations on some generic data.

To create a simple table for demonstration purposes, we can use the command-line ‘sqlite3’ utility.  In one of my previous posts, I discuss how to automate the (re)generation of your database, but this example is contrived enough that it doesn’t warrant such attention.  In the shell, use the following commands:

   $> sqlite3 db.sqlite
   sqlite> CREATE TABLE myObject (primaryKey INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, amount NUMERIC);
   sqlite> .quit
   $>

Getting this new database into your XCode project, and adding the SQLite3 framework would be next, but I’m going to skip over it, as it’s covered elsewhere and not difficult to put together.

Here’s a simple Objective-C object as the data model for our newly created table.

MyObject.h

@interface MyObject : NSObject
{
    NSInteger primaryKey;
    NSString *name;
    float amount;
}

@property (nonatomic, assign, readonly) NSInteger primaryKey;
@property (nonatomic, retain) NSString *name;
@property (nonatomic, assign) float amount;

// Returns: new MyObject with primary key
- (id)initWithPrimaryKey:(NSInteger)pk database:(sqlite3*)db;

// Updates the database with the object values
- (void)updateDatabase:(sqlite3*)db;

// Delete an object with primary key 'pk'
+ (void)removeWithPrimaryKey:(NSInteger)pk database:(sqlite3*)db;

// Returns: primary key of newly inserted object
+ (NSInteger)addMyObjectIntoDatabase:(sqlite3*)db;

// Clean up, called on app termination
+ (void)finalizeStatements;

@end

The class defines “insert” using addMyObjectIntoDatabase:, “update” using updateDatabase:, and “delete” with removeWithPrimaryKey:database:

The class also has a initialization member presumably called by the controller that has better visibility to all of the table elements.  I’ll fill in the method bodies, but leave the actual hook-up to higher level classes as an “exercise for the reader”.

In the pattern above, generally the controller class will issue a “SELECT primaryKey FROM myObject” statement, then iterate through the returned result.  With each row, the controller factory method creates a new MyObject using the row’s primary key.  But, there are a number of valid approaches depending on your implementation requirements.

Here’s the implementation body of the MyObject class.  I took the design choice of preparing the various queries and storing the result as a class static pointer.  The advantage is performance since the queries are parameterized and “pre-compiled” for SQLite.  The disadvantage is that they consume resources that must be released at some point – in our case using the finalizeStatements method.

MyObject.m

static sqlite3_stmt *init_MyObj_statement = nil;
static sqlite3_stmt *update_MyObj_statement = nil;
static sqlite3_stmt *delete_MyObj_statement = nil;
static sqlite3_stmt *insert_MyObj_statement = nil;

@implementation MyObject

@synthesize primaryKey;
@synthesize name;
@synthesize amount;

- (id)initWithPrimaryKey:(NSInteger)pk database:(sqlite3*)db
{
   if (self = [super init])
   {
      primaryKey = pk;
      if (init_MyObj_statement == nil)
      {
         const char *sql = "SELECT * FROM myObject WHERE primaryKey=?";
         int result = sqlite3_prepare_v2(db, sql, -1, &init_MyObj_statement, NULL);
         NSAssert1(result == SQLITE_OK, @"initWithPrimaryKey: failed to prepare statement with err '%s'", sqlite3_errmsg(db));
      }

      sqlite3_bind_int(init_MyObj_statement, 1, primaryKey);

      if (sqlite3_step(init_MyObj_statement) == SQLITE_ROW)
      {
         int columnID = 1; // 0 is primary key
         char *nameStr = (char*)sqlite3_column_text(init_MyObj_statement, columnID++);
         name = (nameStr) ? [[NSString stringWithUTF8String:nameStr] retain] : @"";

         amount = sqlite3_column_double(init_MyObj_statement, columnID);
      }

      sqlite3_reset(init_MyObj_statement);
   }

   return self;
}

- (void)updateDatabase:(sqlite3*)db
{
   if (update_MyObj_statement == nil)
   {
      const char *sql = "UPDATE myObject SET name=?, amount=? WHERE primaryKey=?";
      int success = sqlite3_prepare_v2(db, sql, -1, &update_MyObj_statement, NULL);
      NSAssert1(success == SQLITE_OK, @"updateDatabase: failed to prepare with message '%s'", sqlite3_errmsg(db));
   }

   int columnID = 1;

   sqlite3_bind_text(update_MyObj_statement, columnID++, [name UTF8String], -1, SQLITE_TRANSIENT);
   sqlite3_bind_double(update_MyObj_statement, columnID++, amount);
   sqlite3_bind_int(update_MyObj_statement, columnID++, primaryKey);

   int success = sqlite3_step(update_MyObj_statement);
   NSAssert1(success == SQLITE_DONE, @"updateDatabase: update failed with message '%s'", sqlite3_errmsg(db));

   sqlite3_reset(update_MyObj_statement);
}

+ (void)removeWithPrimaryKey:(NSInteger)pk database:(sqlite3*)db
{
   if (delete_MyObj_statement == nil)
   {
      static char *sql = "DELETE FROM myObject WHERE primaryKey=?";
      int result = sqlite3_prepare_v2(db, sql, -1, &delete_MyObj_statement, NULL);
      NSAssert1(result == SQLITE_OK, @"removeWithPrimaryKey: failed with message '%s'", sqlite3_errmsg(db));
   }

   sqlite3_bind_int(delete_MyObj_statement, 1, pk);
   int success = sqlite3_step(delete_MyObj_statement);
   NSAssert1(success == SQLITE_DONE, @"removeWithPrimaryKey: failed with message '%s'", sqlite3_errmsg(db));
   sqlite3_reset(delete_MyObj_statement);
}

+ (NSInteger)addMyObjectIntoDatabase:(sqlite3*)db
{
   if (insert_MyObj_statement == nil)
   {
      static char *sql = "INSERT INTO myObject (name) VALUES ('')";
      int result = sqlite3_prepare_v2(db, sql, -1, &insert_MyObj_statement, NULL);
      NSAssert1(result == SQLITE_OK, @"addMyObjectIntoDatabase: failed to prepare statement with err '%s'", sqlite3_errmsg(db));
   }

   int success = sqlite3_step(insert_MyObj_statement);
   if (success != SQLITE_ERROR)
   {
      return sqlite3_last_insert_rowid(db);
   }

   NSAssert1(0, @"addMyObjectIntoDatabase: failed with message '%s'", sqlite3_errmsg(db));
   return -1;
}

+ (void)finalizeStatements
{
   if (init_MyObj_statement)
      sqlite3_finalize(init_MyObj_statement);
   if (update_MyObj_statement)
      sqlite3_finalize(update_MyObj_statement);
   if (delete_MyObj_statement)
      sqlite3_finalize(delete_MyObj_statement);
   if (insert_MyObj_statement)
      sqlite3_finalize(insert_MyObj_statement);
}

@end

App Analytics

One of the more daunting aspects of app development is the lack of direct contact with your customers.  With Apple serving as an intermediary, it can be difficult to know what folks like (or dislike!) about your product.  While we get a fair share of constructive feedback about our products, people occasionally post to iTunes or elsewhere online which isn’t regularly monitored.  Thus, it’s possible that we miss a really good idea or could prioritize features better based on usage.

Some make a claim for analytics as a developer tool to combat piracy but that seems less clear to me.  Knowing – even expecting – that a popular app will attract the attention of pirates should be something most developers understand and appreciate.  It’s not like the problem is unique to the App Store, nor is there a unique iPhone solution to combat it.  Piracy exists for all major computer platforms including consoles.  I’d like to think that the majority of iPhone users are honest and would rather pay a nominal amount for an app rather than jump through the required hoops (possibly making their phone less secure) to obtain something illegally.

I’ve been playing around with Pinch Media and their analytics package.  I put together a quick test app to poke around with the API and see how it performed.  It was pretty cool.  The basic process of getting it into an app was easy:

  1. Get the library.
  2. Copy the lib and header into your project.
  3. Include three frameworks: libsqlite, SystemConfiguration and CoreLocation.
  4. Start the data capture on startup.
  5. Stop the data capture on terminate.

Rather than call directly into the Pinch API, I wrote a simple singleton to wrap the interface.  I didn’t want events being triggered during development in the simulator, or in the event we want to add an app option/preference to “opt in”.

The implementation file is a bit “loose” – especially the handling of the static singleton which I’d probably cleanup if attempting to productize this code.  However, for experimentation purposes, the wrapper was a snap and looks something like this:

Analytics.h


#import <Foundation/Foundation.h>
@class CLLocation;

@interface Analytics : NSObject
{
}

+ (id)initAnalytics:(NSString*)appCode useCoreLocation:(BOOL)coreLocation useOnlyWiFi:(BOOL)wiFi;
+ (void)endAnalytics;
+ (id)shared;
- (void)startSubWithName:(NSString*)name timeSession:(BOOL)timed;
- (void)endSubWithName:(NSString*)name;
- (void)setLocation:(CLLocation*)location;

@end

Analytics.m


#import "Analytics.h"
#import "Beacon.h"

#if (!TARGET_IPHONE_SIMULATOR)
static Analytics *obj_Analytics = nil;
#endif

@implementation Analytics

#if (!TARGET_IPHONE_SIMULATOR)

- (id)init
{
   self = [super init];
   obj_Analytics = self;
   return obj_Analytics;
}

+ (id)initAnalytics:(NSString*)appCode useCoreLocation:(BOOL)coreLocation useOnlyWiFi:(BOOL)wiFi
{
   return [Beacon initAndStartBeaconWithApplicationCode:appCode useCoreLocation:coreLocation useOnlyWiFi:wiFi];
}

+ (void)endAnalytics
{
   [obj_Analytics release];
   obj_Analytics = nil;

   [[Beacon shared] endBeacon];
}

+ (id)shared
{
   if (obj_Analytics == nil)
   {
      obj_Analytics = [[self alloc] init];
   }

   return obj_Analytics;
}

- (void)startSubWithName:(NSString*)name timeSession:(BOOL)timed
{
   [[Beacon shared] startSubBeaconWithName:name timeSession:timed];
}

- (void)endSubWithName:(NSString*)name
{
   [[Beacon shared] endSubBeaconWithName:name];
}

- (void)setLocation:(CLLocation*)location
{
   [[Beacon shared] setLocation:location];
}

#else

+ (id)initAnalytics:(NSString*)appCode useCoreLocation:(BOOL)coreLocation useOnlyWiFi:(BOOL)wiFi { return nil; }
+ (void)endAnalytics {}
+ (id)shared { return nil;}
- (void)startSubWithName:(NSString*)name timeSession:(BOOL)timed {}
- (void)endSubWithName:(NSString*)name {}
- (void)setLocation:(CLLocation*)location {}

#endif

@end

After hooking in the startup/shutdown code in your app delegate, the idea is you scatter “markers” throughout your app to track or even time various events using the startSubWithName:timeSession method.

To use the API, you’ll need to register with the Pinch Media site to obtain a unique app-id that will aggregate various statistics on their server.  The library collects a number of stats by default including:

  1. OS version
  2. Device type
  3. Demographic information (if known)
  4. Regional usage (if using CoreLocation)
  5. Lifecycle data (app version, time used, number of actions, etc.)

And – all the various actions you scattered too.  The default charts on the web site are good but basic.  You can export the data to CSV or access it online via a nifty API.

While running a test over a few minutes, I noticed the library (r69) leaked a small amount of memory during the asynchronous send.  During the transfer, I didn’t detect a noticeable performance impact but then, the test app wasn’t exactly doing real time calculations either.  Instruments reported about 1 kb in the library thread.  The library appears to periodically send data perhaps after a certain time period or once the event queue fills.    The data is also sent when the app terminates – which apparently can cause Springboard to “force quit” your app and lead to a misleading crash report.

Viewing the data online took a while.  It was about 24 hours for the test data to show up on the website, and nearly 48 hours for the custom events to appear.

Linking with the library (and the other frameworks) added about 200 kb to the binary, although that may be slightly misleading as libsqlite3 was already being linked into the tester app.

I’m glad that the library also offered a feature to enable developers to expose “opt in” features as users can sometimes be sensitive to profiling, even if it’s anonymous.  Before hooking up the service to your app, it’d be worth checking out their privacy policy and making sure you’re not passing user specific data (which Pinch discourages and won’t allow) to their site.

If you’re looking for some insight into what your customers are doing with your app, linking in Pinch Media’s analytics library may be the answer.  From an ease of integration point of view, it was definitely very easy and worked as advertised.