xcode 屏幕上显示的应用内购买价格(带货币)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28905970/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Price of in-app purchases shown on screen(with currency)
提问by sdd
I want the buttons that you have to tap to buy something to show the price of that.
我想要你必须点击的按钮才能买东西来显示它的价格。
For example: "5 coins 0,99"
例如:“5 个硬币 0,99”
But if I create a UIlabel with exactly that text, Americans will also see the price in instead of usd.
但如果我创建一个 UIlabel 与该文本完全一致,美国人也会看到价格 in 而不是 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!
谢谢你!
回答by f3n1kc
If purchases are done via Apple App Store (using StoreKit framework) you need to get price + currency from SKProduct object (prices will vary).
如果购买是通过 Apple App Store(使用 StoreKit 框架)完成的,您需要从 SKProduct 对象获取价格 + 货币(价格会有所不同)。
https://developer.apple.com/library/ios/documentation/StoreKit/Reference/SKProduct_Reference/
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
- SKProduct 包含显示本地化价格所需的所有信息,但我建议创建 SKProduct 类别,将价格 + 货币格式化为用户当前语言环境
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 hereand here.
Swift 4
斯威夫特 4
import StoreKit
extension SKProduct {
var localizedPrice: String {
let formatter = NumberFormatter()
formatter.numberStyle = .currency
formatter.locale = priceLocale
return formatter.string(from: price)!
}
}
回答by lchamp
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), ...
- 你的故事板(你仍然会处理一个,但在内部文件中翻译你想要的文本)
- 在你的代码里面。低谷
NSLocalizedString
例如:http: //goo.gl/jwQ5Po(Apple), http://goo.gl/S1dCUW(NSHipster), ...