2

I am new on in-app purchase. How can I get subscriptionPeriod below iOS 11.2? As I see it is available from iOS 11.2 in SKProduct class. My code is like this:

var durationNumberString: String? {
    if #available(iOS 11.2, *) {
        guard let nu = product.subscriptionPeriod?.numberOfUnits else { return nil }
        return "\(nu)"
    } else {
        return nil
    }
}

var durationUnitString: String? {
    if #available(iOS 11.2, *) {
        guard let unit = product.subscriptionPeriod?.unit else { return nil }
        
        switch unit {
        case .day:
            return "day"
        case .week:
            return "weak"
        case .month:
            return "month"
        case .year:
            return "year"
        @unknown default:
            return ""
        }
    } else {
        return nil
    }
}

I want to give durationNumberString and durationUnitString values below iOS 11.2 also.

Shohin
  • 519
  • 8
  • 11
  • firstly show your code and your effort please. Read stackoverflow rules. By the way, you can look this one. https://www.appcoda.com/in-app-purchases-guide/ – Emre Gürses Apr 10 '21 at 18:43

1 Answers1

-1

I have found the solution. Before iOS 11.2 I can get durationNumberString and durationUnitString by hardcoding, using product identifier.

var durationNumberString: String? {
    if #available(iOS 11.2, *) {
        guard let nu = product.subscriptionPeriod?.numberOfUnits else { return nil }
        return "\(nu)"
    } else {
        switch product.productIdentifier {
        case "id_199_month":
            return "1"
        case "id_499_3month":
            return "3"
        case "id_2099_annual":
            return "1"
        default:
            return nil
        }
    }
}

var durationUnitString: String? {
    if #available(iOS 11.2, *) {
        guard let unit = product.subscriptionPeriod?.unit else { return nil }
        
        switch unit {
        case .day:
            return "day"
        case .week:
            return "weak"
        case .month:
            return "month"
        case .year:
            return "year"
        @unknown default:
            return ""
        }
    } else {
        switch product.productIdentifier {
        case "id_499_3month",
             "id_199_month":
            return "month"
        case "id_2099_annual":
            return "year"
        default:
            return nil
        }
    }
}
Shohin
  • 519
  • 8
  • 11
  • What you are doing is using the name of the product identifier, however this does not give you the period or whether the product subscription is has not been cancelled. – NSCoder Nov 12 '22 at 14:18