[objc/swift] SKProduct 현지화된 통화코드로 가격표시하기

천현철·2021년 6월 17일
0

iOS

목록 보기
41/51

objc:

NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
[formatter setFormatterBehavior:NSNumberFormatterBehavior10_4];
[formatter setNumberStyle:NSNumberFormatterCurrencyStyle];
[formatter setLocale:[skProduct priceLocale]];
NSString *strPrice = [formatter stringFromNumber:[skProduct price]];

swift4

func priceStringForProduct(item: SKProduct) -> String? {
    let price = item.price
    if price == NSDecimalNumber(decimal: 0.00) {
        return "GET" //or whatever you like really... maybe 'Free'
    } else {
        let numberFormatter = NumberFormatter()
        let locale = item.priceLocale
        numberFormatter.numberStyle = .currency
        numberFormatter.locale = locale
        return numberFormatter.string(from: price)
    }
}

swift5

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

출처
https://www.hackingwithswift.com/example-code/system/how-to-show-the-price-of-an-skproduct
https://stackoverflow.com/questions/36794489/how-to-get-local-currency-for-skproduct-display-iap-price-in-swift
https://stackoverflow.com/questions/2894321/how-to-access-the-price-of-a-product-in-skpayment

profile
기도하지말고 행동하라

0개의 댓글