In App Purchase “Gotchas”

In App Purchase is a terrific addition to the iPhone programmers box of tools.  It’s a very versatile tool that developers can use to diversify ways of earning income from iPhone apps.  Unfortunately, like app provisioning, when in-app purchase fails, it doesn’t give you a lot of details about why.  This has led to a lot of misinformation floating around the net about various forms of voodoo developers need to perform to get their code working.  Apple has a great tutorial about how to implement In App Purchase, so I won’t be going through the entire process, but I would like to highlight a few “gotchas” that seem to trip people up.  Here we go.

  1. It probably won’t work right away. – This is the big one.  It apparently takes up to 24 hours for a new product to propagate through Apple’s servers.  Until that time your productRequest will not return a product.  If you suspect this is the case, check the SKProductResponse object’s invalidProductIdentifiers array.  If it contains a productID identical to the one in your iTunes Connect profile then you’re doing things right.  You just need to wait a little longer and try again.
  2. You can debug with your usual provisioning profile. – Although all the information on Provisioning Profiles warns that you cannot enable In App Purchase in a profile that uses a wildcard in its name, this is not the case for development.  You can successfully run your Store Kit code in Debug Configuration using any provisioning profile that works for your app.
  3. You must mark your product “Cleared for Sale” to test. – It seems like a step you shouldn’t take until your testing is done, but you must check the “Cleared for Sale” box in iTunes Connect to be able to test the product.
  4. You must run it on the device, not the simulator. – This one isn’t too hard to figure out, it actually gives you a clear error, but it’s good to know ahead of time.  StoreKit code will not work in the simulator.

Hope you have a good experience implementing In App Purchase.  Send me an email with further questions if things aren’t clear.

Cheers.

BrewTour CO 1.0 Now Available!

Colorado local beer-lovers rejoice! Version one of BrewTour CO has hit the App Store and is free to download. Look ahead shortly for new features such as the ability to input beers you have tried and rate your impressions.

Cheers!

Adding a Segmented Control to UIToolbar

This is a pretty simple bit of UI programming, but I found quite a bit of confusion about it online when I was looking for clues about how to do it, including some assertions that it couldn’t be done.  So I thought I’d run through it in case more people come around searching for this technique.

Step 1:  Create the UISegmentedControl

The segmented control requires an array with the titles or images appearing in its segments, so create this first.

NSArray *segments = [NSArray arrayWithObjects:@"First", @"Second",
@"Third", nil];

Then initialize the UISegmentedControl, passing it the array.  While you’re here, you might as well supply the control with its target information.

UISegmentedControl *segmentedCtrl = [[UISegmentedControl alloc]
initWithItems:segments];
[segmentedCtrl addTarget:self action:@selector(segmentAction)
forControlEvents:UIControlEventValueChanged];

Step 2: Put the Segment Control in a UIBarButtonItem

Now we’re going to create a UIBarButtonItem to stick on the toolbar.  Using the right method here is key.  Rather than initializing the button with a title or image, use the initWithCustomView method and pass in the UISegmentedControl as the method parameter.

UIBarButtonItem *segmentButton = [[UIBarButtonItem alloc]
initWithCustomView:segmentedCtrl];

You don’t need to specify target information for the UIBarButtonItem because we already did this when we created the Segmented Controller.  Create any other toolbar items, including flexible space, you will be using here too so they can all be added to the toolbar at the same time.

Step 3:  Add UIBarButtonItems to the UIToolBar

You add toolbar items to the toolbar by putting them in an array in the order you want them, and then assigning that array to the toolbarItems property of your view controller.  Assuming you are executing this in a UIViewController subclass, your code will look like this:

NSArray *myToolbarItems = [NSArray arrayWithObjects:segmentButton,
nil];
self.toolbarItems = myToolbarItems;

Bake at 400° for 45 minutes, and it should come out looking like this

Some Additional Notes

The segmented controller will call its action method anytime any of its segments is tapped.  To determine which segment is selected, check its selectedSegmentIndex property.  You can also set this property during setup to cause one of the segments to be preselected when the controller appears.

360 iDev

I’d like to welcome all the new friends I’ve made so far at 360 iDev to Giant Robot Pilot.  You all are a talented bunch of guys and gals.  I’m glad those of you from out of town are enjoying Denver so much.  Please subscribe to the site to get updates of product releases, iPhone development musings, and other info.  You can also follow me on Twitter.