Creating an In-App Purchase in iOS 7 – Part 1: Setup

In this 7-part screencast series I’ll show you how to create an In-App Purchase in iOS 7 with Xcode 5.1.

The course will run you through everything from setting up your product in iTunes Connect, creating a custom shop class for easy re-use, making “first contact” with the App Store and how to deal with its responses. I’ll explain the overall concept in Part 1.

I’ll describe the overall concept in this part, and how to setup your your app for use with In-App purchases. We’ll setup a new App ID in Member Center and then create a product in iTunes Connect.

Demo Project

You can download the full source code of what I’m building in this screencast on GitHub:

Further Reading

I’m following my earlier two articles almost to the letter, here they are for reference:

Watch the full course in one convenient playlist:
Catch this episode on my iOS Dev Diary Podcast:

Where is the Documents Directory for the iOS 8 Simulator

xcode-6-iconYou may have noticed that the iPhone Simulator has changed with Xcode 6, and with it – of course – the path to your simulated apps’ Documents Directory. At times we may need to take a look at it.

Finding that path is not as easy as it was once, namely Library/Application Support/iPhone Simulator/7.1/Applications/ followed by a cryptic number representing your app.

As of Xcode 6 and iOS 8 you’ll find it here:
Library/Developer/CoreSimulator/Devices/cryptic number/data/Containers/Data/Application/cryptic number

It was fairly easy to spot that one cryptic number before – but now you have to play the guessing game twice. This can become tedious, especially if you’re testing several user accounts and therefore accumulate a vast array of cryptic numbers.

Here’s a great way of writing out the Documents directory as a log message:

Add this to a viewDidLoad or applicationDidFinishLaunching method and you’re golden. If you’re running the app on a Simulator, this will write out the correct location for the current app.

Thanks to Ian for this tip!

Worse yet: every time you run the app, the folder location changes!

Thanks to Keith for bringing this up:

The location of this folder changes every time you run the app on the Simulator. Only Apple know why.

The good news is that once you open a Finder window and navigate to your project, Finder will update the location automatically as long as you don’t close that window.

At least this way you don’t have to play detective every time you launch the app from Xcode.

Complex and Cumbersome Method

Originally I did the following to detect where they Documents directory was:

  • add an identifiable file to your bundle (for example, a picture)
  • copy that file into your app’s Documents directory, using a name you can remember
  • run the app on the simulator
  • search for the file on the command line

Here’s how I did this:

This will copy a hypothetical file “swift-icon.png” from the Main Bundle to the simulator’s Documents directory as “WhereAreYouHiding-4s”. I’m adding 4s here because I’d like to find that particular simulator’s directory – which is likely different for the other models.

Searching for this file in Finder won’t yield any results – because a standard user does not have the permissions to read the simulator’s directory. Hence we’ll open Terminal and look for our file with root privileges:

This will prompt you to type in your Administrator password for your Mac and will go to work. The find command will look through every file on your hard disk and should only come up with very few results. Once you’ve found something resembling that cryptic path I’ve mentioned above, you can stop the search with CTRL+C.

How to use Popovers on iPad – Part 3: Image Picker

In this series I’ll show you how to create Popovers on iPad. They’re fairly easy to create once you get the hang of the inner workings of the UIPopoverController.

I’ll show you how to create basic Popover in code and in your Storyboard, and we’ll discuss how you can retrieve data from a Popover when it’s dismissed. We’ll do this with a simple UIDatePicker. In the last video I’ll demonstrate how you can pick images from the camera roll using the UImagePickerController with a Popover – which is how you’re meant to do it on iPad.

The series contains three videos in total:

  • Part 1 deals with the initial code and class etup
  • Part 2 deals with setting up the UIStoryboard and connect our code to it
  • and in Part 3 we’ll implement a UIImagePickerController to demonstrate a use case

Enjoy!

Get the code for this project on GitHub

Watch the full course in one convenient playlist:
Catch this episode on my iOS Dev Diary Podcast:

How to use Popovers on iPad – Part 2: Storyboard

In this series I’ll show you how to create Popovers on iPad. They’re fairly easy to create once you get the hang of the inner workings of the UIPopoverController.

I’ll show you how to create basic Popover in code and in your Storyboard, and we’ll discuss how you can retrieve data from a Popover when it’s dismissed. We’ll do this with a simple UIDatePicker. In the last video I’ll demonstrate how you can pick images from the camera roll using the UImagePickerController with a Popover – which is how you’re meant to do it on iPad.

The series contains three videos in total:

  • Part 1 deals with the initial code and class etup
  • Part 2 deals with setting up the UIStoryboard and connect our code to it
  • and in Part 3 we’ll implement a UIImagePickerController to demonstrate a use case

Enjoy!

Get the code for this project on GitHub

How to test the existence of a file with NSFileManager

Here’s how you can check if a file exists in your app’s Documents directory:

This example relies on another method which is usually part of the AppDelegate.m file called applicationDocumentsDirectory. It will return an NSURL to your Documents directory. Just in case you’re working on a project which does not include it, here it is:

How to migrate your persistent store from one location to another

Sometimes you need to change your NSPersistentStore file from one location to another. Or you want to add other options that the old store didn’t have (for example, add iCloud or turn an XML store into an SQLite store). In those cases you can migrate the current store.

Typically this is done by leaving the existing store as is, creating a new store with new properties. Then we’ll tell the NSPersistentStoreCoordinator about our plan, and he’ll swap out the store for us.

Here’s an example: I’ve created a Master/Detail Test App with Core Data and added a few records. All looks good. My persistent store was created by default like this – as provided by the template in AppDelegate.m:

Now I’d like to change the location of the store to a new URL. While I’m at it I could add a new options dictionary – but I won’t show that here, because I’m pretty sure you know how that works 😉

Here’s a method I’m using for the migration:

You can check if the migration has worked by writing out the URL before and after the migration. At this point the old store is no longer in use. If you’d like to go back it it at any time, simply perform another migration.

Demo Project

I’ve added a demo project on GitHub which demonstrates this: