PhoneGap is a great framework for building native iOS, Android, etc apps with just HTML, CSS & JS. However, there is a slight misconfiguration in the way PhoneGap sets up new Xcode projects, and this will not allow 3rd party analytic packages to work correctly.
Services such as Flurry and Tapjoy require you to add a bit of code into the “applicationDidFinishLaunching” section of your AppDelegate file (located in your Xcode project -> “Classes” folder -> “APPNAMEAppDelegate.m”). However, this code never actually gets executed. Below is how to setup your AppDelegate to work correctly.
First, you’ll need to include the framework’s files into your Xcode project. Follow the specific instructions from the provider as these may change from library to library and from version to version.
Second, open up the AppDelegate.m file in the “Classes” folder for your project. It’s easier to do this while in Xcode.
Add the import reference for the supplied .h file for your provider underneath the existing import references. I’ll be using Flurry Analytics as an example:
Old:
[sourcecode language=”plain”]#import "APPNAMEAppDelegate.h"
#import "PhoneGapViewController.h"[/sourcecode]
New:
[sourcecode language=”plain”]#import "APPNAMEAppDelegate.h"
#import "PhoneGapViewController.h"
#import "FlurryAPI.h"[/sourcecode]
Next you’ll be removing a block of code and replacing it with a new one.
Remove:
[sourcecode language=”plain”]/**
* This is main kick off after the app inits, the views and Settings are setup here.
*/
– (void)applicationDidFinishLaunching:(UIApplication *)application {
[ super applicationDidFinishLaunching:application ];
}[/sourcecode]
Replace with:
[sourcecode language=”plain”]
/**
* This is main kick off after the app inits, the views and Settings are setup here.
*/
– (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
//Custom Code Here
[FlurryAPI startSession:@"FLURRYSECRET"];
return [ super application:application didFinishLaunchingWithOptions:launchOptions ];
}
[/sourcecode]
And that’s it! Command-S to save your file and you should be fully integrated with your 3rd party analytics package.