Satyam
Satyam

Reputation: 15904

iOS - In app purchase - Get products price

In my iOS application, I've 3 products where users can purchase using in-app purchase. From my iOS application, i just want to get the price of all the 3 items that are on the apple server. How can I get only the price without user's intervention. (May be i've to handle in view did load or some method)

Upvotes: 5

Views: 14086

Answers (6)

Stive
Stive

Reputation: 1

For get price of in-app product, you can use my server.
Works only for available apps in the App Store.
Send post request to:

http://195.138.193.194:5003/store/

With body:

{
  "bundleId": "com.example.bundle",
  "products": [
    "com.example.productId1",
    "com.example.productId2",
  ]
}

Upvotes: 0

Roy Sianez
Roy Sianez

Reputation: 111

You can add this function to your class.

func priceOf(product: SKProduct) -> String {
    let numberFormatter = NumberFormatter()
    numberFormatter.formatterBehavior = .behavior10_4
    numberFormatter.numberStyle = .currency
    numberFormatter.locale = product.priceLocale
    return numberFormatter.string(from: product.price)!
}

Then, you can pass it your product!

priceLabel.text = "The price of the item is \(priceOf(product: product))"

Replace product with the name of your SKProduct.

Upvotes: 1

Madasamy
Madasamy

Reputation: 67

Swift 3.x, you will use below

public func productsRequest(_ request: SKProductsRequest, didReceive response: SKProductsResponse) { 
        for product in response.products {
          let price =  product.price
          let currencySymbol = productpriceLocale.currencySymbol
        }
    }

Upvotes: 0

Binladenit Tran
Binladenit Tran

Reputation: 119

-(NSString*)getPrice:(SKProduct*)product{
    NSNumberFormatter * _priceFormatter;

    _priceFormatter = [[NSNumberFormatter alloc] init];
    [_priceFormatter setFormatterBehavior:NSNumberFormatterBehavior10_4];
    [_priceFormatter setNumberStyle:NSNumberFormatterCurrencyStyle];

    [_priceFormatter setLocale:product.priceLocale];
    return [_priceFormatter stringFromNumber:product.price];

}

Upvotes: 2

Sanjay Chaudhry
Sanjay Chaudhry

Reputation: 3181

You don't need any user intervention to get any product info. All you have to do is to send a product request info and implement a callback method that'll handle the response as shown below (the example has been taken from http://troybrant.net/blog/2010/01/in-app-purchases-a-full-walkthrough/):

- (void)requestProUpgradeProductData
{
    NSSet *productIdentifiers = [NSSet setWithObject:@"com.runmonster.runmonsterfree.upgradetopro" ];
    productsRequest = [[SKProductsRequest alloc] initWithProductIdentifiers:productIdentifiers];
    productsRequest.delegate = self;
    [productsRequest start];

    // we will release the request object in the delegate callback
}

#pragma mark -
#pragma mark SKProductsRequestDelegate methods

- (void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response
{
    NSArray *products = response.products;
    proUpgradeProduct = [products count] == 1 ? [[products firstObject] retain] : nil;
    if (proUpgradeProduct)
    {
        NSLog(@"Product title: %@" , proUpgradeProduct.localizedTitle);
        NSLog(@"Product description: %@" , proUpgradeProduct.localizedDescription);
        NSLog(@"Product price: %@" , proUpgradeProduct.price);
        NSLog(@"Product id: %@" , proUpgradeProduct.productIdentifier);
    }

    for (NSString *invalidProductId in response.invalidProductIdentifiers)
    {
        NSLog(@"Invalid product id: %@" , invalidProductId);
    }

    // finally release the reqest we alloc/init’ed in requestProUpgradeProductData
    [productsRequest release];

    [[NSNotificationCenter defaultCenter] postNotificationName:kInAppPurchaseManagerProductsFetchedNotification object:self userInfo:nil];
}

Just call requestProUpgradeProductData from viewDidLoad.

Upvotes: 11

David V
David V

Reputation: 11699

You can create an instance of SKProductsRequest with a list of known identifiers at any time. I have it done when the app is launching. It is an async call using a delegate so it is easy to do without blocking.

myProductsRequest = [SKProductsRequest initWithProductIdentifiers:someSetOfIds];
myProductsRequest.delegate = myDelegageSomewhere;

In your SKProductsRequestDelegate:

- (void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response
{
  for(SKProduct *product in response.products)
  {
     [self doSomethingWithPrice:product.price]
  }
}

The information is tied to the user's iTunes account. That is how it gets the price for different country stores (different currencies).

Upvotes: 6

Related Questions