Browse Source

Merge pull request #318 from bizz84/feature/localized-introductory-price

Add localizedIntroductoryPrice helper
Andrea Bizzotto 7 years ago
parent
commit
3e175746d5
2 changed files with 16 additions and 3 deletions
  1. 2 1
      CHANGELOG.md
  2. 14 2
      SwiftyStoreKit/SKProduct+LocalizedPrice.swift

+ 2 - 1
CHANGELOG.md

@@ -2,9 +2,10 @@
 
 All notable changes to this project will be documented in this file.
 
-## [0.11.1](https://github.com/bizz84/SwiftyStoreKit/releases/tag/0.11.1) Add `transactionDate` to `PaymentTransaction`
+## [0.11.1](https://github.com/bizz84/SwiftyStoreKit/releases/tag/0.11.1) Add `PaymentTransaction.transactionDate` and  `SKProduct.localizedIntroductoryPrice`
 
 * Add `transactionDate` to `PaymentTransaction` ([#316](https://github.com/bizz84/SwiftyStoreKit/pull/316), see [#312](https://github.com/bizz84/SwiftyStoreKit/issues/312)).
+* Add `localizedIntroductoryPrice` to `SKProduct` ([#318](https://github.com/bizz84/SwiftyStoreKit/pull/318)).
 
 ## [0.11.0](https://github.com/bizz84/SwiftyStoreKit/releases/tag/0.11.0) Add `fetchReceipt` method + update `verifyReceipt` and `ReceiptValidator` protocol
 

+ 14 - 2
SwiftyStoreKit/SKProduct+LocalizedPrice.swift

@@ -28,9 +28,21 @@ import StoreKit
 public extension SKProduct {
 
     public var localizedPrice: String? {
+        return formattedPrice(price: price, locale: priceLocale)
+    }
+
+    @available(iOS 11.2, OSX 10.13.2, tvOS 11.2, *)
+    public var localizedIntroductoryPrice: String? {
+        guard let introductoryPrice = introductoryPrice else {
+            return nil
+        }
+        return formattedPrice(price: introductoryPrice.price, locale: introductoryPrice.priceLocale)
+    }
+
+    private func formattedPrice(price: NSDecimalNumber, locale: Locale) -> String? {
         let numberFormatter = NumberFormatter()
-        numberFormatter.locale = self.priceLocale
+        numberFormatter.locale = locale
         numberFormatter.numberStyle = .currency
-        return numberFormatter.string(from: self.price)
+        return numberFormatter.string(from: price)
     }
 }