Price of in-app purchases shown on screen(with currency)

孤街浪徒 提交于 2019-11-29 01:57:45
f3n1kc

If purchases are done via Apple App Store (using StoreKit framework) you need to get price + currency from SKProduct object (prices will vary).

https://developer.apple.com/library/ios/documentation/StoreKit/Reference/SKProduct_Reference/

Update

  1. you need to perform request to load available products
var productID:NSSet = NSSet(object: “product_id_on_itunes_connect”);
var productsRequest:SKProductsRequest = SKProductsRequest(productIdentifiers: productID);
productsRequest.delegate = self;
productsRequest.start();
  1. Request delegate will return SKProduct.
func productsRequest (request: SKProductsRequest, didReceiveResponse response: SKProductsResponse) {
    println("got the request from Apple")
    var validProducts = response.products
    if !validProducts.isEmpty {
        var validProduct: SKProduct = response.products[0] as SKProduct
        if (validProduct.productIdentifier == self.product_id) {
            println(validProduct.localizedTitle)
            println(validProduct.localizedDescription)
            println(validProduct.price)
            buyProduct(validProduct);
        } else {
            println(validProduct.productIdentifier)
        }
    } else {
        println("nothing")
    }
}
  1. SKProduct contains all needed information to display localized price, but I suggest to create SKProduct category that formats price + currency to user current locale
import StoreKit

extension SKProduct {

    func localizedPrice() -> String {
        let formatter = NSNumberFormatter()
        formatter.numberStyle = .CurrencyStyle
        formatter.locale = self.priceLocale
        return formatter.stringFromNumber(self.price)!
    }

}

Information taken from here and here.

Swift 4

import StoreKit

extension SKProduct {
    var localizedPrice: String {
        let formatter = NumberFormatter()
        formatter.numberStyle = .currency
        formatter.locale = priceLocale
        return formatter.string(from: price)!
    }
}

You might want to to localize (internationalize) your interface and texts.

In order to do that you'll have look how to do it for :

  • Your storyboard (you'll still handle one, but translate the texts you want in an inside file)
  • Inside your code. Trough NSLocalizedString for example : http://goo.gl/jwQ5Po (Apple), http://goo.gl/S1dCUW (NSHipster), ...
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!