If you were watching twitter this week, you might have heard that OpenFeint 2.4 was quietly released to developers. This update surely stretches the definition of “point release” because it is packed with awesome new features as well as a complete overhaul of the interface.
If you are upgrading to OpenFeint 2.4 from a previous version, then it is not as easy as replacing the source code. However, it is still quite easy to make the upgrade if you just keep a few things in mind. Here’s what you need to know:
If you were previously disabling the chat feature in order to meet a certain parental ratings requirement, then you would be doing so by passing the OpenFeintSettingDisableChat setting into the initializeWithProductKey method. Since chat is not the only user-genereated content in OpenFeint 2.4, this setting name has been changed to OpenFeintSettingDisableUserGeneratedContent. Below is the new way to initialize OpenFeint with chat (and all user-generated content) disabled:
NSDictionary* settings = [NSDictionary dictionaryWithObjectsAndKeys: [NSNumber numberWithInt:UIInterfaceOrientationLandscapeRight], OpenFeintSettingDashboardOrientation, @"Addicus", OpenFeintSettingShortDisplayName, [NSNumber numberWithBool:YES], OpenFeintSettingDisableUserGeneratedContent, nil]; OFDelegatesContainer* delegates = [OFDelegatesContainer containerWithOpenFeintDelegate:self andChallengeDelegate:self andNotificationDelegate:self]; [OpenFeint initializeWithProductKey:@"MY_PRODUCT_KEY" andSecret:@"MY_PRODUCT_SECRET" andDisplayName:@"Addicus" andSettings:settings andDelegates:delegates];
New Library Dependencies
If you replaced OpenFeint 2.3.x in your project with the new 2.4 source, you might have gotten a bunch of nasty errors upon building it. This is because the OpenFeint 2.4 requires more libraries to be added to your target. You need to add the following 3 frameworks to your target to get your OpenFeint 2.4 game to build successfully:
Achievements and Social Networks
In OpenFeint 2.4, unlocking an achievement no longer prompts the user to notify their friends on Twitter and Facebook by default. However, you can restore this behaviour by setting the OpenFeintSettingPromptToPostAchievementUnlock setting to be true at the time of initialization. Below is how to initialize OpenFeint 2.4 with prompts to post achievements to social networks enabled:
NSDictionary* settings = [NSDictionary dictionaryWithObjectsAndKeys: [NSNumber numberWithInt:UIInterfaceOrientationLandscapeRight], OpenFeintSettingDashboardOrientation, @"Addicus", OpenFeintSettingShortDisplayName, [NSNumber numberWithBool:YES], OpenFeintSettingPromptToPostAchievementUnlock, nil]; OFDelegatesContainer* delegates = [OFDelegatesContainer containerWithOpenFeintDelegate:self andChallengeDelegate:self andNotificationDelegate:self]; [OpenFeint initializeWithProductKey:@"MY_PRODUCT_KEY" andSecret:@"MY_PRODUCT_SECRET" andDisplayName:@"Addicus" andSettings:settings andDelegates:delegates];
Splash screens, cutscenes, tutorials or just good old content. There are plenty of reasons to need to play a video in your iPhone app. Here’s how to do it in 3 simple steps:
1. Add the MediaPlayer framework to your target.
2. Import the MediaPlayer header file in the file you intend to begin playing the video in, like so:
3a. Now we can stream a video from the internet like so:
NSURL *url = [NSURL URLWithString:@"http://www.example.com/myvideo.m4v"]; MPMoviePlayerController *player = [[MPMoviePlayerController alloc] initWithContentURL:url]; [player play];
3b. Alternately, we can stream a video that you include in your app bundle. To stream a file called myvideo.m4v, you would run the following code:
NSString *path = [[NSBundle mainBundle] pathForResource:@"myvideo" ofType:@"m4v"]; NSURL *url = [NSURL fileURLWithPath:path]; MPMoviePlayerController *player = [[MPMoviePlayerController alloc] initWithContentURL:url]; [player play];
It’s back to basics with today’s advent tip. I would be surprised if anything short of ALL developers have had a need to format strings at one time or another. iPhone app development is no exception.
String formatting allows you to build a string based on a template and varying types of values. For example, if I am keeping track of a player’s score as an integer and I want to display a string to the player that celebrates their score by saying “You scored 2600! Way to go!”, then I need string formatting to get the correct score in the middle of that string.
How to Do It
Below is an example of how to build the above NSString. This example formats a string with an integer in it using the %i format specifier.
int score = 2600; NSString * scoreString = [NSString stringWithFormat:@"You scored %i! Way to go!",score];
But string formatting doesn’t stop at putting integers in strings. Below are some of the most common format specifiers for other value types:
|%@||Objective-C object (commonly used to specify another NSString)|
iPhone apps have a curved shine applied to their icons by default. It makes any icon look like it has been buffed to a high gloss. But what if you don’t want your icon to look shiny? What if you want full control over the look of your icon? Don’t fret: there are a couple of easy ways to disable the shine and take back control, and they’re both rather easy.
The first method is to add a setting to your project in Xcode. Just follow these 4 simple steps to do so:
1. Open your iPhone app’s info.plist file.
2. Command-click and select Add Row.
3. Select “Icon already includes gloss and bevel effects” from the drop down that appears.
4. Check the checkbox that appears next to the new row.
Alternately, you could set this setting in your info.plist by editing it in a text editor. You just need to add the following 2 XML tags inside the <dict> tag:
<dict> <key>UIPrerenderedIcon</key> <true/> ...
Left idle, the iPhone goes into sleep mode after about a minute. This narcoleptic behaviour saves on battery life, but there are situations where you would not want the device to go to sleep. For instance, in the middle of a multiplayer game that is turn-based, such as chess. You could wait for longer than a minute for your opponent to make make their move and you wouldn’t want to miss it when they did.
To keep the iPhone awake and alert, simply run the following line of code:
[UIApplication sharedApplication].idleTimerDisabled = YES;
Do use this tip sparingly though. The iPhone’s power is unmatched, to be sure, but unfortunately it is not unlimited.
The network activity indicator is like the UIActivityIndicatorView we previously discussed, only it sits on the status bar, it is smaller, and believe it or not, it is even easier to manipulate. It’s the little rotating wheel of bars (pictured right) that shows up on the status bar whenever your iPhone is accessing the network.
If you would like to let your users know that your iPhone app is currently swapping data with the network, you can do so with this simple line of code:
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
You guessed it: once you’re done showing it, to hide it again, just set the same UIApplication property to NO like so.
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
The iPhone is capable of dazzling the user eyes by pushing impressive graphics around its screen and tickling their ears with a lot of audio to go with it, but you may want to give users of your iPhone app some additional sensory experience on top of that. You can do this by making the device vibrate.
We actually use the Audio framework to vibrate device. Here is how to do it in 3 simple steps:
1. Add the AudioToolbox framework to your target.
2. In the file you intend to trigger a vibration, import the AudioToolbox header file:
3. Finally, call the following line to make the device vibrate:
You should be familiar with the application delegate class if you have ever developed any kind of iPhone app. It is the class that contains your app’s applicationDidFinishLaunching which most if not all apps use as an entry point for execution. You might use it to keep track of application-level state variables or objects, for example.
As such, from time to time you will have a need to retrieve your app’s application delegate instance. You can retrieve it from anywhere within your app by running the following single line of code:
MyAppDelegateClass *app = (MyAppDelegateClass *)[[UIApplication sharedApplication] delegate];
Just be sure to replace “MyAppDelegateClass” with your own iPhone app’s application delegate class name.
Have you ever needed to retrieve your iPhone app’s name or version at runtime? Sure, you can use constants in a lot of cases, but it can often save you time in the long run to make use of the information that is available via a class called NSBundle.
You can retrieve your iPhone app’s display name using the following one line of code:
NSString *appName = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleDisplayName"];
To retrieve your iPhone app’s version number, use the following similar line of code:
NSString *appVersion = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleVersion"];
In Addicus, when you click the “Get Set” button on the main menu, it links to our website and passes in the referring application name and app version as querystring parameters, allowing us to track visits to our website from different versions of all of our iPhone games. To do this, we combine the above code with code that opens a URL. Here is the resulting code:
NSString *appName = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleDisplayName"]; NSString *appVersion = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleVersion"]; NSString *url = [NSString stringWithFormat:@"http://getsetgames.com/?r=%@&v=%@",appName,appVersion]; [[UIApplication sharedApplication] openURL:[NSURL URLWithString:[url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]];
Since we’re retrieving this info via the NSBundle class, we never need to change this code when we update the version of the app. The version change propagates down to the URL we open automatically. And this code can be reused in any app without changing it.
With so many awesome games and apps on the iPhone, it is easy to forget that it can actually be used to make phone calls. No, really, it’s true! And as is the case with so many other pieces of functionality, this is easy to accomplish with the iPhone SDK.
Like other phones, the iPhone supports the tel application protocol in URLs. This means that all we need to use is the trusty openURL method of the UIApplication class. We have previously discussed this method when pre-composing emails and, believe it or not, opening a URL.
Here is how to dial a phone number from your iPhone app in one line of code:
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"tel:555-555-5555"]];
Dialing From a Web Page
You can also put links in your web pages that dial a phone number on the iPhone by using the tel protocol like so:
<a href="tel:555-555-5555">Dial 555-555-5555</a>