aanrv
aanrv

Reputation: 2241

iOS - Display product price in proper currency before it is requested by user?

I have created the UI for a Store in my app where users can purchase in-app products. I also want to display the price of the product next to it without the user having to click purchase, but can't find a proper way to do so.

A question very similar to this one has already been answered here.

But the solution above requires an SKProduct object. This means the user must click the purchase button and the productsRequest:didReceiveResponse: method will be called. Only once the SKProduct object is created can I retrieve information about the product.

I have also read the Apple Documentation and it uses a similar method.

Is there anyway to retrieve product information and price without having the user request a purchase first?

Upvotes: 0

Views: 704

Answers (2)

Indrajeet
Indrajeet

Reputation: 5656

Yes you can do it by making request in appdelegate while launching app.

Let create one method named getInAppProductDetails. Use this method for making request

-(void)getInAppProductDetails
{
    SKProductsRequest *request;
    request = [[SKProductsRequest alloc] initWithProductIdentifiers:[NSSet setWithObjects:
                                                                 @"InAppID1",
                                                                 @"InAppID2,
                                                                 nil]];

    request.delegate = self;
    [request start];
}

Call above method during launch. After that handle the response you got

- (void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response
{
    NSArray *myProducts = response.products;
    NSMutableDictionary *dictMain = [[NSMutableDictionary alloc]init];

    for (int i = 0;i < [myProducts count]; i++)
    {
        SKProduct *product = [myProducts objectAtIndex:i];
        NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];

        [numberFormatter setFormatterBehavior:NSNumberFormatterBehavior10_4];
        [numberFormatter setNumberStyle:NSNumberFormatterCurrencyStyle];
        [numberFormatter setLocale:product.priceLocale];

        NSString *priceString = [numberFormatter stringFromNumber:product.price];
        NSString *strTitle = product.localizedTitle;

        NSLog(@"Title: %@", product.localizedTitle);
        NSLog(@"Desc: %@", product.localizedDescription);
        NSLog(@"Price: %@", priceString);
        NSLog(@"id : %@",product.productIdentifier);

        NSMutableDictionary *dict = [[NSMutableDictionary alloc]init];
        [dict setValue:priceString forKey:@"productPrice"];
        [dict setValue:strTitle forKey:@"prodcuctTitle"];

        [dictMain setObject:dict forKey:product.productIdentifier];
    }
    [[NSUserDefaults standardUserDefaults] setObject:dictMain forKey:@"products"];
}

In this delegate method you get each and every in app details which you stored in userdefaults as a dictionary for later use.

To get price of product from the dictionary, you can do like this

NSString *str1 = [[dictPriceList valueForKey:@"InAppID1"] valueForKey:@"productPrice"];
NSString *str2 = [[dictPriceList valueForKey:@"InAppID2"] valueForKey:@"productPrice"];

Upvotes: 1

DrC
DrC

Reputation: 7698

Slight ordering issue here - you should make the SKProductsRequest before displaying the ability for the user to purchase (the purchase button). That way you have the price and other information to allow the user to make the purchase decision. You will also know if any of your products are invalid or have localized descriptions.

For example, I have a screen that lists all my IAP items. In the initialization phase for that view, I start the SKProductsRequest. I display all the items in the view when the associated delegate is called.

Once you change the flow, then the other solution you referenced will give you formatted pricing and you can also grab the localized descriptions.

Upvotes: 2

Related Questions