屏幕上显示的应用内购买价格(含货币) [英] Price of in-app purchases shown on screen(with currency)
问题描述
我希望您点按的按钮可以购买一些东西来显示价格。
例如:5币€0,99
但是,如果我创建一个完全具有该文本的UIlabel,美国人也会看到以欧元而不是美元的价格。
现在,我如何设置适应用户所在货币的价格?
我在一些游戏中看到它,所以我确信这是可能的。
谢谢!
如果通过Apple App Store购买(使用StoreKit框架),您需要从SKProduct对象获得价格+货币(价格会有所不同)。
更新
- 您需要执行加载可用产品的请求
< pre class =lang -c prettyprint-override> var productID:NSSet = NSSet(object:product_id_on_itunes_connect);
var productsRequest:SKProductsRequest = SKProductsRequest(productIdentifiers:productID);
productsRequest.delegate = self;
productsRequest.start();
func productsRequest(请求:SKProductsRequest,didReceiveResponse响应:SKProductsResponse){
println(得到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
扩展名SKProduct {
func localizedPrice() - > String {
let formatter = NSNumberFormatter()
formatter.numberStyle = .CurrencyStyle
formatter.locale = self.priceLocale
return formatter.stringFromNumber(self.price)!
}
}
Swift 4
import StoreKit
扩展名SKProduct {
var localizedPrice :String {
let formatter = NumberFormatter()
formatter.numberStyle = .currency
formatter.locale = priceLocale
return formatter.string(from:price)!
}
}
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!
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
- 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();
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 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)!
}
}
这篇关于屏幕上显示的应用内购买价格(含货币)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!