问题描述
我想要购买商品时必须点击的按钮来显示商品的价格.
I want the buttons that you have to tap to buy something to show the price of that.
例如:5 个硬币 0.99 欧元"
For example: "5 coins €0,99"
但如果我创建一个 UIlabel 与该文本完全一致,美国人也会看到以欧元而不是美元为单位的价格.
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.
谢谢!
推荐答案
如果购买是通过 Apple App Store(使用 StoreKit 框架)完成的,您需要从 SKProduct 对象中获取价格 + 货币(价格会有所不同).
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/
更新
- 您需要执行加载可用产品的请求
var productID:NSSet = NSSet(object: "product_id_on_itunes_connect");
var productsRequest:SKProductsRequest = SKProductsRequest(productIdentifiers: productID);
productsRequest.delegate = self;
productsRequest.start();
- 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")
}
}
- SKProduct 包含显示本地化价格所需的所有信息,但我建议创建 SKProduct 类别,将价格 + 货币格式设置为用户当前语言环境
import StoreKit
extension SKProduct {
func localizedPrice() -> String {
let formatter = NSNumberFormatter()
formatter.numberStyle = .CurrencyStyle
formatter.locale = self.priceLocale
return formatter.stringFromNumber(self.price)!
}
}
import StoreKit
extension SKProduct {
var localizedPrice: String {
let formatter = NumberFormatter()
formatter.numberStyle = .currency
formatter.locale = priceLocale
return formatter.string(from: price)!
}
}
这篇关于屏幕上显示的应用内购买价格(带货币)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!