23

我正在通过订阅进行应用内购买。在 swift 中,您可以像这样从 SKProduct 获取价格和价格区域设置:

weeklyProduct.price.doubleValue  
weeklyProduct.priceLocale.currencySymbol

其中weeklyProduct 是一个SKProduct。

是否可以获得免费试用期?例如,我为该产品指定了两周的免费试用期。我可以从 SKProduct 得到这个吗?

4

11 回答 11

23

我已经使用 DateComponentsFormatter 解决了这个问题,这可以为您节省大量使用不同语言进行本地化和处理复数等等的时间。这可能看起来像很多代码,但我希望它会在未来节省我的时间。

import Foundation

class PeriodFormatter {
    static var componentFormatter: DateComponentsFormatter {
        let formatter = DateComponentsFormatter()
        formatter.maximumUnitCount = 1
        formatter.unitsStyle = .full
        formatter.zeroFormattingBehavior = .dropAll
        return formatter
    }

    static func format(unit: NSCalendar.Unit, numberOfUnits: Int) -> String? {
        var dateComponents = DateComponents()
        dateComponents.calendar = Calendar.current
        componentFormatter.allowedUnits = [unit]
        switch unit {
        case .day:
            dateComponents.setValue(numberOfUnits, for: .day)
        case .weekOfMonth:
            dateComponents.setValue(numberOfUnits, for: .weekOfMonth)
        case .month:
            dateComponents.setValue(numberOfUnits, for: .month)
        case .year:
            dateComponents.setValue(numberOfUnits, for: .year)
        default:
            return nil
        }

        return componentFormatter.string(from: dateComponents)
    }
}

它需要将 SKProduct 周期单位转换为 NSCalendarUnit

import StoreKit

@available(iOS 11.2, *)
extension SKProduct.PeriodUnit {
    func toCalendarUnit() -> NSCalendar.Unit {
        switch self {
        case .day:
            return .day
        case .month:
            return .month
        case .week:
            return .weekOfMonth
        case .year:
            return .year
        @unknown default:
            debugPrint("Unknown period unit")
        }
        return .day
    }
}

您可以像这样从 SubscriptionPeriod 调用它:

import StoreKit

@available(iOS 11.2, *)
extension SKProductSubscriptionPeriod {
    func localizedPeriod() -> String? {
        return PeriodFormatter.format(unit: unit.toCalendarUnit(), numberOfUnits: numberOfUnits)
    }
}

您可以像这样从 SKProductDiscount 依次调用。请注意,我暂时没有实现其他 PaymentModes。

import StoreKit

@available(iOS 11.2, *)
extension SKProductDiscount {
    func localizedDiscount() -> String? {
        switch paymentMode {
        case PaymentMode.freeTrial:
            return "Free trial for \(subscriptionPeriod.localizedPeriod() ?? "a period")"
        default:
            return nil
        }
    }
}
于 2019-07-03T12:20:52.583 回答
19

您可以获取它,但如上所述,它仅从 iOS 11.2 开始有效,对于其他版本,您必须通过 API 从您的服务器获取它。

这是我使用的示例代码:

if #available(iOS 11.2, *) {
  if let period = prod.introductoryPrice?.subscriptionPeriod {
     print("Start your \(period.numberOfUnits) \(unitName(unitRawValue: period.unit.rawValue)) free trial")
  }
} else {
  // Fallback on earlier versions
  // Get it from your server via API
}

func unitName(unitRawValue:UInt) -> String {
    switch unitRawValue {
    case 0: return "days"
    case 1: return "weeks"
    case 2: return "months"
    case 3: return "years"
    default: return ""
    }
}
于 2018-05-30T12:21:08.190 回答
12

Eslam 的回答为灵感,我创建了 SKProduct.PeriodUnit 的扩展

extension SKProduct.PeriodUnit {
    func description(capitalizeFirstLetter: Bool = false, numberOfUnits: Int? = nil) -> String {
        let period:String = {
            switch self {
            case .day: return "day"
            case .week: return "week"
            case .month: return "month"
            case .year: return "year"
            }
        }()

        var numUnits = ""
        var plural = ""
        if let numberOfUnits = numberOfUnits {
            numUnits = "\(numberOfUnits) " // Add space for formatting
            plural = numberOfUnits > 1 ? "s" : ""
        }
        return "\(numUnits)\(capitalizeFirstLetter ? period.capitalized : period)\(plural)"
    }
}

要使用:

if #available(iOS 11.2, *),
    let period = prod?.introductoryPrice?.subscriptionPeriod
{
    let desc = period.unit.description(capitalizeFirstLetter: true, numberOfUnits: period.numberOfUnits)
} else {
    // Fallback
}

这将创建一个格式良好的字符串(例如 1 天、1 周、2 个月、2 年)

于 2018-08-02T15:55:44.040 回答
2

不错的@scott Wood。我会把它变成一个属性SKProduct.PeriodUnit而不是一个函数。这将使行为与枚举更加一致:

@available(iOS 11.2, *)
extension SKProduct.PeriodUnit {

    var description: String {
        switch self {
        case .day: return "day"
        case .week: return "week"
        case .month: return "month"
        case .year: return "year"
        // support for future values
        default:
            return "N/A"
        }
    }

    func pluralisedDescription(length: Int) -> String {
        let lengthAndDescription = length.description + " " + self.description
        let plural = length > 1 ?  lengthAndDescription + "s" : lengthAndDescription
        return plural
    }
}

然后是一个基于description属性返回复数的函数。

是的,正如其他人指出的那样,如果您的应用程序有其他语言版本,您应该本地化复数。

于 2019-04-16T10:47:38.110 回答
0
+ (NSString*)localizedTitleForSKPeriod:(SKProductSubscriptionPeriod*)period{
    NSDateComponents *comps = [NSDateComponents new];
    NSDateComponentsFormatter *fmt = [NSDateComponentsFormatter new];
    switch (period.unit) {
        case SKProductPeriodUnitDay:{
            fmt.allowedUnits = NSCalendarUnitDay;
            comps.day = period.numberOfUnits;
        }break;
        case SKProductPeriodUnitWeek:{
            fmt.allowedUnits = NSCalendarUnitWeekOfMonth;
            comps.weekOfMonth = period.numberOfUnits;
        }break;
        case SKProductPeriodUnitMonth:{
            fmt.allowedUnits = NSCalendarUnitMonth;
            comps.month = period.numberOfUnits;
        }break;
        case SKProductPeriodUnitYear: {
            fmt.allowedUnits = NSCalendarUnitYear;
            comps.year = period.numberOfUnits;
        }break;
    }
    // 1 Day, 1 Week, 2 Weeks, 1 Month, 2 Months, 3 Months, 6 Months, 1 Year
    fmt.unitsStyle = NSDateComponentsFormatterUnitsStyleFull;
    // One Day, One Week, Two Weeks, etc
    //fmt.unitsStyle = NSDateComponentsFormatterUnitsStyleSpellOut;
    NSString *s = [[fmt stringFromDateComponents:comps] capitalizedString];
    return s;
}
于 2020-12-02T18:41:20.037 回答
0

如果您使用SwiftyStoreKitlocalizedSubscriptionPeriod是最简单的方法

import SwiftyStoreKit

product.introductoryPrice?.localizedSubscriptionPeriod // "1 week"

这是实现: https ://github.com/bizz84/SwiftyStoreKit/blob/master/Sources/SwiftyStoreKit/SKProductDiscount+LocalizedPrice.swift

于 2021-08-08T08:47:38.457 回答
0

试用期不包含在 SKProduct 信息中,必须硬编码到应用程序中或存储在您的服务器上。获取此类信息的唯一可用选项(当前)是来自收据本身。

于 2017-10-11T23:01:17.567 回答
0

斯威夫特 5

使用EslamScott 的答案作为灵感:

import StoreKit

extension SKProduct {
    func priceString() -> String {
        let period:String = {
            switch self.subscriptionPeriod?.unit {
            case .day: return "day"
            case .week: return "week"
            case .month: return "month"
            case .year: return "year"
            case .none: return ""
            case .some(_): return ""
            }
        }()

        let price = self.localizedPrice!
        let numUnits = self.subscriptionPeriod?.numberOfUnits ?? 0
        let plural = numUnits > 1 ? "s" : ""
        return String(format: "%@ for %d %@%@", arguments: [price, numUnits, period, plural])
    }
}

要使用:

let price = product.priceString()
print(price)

结果:

THB 89.00 for 7 days
THB 149.00 for 1 month
于 2020-05-15T15:21:40.770 回答
0

从 iOS 11.2 开始,您可以使用introductoryPrice.SKProduct

它包含SKProductDiscount类的实例,它描述了包括免费试用在内的所有折扣期。

于 2018-01-06T15:57:16.850 回答
0

目标 C

#import "SKProduct+SKProduct.h"

-(NSString*_Nullable)localizedTrialDuraion{

if (@available(iOS 11.2, *)) {
    
    NSDateComponentsFormatter *formatter = [[NSDateComponentsFormatter alloc] init];
    [formatter setUnitsStyle:NSDateComponentsFormatterUnitsStyleFull]; //e.g 1 month
    formatter.zeroFormattingBehavior = NSDateComponentsFormatterZeroFormattingBehaviorDropAll;
    NSDateComponents * dateComponents = [[NSDateComponents alloc]init];
    [dateComponents setCalendar:[NSCalendar currentCalendar]];
    
    switch (self.introductoryPrice.subscriptionPeriod.unit) {
        case SKProductPeriodUnitDay:{
            formatter.allowedUnits = NSCalendarUnitDay;
            [dateComponents setDay:self.introductoryPrice.subscriptionPeriod.numberOfUnits];
            break;
        }
        case SKProductPeriodUnitWeek:{
            formatter.allowedUnits = NSCalendarUnitWeekOfMonth;
            [dateComponents setWeekOfMonth:self.introductoryPrice.subscriptionPeriod.numberOfUnits];
            break;
        }
        case SKProductPeriodUnitMonth:{
            formatter.allowedUnits = NSCalendarUnitMonth;
            [dateComponents setMonth:self.introductoryPrice.subscriptionPeriod.numberOfUnits];
            break;
        }
        case SKProductPeriodUnitYear:{
            formatter.allowedUnits = NSCalendarUnitYear;
            [dateComponents setYear:self.introductoryPrice.subscriptionPeriod.numberOfUnits];
            break;
        }
        default:{
            return nil;
            break;
        }
            break;
    }
    [dateComponents setValue:self.introductoryPrice.subscriptionPeriod.numberOfUnits forComponent:formatter.allowedUnits];
    return [formatter stringFromDateComponents:dateComponents];
} else {
    // Fallback on earlier versions
}

return nil;

}

于 2020-12-13T21:49:21.823 回答
-1

这是 swift 5 的更紧凑和更短的使用版本,扩展了 SKProductSubscriptionPeriod

用法:

print("\(period.localizedDescription) free trial")

//Printed example "1 week free trial"

执行:

extension SKProductSubscriptionPeriod {
    public var localizedDescription: String {
        let period:String = {
            switch self.unit {
            case .day: return "day"
            case .week: return "week"
            case .month: return "month"
            case .year: return "year"
            @unknown default:
                return "unknown period"
            }
        }()
    
        let plural = numberOfUnits > 1 ? "s" : ""
        return "\(numberOfUnits) \(period)\(plural)"
    }
}
于 2020-12-13T14:29:58.923 回答