我的应用程序的后端需要两件事以及收据验证,以确保用户可以访问订阅。一个subscriptionStartTimestamp和一个subscriptionDuration。我想知道SKProduct是否提供了类似的东西,我偶然发现了SKProduct.subscriptionPeriod,它包含一个numberOfUnits和一个unit。这个单元可以是day、month或其他东西。
然后,我突然想到了另一个问题:“苹果总是认为一个月30天吗?”,“闰年呢?”如何找到SKProduct的subscriptionDuration
发布于 2020-08-04 21:58:58
我找到this了。
根据上面提到的答案,我设计了一个函数,它接受一个SKProduct并返回订阅的持续时间。
private func subscriptionDuration(product: SKProduct) -> TimeInterval {
let currentDate = Date()
guard let subscriptionPeriod = product.subscriptionPeriod else {
return 0
}
let numberOfUnits = subscriptionPeriod.numberOfUnits
let unit = subscriptionPeriod.unit
var calendarComponent: Calendar.Component? = nil
switch unit {
case .day:
calendarComponent = .day
case .month:
calendarComponent = .month
case .week:
calendarComponent = .weekOfYear
case .year:
calendarComponent = .year
default:
break
}
guard let component = calendarComponent else {
return 0
}
let calendar = Calendar(identifier: .iso8601)
guard let newDate = calendar.date(byAdding: component, value: numberOfUnits, to: currentDate) else {
return 0
}
return newDate.timeIntervalSince1970 - currentDate.timeIntervalSince1970
}https://stackoverflow.com/questions/63248523
复制相似问题