So recently I added In-App purchases into one of my games. A pretty frustrating process, but thanks to some of the fantastic guides online I managed to finally get everything working how I wanted. One thing did strike me though, iTunes Connect doesn’t provide the ability to choose the order in which In App purchase products are returned. I was sure hundreds of other developers would have experienced the same issue, but found very little help online, so in the end, I figured it out myself. Here’s how.
- (void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response
Here’s the delegate method which is called when you receive a response from your In App purchase ‘products request’. The object response has a property called products which returns an array of SKProducts. It’s this array that I was using to populate my UITableView with my In App Purchase products. And until now, it’s this array that was sorted alphabetically, rather than how I would like. This is how I fixed it…
NSSortDescriptor *mySortDescriptor = [[NSSortDescriptor alloc] initWithKey:@”price” ascending:YES];
1. Create a sort descriptor which sorts using the key ‘price‘. Price is a property/key of an SKProduct which returns the product’s price.
NSMutableArray *tempArray = [[NSMutableArray alloc] initWithArray:response.products];
2. Create a MUTABLE array from response.products (which is passed to the delegate method).
[tempArray sortUsingDescriptors:[NSArray arrayWithObject:mySortDescriptor]];
3. Sort that array using the sort descriptor previously defined.
self.myArrayOfProducts = tempArray;
4. I choose to store the resulting array in an instance variable in my class (using properties/dot notation), but you can use the mutable array to populate your UITableView directly if you prefer. If so you must do this before you release it.
5. Tidy up memory. Release the sort descriptor and the mutable array.
6. You’re done. Now you should find the array of SKProduct’s is sorted by cost and your In App purchases look great in their new tidy and logical order. Ahhhhh!