Skip to content

Commit

Permalink
Merge pull request #3 from codykerns/support-ios-9
Browse files Browse the repository at this point in the history
Support iOS 9
  • Loading branch information
codykerns authored Aug 27, 2020
2 parents d23123e + cfa8575 commit 2f2286c
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 15 deletions.
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import PackageDescription
let package = Package(
name: "PurchasesHelper",
platforms: [
.iOS("11.2"), .watchOS("6.2")
.iOS("9.3"), .watchOS("6.2")
],
products: [
// Products define the executables and libraries produced by a package, and make them visible to other packages.
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,4 +122,4 @@ The available sorting options are:
> Sorts by longest duration -> shortest duration
> **.hasIntroductoryPrice**
> Sorts by packages that have an introductory price (e.g. free trial) first
> Sorts by packages that have an introductory price (e.g. free trial) first. Requires iOS 11.2 minimum.
17 changes: 8 additions & 9 deletions Sources/PurchasesHelper/CompatibilityAccessManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,38 +39,37 @@ public class CompatibilityAccessManager {
/**
Optional configuration call to set entitlement versions as well as restore transactions if a receipt is available. **IMPORTANT**: this method should be called *after* you initialize the Purchases SDK.
*/
public static func configure(entitlements: [BackwardsCompatibilityEntitlement], completion: ((Purchases.PurchaserInfo?) -> Void)? = nil) {
let manager = CompatibilityAccessManager.shared
public func configure(entitlements: [BackwardsCompatibilityEntitlement], completion: ((Purchases.PurchaserInfo?) -> Void)? = nil) {

entitlements.forEach { (entitlement) in
CompatibilityAccessManager.shared.register(entitlement: entitlement)
self.register(entitlement: entitlement)
}

/// If we don't have an originalApplicationVersion in the Purchases SDK, and we have a receipt available, automatically restore transactions to ensure a value for originalApplicationVersion in PurchaserInfo

manager.log("Fetching PurchaserInfo.")
self.log("Fetching PurchaserInfo.")

Purchases.shared.purchaserInfo { (info, error) in
if let originalApplicationVersion = info?.originalApplicationVersionFixed {
manager.log("originalApplicationVersion is \(originalApplicationVersion)")
self.log("originalApplicationVersion is \(originalApplicationVersion)")

completion?(info)
} else {
manager.log("originalApplicationVersion is nil - checking for a receipt..")
self.log("originalApplicationVersion is nil - checking for a receipt..")

if let receiptURL = Bundle.main.appStoreReceiptURL,
let _ = try? Data(contentsOf: receiptURL) {
manager.log("Receipt data found. Syncing with Purchases..")
self.log("Receipt data found. Syncing with Purchases..")

Purchases.shared.restoreTransactions { (info, error) in
if error == nil {
manager.log("Receipt synced.")
self.log("Receipt synced.")
}

completion?(info)
}
} else {
manager.log("No receipt data found.")
self.log("No receipt data found.")

/// No receipt data - restoreTransactions will need to be called manually as it will likely require a sign-in
completion?(nil)
Expand Down
17 changes: 13 additions & 4 deletions Sources/PurchasesHelper/PackageExtensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ public extension Purchases.Package {
}

// - check for an introductory discount for a package
if let intro = self.product.introductoryPrice {
if #available(iOS 11.2, *),
let intro = self.product.introductoryPrice {

// - introductory offers
let introLengthTitle = intro.subscriptionPeriod.periodLengthTitle

Expand Down Expand Up @@ -76,27 +78,34 @@ public extension Array where Element: Purchases.Package {
return self.sorted(by: { $0.packageType.rawValue < $1.packageType.rawValue })
case .hasIntroductoryPrice:
// 3 day trial, yearly -> weekly -> monthly
return self.sorted(by: {
return $0.product.introductoryPrice != nil && $1.product.introductoryPrice == nil
})
if #available(iOS 11.2, *) {
return self.sorted(by: {
return $0.product.introductoryPrice != nil && $1.product.introductoryPrice == nil
})
} else {
return self.sorted(by: .timeAscending)
}
}
}
}

@available(iOS 11.2, *)
fileprivate extension SKProductSubscriptionPeriod {
var periodLengthTitle: String {
let isPlural = numberOfUnits != 1
return "\(numberOfUnits) \(unit.title)\(isPlural ? "s" : "")"
}
}

@available(iOS 11.2, *)
fileprivate extension SKProductDiscount {
func periodLengthTitle(for unit: SKProduct.PeriodUnit) -> String {
let isPlural = numberOfPeriods != 1
return "\(numberOfPeriods) \(unit.title)\(isPlural ? "s" : "")"
}
}

@available(iOS 11.2, *)
fileprivate extension SKProduct.PeriodUnit {
var title: String {
switch self {
Expand Down

0 comments on commit 2f2286c

Please sign in to comment.