Przeglądaj źródła

Add localizedIntroductoryPrice helper

Andrea Bizzotto 7 lat temu
rodzic
commit
2f0075e28b
1 zmienionych plików z 14 dodań i 2 usunięć
  1. 14 2
      SwiftyStoreKit/SKProduct+LocalizedPrice.swift

+ 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, *)
+    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)
     }
 }