Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
725 views
in Technique[技术] by (71.8m points)

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

I want the buttons that you have to tap to buy something to show the price of that.

For example: "5 coins €0,99"

But if I create a UIlabel with exactly that text, Americans will also see the price in € instead of usd.

Now how can I set the price where it adjust to the currency the user lives in? I saw it on some games so I am convinced that this is possible.

thank you!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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)!
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...