iOSPurchases
iOSPurchases
帮助您通过自己的服务器管理Apple Subscriptions。替换SwiftyStoreKit。
先决条件
- iOS 10.0+
安装
CocoaPods
iOSPurchases
通过CocoaPods提供。要安装它,只需将以下行添加到您的Podfile中
pod 'iOSPurchases'
入门
代码
App Delegate -
import iOSPurchases
在application didFinishLaunchingWithOptions
Purchase.shared.start(key: "YOUR_KEY_IS_HERE", shouldAddStorePayment: { (payment) -> (Bool) in
return true
})
Purchase.shared.serverUrlString = "YOUR SERVER URL HERE"
在application will terminate
Purchase.shared.applicationWillTerminate
订阅所需的通知
Purchase.optionsLoadedNotification
Purchase.restoreSuccessfulNotification
Purchase.purchaseSuccessfulNotification
Purchase.filedNotification
Purchase.restoreFailed
Purchase.optionsFailedToLoad
每次您想要加载选项时 -- 只需提供标识符列表
Purchase.shared.load(inAppOptions: ["GIVE","HERE","LIST","OF","INAPP","IDENTIFIERS"])
选项加载时,您将接收到Purchase.optionsLoadedNotification。然后只需使用
Purchase.shared.options
当用户尝试购买某物时 - 使用
Purchase.shared.purchase(option: InAppOption)
恢复
Purchase.shared.restore(needVerifyInstantly: Bool = false, verifyingCompletion: ((Response?, Error?) -> ())? = nil)
Response Structure
public struct Response: Decodable {
public let isSubscribed: Bool
public let dateEnd: Int64?
public let serverDate: Int64?
public var date: Date {
return Date(timeIntervalSince1970: Double(dateEnd ?? 0))
}
public var server: Date {
return Date(timeIntervalSince1970: Double(serverDate ?? 0))
}
init(data: Data) throws {
self = try JSONDecoder().decode(Response.self, from: data)
}
}
检查用户是否已订阅或验证订阅
Purchase.shared.isSubscribed(completion: @escaping (Bool, Error?) -> ())
Purchase.shared.verify(completion: ((Response?, Error?) -> ())? = nil)
需要改进的内容
- 自定义Response结构体
- ...
授权
iOSPurchases
采用MIT协议发布。具体细节请见 LICENSE.md。