Reputation: 687
I have two View Controllers. One of them shows the available In App Purchases. When an user selects one of them it presents a new View Controllers containing the details of the Purchase and the "Buy" button.
The way I pass my code from the first view to the second is the following:
-(IBAction)purchase1:(id)sender{
_purchasedController = [[iPadPurchasedViewController alloc] initWithNibName:nil bundle:nil];
_purchasedController.productID = @"xxxxx.xxxxx.xxxxx";
[self presentViewController:_purchasedController animated:YES completion:NULL];
[self.purchasedController getProductID:self];
}
So when a user selects one button I pass the productID string to the second ViewController. However, I wanted to present the price to the user on the first View Controller. Can somebody help me with that? Thank you.
Upvotes: 0
Views: 254
Reputation: 2085
Edited answer to be more complete:
In your ViewController.h file there should be
@interface ViewController : UIViewController <SKProductsRequestDelegate> {
NSArray *allProducts;
}
Then in ViewController.m eg. in viewDidload
:
-(void) viewDidload {
[super viewDidLoad];
SKProductsRequest *request = [[SKProductsRequest alloc] initWithProductIdentifiers:[NSSet setWithObjects:@"xxxx1",@"xxxx2",nil]];
request.delegate=self;
[request start];
}
-(void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response{
allProducts = response.products;
NSLog(@"Fetched %d products", allProducts.count);
}
And then it can be used as such:
-(NSString*) getPriceForProductWithID:(NSString*)productID{
if(allProducts.count < 1) {
NSLog(@"Did not fetch products yet");
}
else {
NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
numberFormatter setFormatterBehavior:NSNumberFormatterBehavior10_4];
[numberFormatter setNumberStyle:NSNumberFormatterCurrencyStyle];
for(SKProduct * product in allProducts) {
if([product.productIdentifier isEqualToString:productID])
{
[numberFormatter setLocale:product.priceLocale];
NSString *priceString= [numberFormatter stringFromNumber:product.price];
NSLog(@"Product with ID: %@ has price: %@",product.productIdentifier, priceString);
return priceString;
}
}
}
return nil;
}
Upvotes: 1
Reputation: 2332
Get products:
SKProductsRequest *request = [[SKProductsRequest alloc] initWithProductIdentifiers:[NSSet setWithObject:@"xxxxx"]];
If it's going to be success, the following delegate will be called:
- (void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response {
if (response.products == nil) {
NSLog(@"No product belonging to the app!");
}
if ([response.invalidProductIdentifiers count] != 0) {
NSLog(@"The following product indentifiers are invalid: %@",response.invalidProductIdentifiers);
}
for (SKProduct* prod in response.products) {
NSLog(@"Proce: %@",prod.price);
}
I hope this hepls for you.
Important:The code is untested, I just wrote it right now...
Good luck!
Upvotes: 1