#import "Store.h" @interface Store () @property (nonatomic, retain) SKRequest* request; @property (nonatomic, retain) NSArray* products; @property (nonatomic, retain, readonly) NSNumberFormatter* priceFormatter; - (void)dismiss; @end @implementation Store @synthesize request; @synthesize products; @synthesize tableView; @synthesize loadingView; - (NSNumberFormatter*)priceFormatter { if (!priceFormatter) { priceFormatter = [[NSNumberFormatter alloc] init]; priceFormatter.formatterBehavior = NSNumberFormatterBehavior10_4; priceFormatter.numberStyle = NSNumberFormatterCurrencyStyle; } return priceFormatter; } - (void)viewDidLoad { [super viewDidLoad]; self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(dismiss)] autorelease]; self.title = @"Store"; NSArray* a = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"Products" ofType:@"plist"]]; self.request = [[[SKProductsRequest alloc] initWithProductIdentifiers:[NSSet setWithArray:a]] autorelease]; self.request.delegate = self; [self.request start]; } - (void)dealloc { [request cancel]; [request release]; [products release]; [priceFormatter release]; [tableView release]; [loadingView release]; [super dealloc]; } #pragma mark Table view data source methods - (NSInteger)numberOfSectionsInTableView:(UITableView*)tableView { return 1; } - (NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section { return [products count]; } - (UITableViewCell*)tableView:(UITableView*)aTableView cellForRowAtIndexPath:(NSIndexPath*)indexPath { static NSString* CellIdentifier = @"Cell"; UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease]; } SKProduct* product = [products objectAtIndex:indexPath.row]; NSNumberFormatter* pf = self.priceFormatter; pf.locale = product.priceLocale; cell.textLabel.text = product.localizedTitle; cell.detailTextLabel.text = [pf stringFromNumber:product.price]; return cell; } #pragma mark Table view delegate methods - (NSIndexPath*)tableView:(UITableView*)tableView willSelectRowAtIndexPath:(NSIndexPath*)indexPath { return nil; } #pragma mark Products request delegate methods - (void)productsRequest:(SKProductsRequest*)aRequest didReceiveResponse:(SKProductsResponse*)response { self.request = nil; self.products = response.products; [self.tableView reloadData]; self.loadingView.hidden = YES; } #pragma mark Extension methods - (void)dismiss { [self.navigationController.parentViewController dismissModalViewControllerAnimated:YES]; } @end