How to Get the Current Time with NSDate

Dec 7, 2009   //   by Derek van Vliet   //   Development  //  No Comments

In iPhone app development, timing is everything. Being able to take a pulse on the current time is crucial to many apps, especially games. Fortunately, this can be done with as much ease as a developer would expect using the NSDate class in the iPhone SDK. Today’s iPhone development advent tip is another one-line wonder:

NSTimeInterval time = [[NSDate date] timeIntervalSince1970];

The timeIntervalSince1970 method returns the number of seconds since epoch. The return value type, NSTimeInterval, is a typedef of double, so you will have the necessary accuracy to get the current milliseconds from the first 3 digits after the decimal if need be.

How to Open the Mail App With a Pre-Composed HTML Email

Dec 6, 2009   //   by Derek van Vliet   //   Development  //  3 Comments

photo 2-1Have you ever wanted to open the Mail app with a pre-composed email? Then today’s iPhone development advent tip is the one for you! This has many uses, but a common one is a tell-a-friend feature in apps. If you attach the code from this tip to a button, you can give your users a way to promote your app to their friends via the iPhone’s built in Mail app.

A Handy Function

The sendEmailTo function, seen below, can be included in any class in your app. This function gives you a simple interface to send emails. It allows you to define “to” addresses, “cc” addresses, “bcc” addresses, a subject and a body. The body supports HTML tags.

- (void)sendEmailTo:(NSString*)to withCC:(NSString*)cc withBCC:(NSString*)bcc withSubject:(NSString*)subject withBody:(NSString*)body {
	NSString * url = [NSString stringWithFormat:@"mailto:?to=%@&cc=%@&bcc=%@&subject=%@&body=%@",
					  [to stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding],
					  [cc stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding],
					  [bcc stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding],
					  [subject stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding],
					  [body stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding]];
 
	[[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]];
}

NOTE: Running this function in the iPhone Simulator will have no effect because the simulator does not include the iPhone’s Mail app. To see the effects of this function, it must be run on a device.

Example Usage

Once you have that message defined in one of your classes, you can open the Mail app with a pre-composed email with just one message call. The following example opens an email and specifies a to, cc and bcc address. It populates the subject with “Addicus” and populates the body with a short message about Addicus that includes a link and an image (which is also a link). As you can see, this is done with good old HTML tags. This results in the email in the above screenshot.

[self sendEmailTo:@"mom@example.com"
           withCC:@"dad@example.com"
          withBCC:@"asecretfriend@example.com"
      withSubject:@"Addicus"
         withBody:@"<p>Check out this game, <a href='http://addicusgame.com'>Addicus</a>, for the iPhone and iPod Touch. It's super-addictive!</p><p><a href='http://addicusgame.com'><img src='http://getsetgames.com/wp-content/uploads/2009/11/icon_addicus.png'/></a></p>"];

If you would like to have the user specify the to, cc and bcc addresses themselves, as you would in the case of a tell-a-friend button, then you would call the following message:

[self sendEmailTo:@""
           withCC:@""
          withBCC:@""
      withSubject:@"Addicus"
         withBody:@"<p>Check out this game, <a href='http://addicusgame.com'>Addicus</a>, for the iPhone and iPod Touch. It's super-addictive!</p><p><a href='http://addicusgame.com'><img src='http://getsetgames.com/wp-content/uploads/2009/11/icon_addicus.png'/></a></p>"];

How to Load a UIImage From a URL

Dec 5, 2009   //   by Derek van Vliet   //   Development  //  1 Comment

Early on in development of Addicus, we discovered how easy it was to load images from the web. This was a huge boon to us since it meant our artists could modify assets on our server and they would update live in the test builds, without having to make a new build.

This all hinged on the ability to load a UIImage from a URL. And as it happens, this can be accomplished in as little as one line of code:

UIImage * img = [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://example.com/image.png"]]];

This eventually led to us writing a resource manager singleton class that handles all of our resource loading and can be toggled between local and web mode.

Addicus in the Spotlight on OpenFeint’s Shiny New Site

Dec 5, 2009   //   by Derek van Vliet   //   In the Media  //  No Comments

Screen shot 2009-12-04 at 11.50.29 PM

iPhone social game network OpenFeint launched version 0.1 of their new website today. The new site’s slick design is made even more beautiful by the loveable little shrooms of Addicus. For you see, Addicus is one of the games that OpenFeint has selected to feature in their spotlight.

We have so many good things to say about OpenFeint that I wouldn’t know where to begin. From the easy integration to the feature-filled product to the stellar level of support, they have all the bases covered. You can expect many more OpenFeint-enabled games from Get Set Games.

Addicus Free Now Available on the iPhone App Store – For Free!

Dec 4, 2009   //   by Derek van Vliet   //   Announcements  //  No Comments

Free_shroom
We are delighted to announce that the free version of Addicus, called Addicus Free, is now available for download in the App Store. Addicus Free will give you a taste of the world of number-mushroom-adding fun, but without all the hassle of having to pay for it.

Like the full version, it has all of these heart-warming features:

  • Dozens of secret bonuses!
  • Multipliers and chain combos!
  • 70 unlockable achievements worth 1,000 Feint score!
  • Overdrive mode for ridiculously high scores!
  • Endless replayability!
  • Part of OpenFeint, the largest iPhone game community!
  • Integrated with Facebook and Twitter!

App_Store_Badge_100x200

Addicus 1.1 Update Now Available with 70 OpenFeint Achievements!

Dec 4, 2009   //   by Derek van Vliet   //   Announcements  //  No Comments

Update_shroom
Addicus version 1.1 is now available on the iPhone App Store! It has all the number-popping fun you’ve been enjoying, plus these new and wonderful additions:

  • added 70 unlockable achievements worth 1,000 Feint Score
  • option to publish to Twitter and Facebook
  • updated to OpenFeint 2.3.1
  • user interface improvements
  • minor bug fixes

Stay tuned for more totally awesome updates to Addicus!

App_Store_Badge_100x200

Eliminating class dependencies on your application delegate

Dec 4, 2009   //   by Rob Segal   //   Development  //  No Comments

Ever have the need to call a method inside your app delegate from an arbitrary place in your code?  We’ve had to deal with this issue in a few cases working on our new project.  We’re making use of a UIScrollView instance to give us that popular flick scroll movement that is so popular on the iPhone.  We’ve found that alot of people have been interested in doing the same thing and you can read our blog on that particular challenge here.  We have a UIScrollView instance sitting inside our app delegate and there are certain parts of our code where we need to move that UIScrollView among different layers in Cocos 2D.  To do that we need access to the scroll view instance itself.  That results in code like the following…

myAppDelegate *app = [[UIApplication sharedApplication] delegate];
UIScrollView *appOverlayScrollView  = (UIScrollView *)app.view;
[[app window] sendSubviewToBack:appOverlayScrollView];

This creates a messy dependency between an arbitrary class and the app delegate class.  Wouldn’t it be great if we could send a message to the app delegate and tell it to move the scroll view to the front or back maintaining encapsulation?  Turns out you can indeed do this.  The UIApplication class contains a method called sendAction which is normally used to send an action message identified by a selector to a specified target.  This method can easily be used to call selectors in any object so the above code can be converted to…

[[UIApplication sharedApplication]
sendAction:@selector(bringScrollViewToFront)
to:[[UIApplication sharedApplication] delegate]
from:self
forEvent:nil];

The only downside to this approach is that I haven’t found a way to supply arguments along with the selector so you need to be calling a method with no arguments.  If you know of a way around this issue please do let me know.

How to Display an Activity Indicator with UIActivityIndicatorView

Dec 3, 2009   //   by Derek van Vliet   //   Development  //  3 Comments

We all wish that our apps could run without latency and pauses, but the reality is loading screens are needed from time to time. Today’s advent tip is how to display an activity indicator in your app like the one seen below. It’s great for soothing the savage, impatient user.

ActivityIndicator

It can actually be done with just a few lines of code. First, we create and position the UIActivityIndicatorView by doing the following. It needs to be added to a view, so be sure to replace “myView” with your own view.

UIActivityIndicatorView *activityView = [[[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge] autorelease];
[myView addSubview: activityView];
activityView.center = CGPointMake(240,160);

So now the activity indicator is created and positioned but it isn’t visible yet. You can make it visible at any time by running the following one line:

[activityView startAnimating];

Finally, once you’ve finished loading the copious amounts of awesomesauce that fuel your app, you can make the activity indicator go away by running the following solitary line of code:

[activityView stopAnimating];

Addicus Reviewed by appSafari

Dec 3, 2009   //   by Derek van Vliet   //   In the Media  //  No Comments

Screen shot 2009-12-03 at 2.39.06 AMThe reviews are pouring in one after the other and we are loving what people have to say about Addicus. This latest one is by appSafari.com. Money quote:

The graphics are not only cute (everything bounces), but eye-catching. The music and sound effects are also cute, but if you’re the type who needs silence to do math, you might want to turn them off. Addicus makes math fun–which hasn’t happened since maybe second grade–and it’s bouncy, colorful, and challenging, so it’s well worth a download. It’ll come in handy the next time a dinner bill needs to be split.

Read the full review here

How to Show an Alert with UIAlertView

Dec 2, 2009   //   by Derek van Vliet   //   Development  //  6 Comments

2

Showing an alert message to the user isn’t just a handy interface lick, it’s also an invaluable debugging tool. Thankfully, this functionality is built into the iPhone SDK.

To pop an alert with a title, message and OK button, we use the UIAlertView class like so:

UIAlertView *alert = [[UIAlertView alloc]
			initWithTitle: @"Announcement"
			message: @"It turns out that you are playing Addicus!"
			delegate: nil
			cancelButtonTitle:@"OK"
			otherButtonTitles:nil];
[alert show];
[alert release];

But what if you want to have more than one button on the alert and collect user input from it? Well that is pretty simple too. To do that, you first need to create a class that employs the UIAlertViewDelegate delegate like so:

@interface MyClass : NSObject <UIAlertViewDelegate>

Then, in your class, you need to override the alertView method in order to receive input like so:

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
	if (buttonIndex == 0) {
		NSLog(@"user pressed OK");
	}
	else {
		NSLog(@"user pressed Cancel");
	}
}

Finally, we create the UIAlertView and pass in the delegate. Also note that we’ve labeled the Cancel button “Cancel” and added the “OK” button to the other button titles, which differs slightly from the above example.

UIAlertView *alert = [[UIAlertView alloc]
			initWithTitle: @"Announcement"
			message: @"It turns out that you are playing Addicus!"
			delegate: MY_DELEGATE
			cancelButtonTitle:@"Cancel"
			otherButtonTitles:@"OK",nil];
[alert show];
[alert release];

Our Games

Latest Tweets