Store Kit源码阅读

相关文章:
优惠:https://developer.apple.com/cn/documentation/storekit/in-app_purchase/subscriptions_and_offers/implementing_subscription_offers_in_your_app/

1、为APP内的内容和服务提供内购方式
2、通过App Store-signed交易核实用户的支付
3、验证由广告驱动的应用程序安装
4、为第三方内容提供建议
5、请求应用程序的应用商店的评论和评级
6、在应用程序中显示应用程序应用商店的消息

StoreKit概览
**支付相关**
#import 
#import 

#import 
#import 
#import 
#import 
// 商品信息
#import 
// 商品折扣信息
#import 
// 用于查询APP在苹果后台配置的商品信息
#import 
// App Store产品页面上为每台设备设置促销的应用内购买的顺序
// 详见:https://developer.apple.com/app-store/promoting-in-app-purchases/
#import 
// 刷新票据结果(无用?)
#import 
// 抽象类,子类SKProductsRequest和SKReceiptRefreshRequest
#import 

**广告网络归因**
#import 
#import 
#import 
#if TARGET_OS_IOS && !TARGET_OS_MACCATALYST
#import 
#endif

**Apple Music**
// 用于协助用户进行云服务设置 (例如 Apple Music 订阅) 的视图控制器。
#import 
// 用于确定用户音乐资料库的当前功能的对象。
#import 

**APP 评论和推荐**
// 用于控制请求用户提供 App Store 评分和评论的流程的对象。
#import 

#import 
// 提供可供用户从 App Store 购买媒体的页面的视图控制器。
#import 
#import 

// 用于显示叠层的类,可供您用来推荐另一个 App 或轻 App 的相应完整 App。
#import 
#import 
#import 

SK升级,仅涉及支付相关流程,其他内容不作调整(上表的第一部分)

SKDownload(API_DEPRECATED)
SKError

支付错误,枚举列表

// error codes for the SKErrorDomain
typedef NS_ENUM(NSInteger,SKErrorCode) {
    SKErrorUnknown,
    SKErrorClientInvalid,                                                                                       // client is not allowed to issue the request, etc.
    SKErrorPaymentCancelled,                                                                                    // user cancelled the request, etc.
    SKErrorPaymentInvalid,                                                                                      // purchase identifier was invalid, etc.
    SKErrorPaymentNotAllowed,                                                                                   // this device is not allowed to make the payment
    SKErrorStoreProductNotAvailable API_AVAILABLE(ios(3.0), macos(10.15), watchos(6.2)),                        // Product is not available in the current storefront
    SKErrorCloudServicePermissionDenied API_AVAILABLE(ios(9.3), tvos(9.3), watchos(6.2), macos(11.0)),          // user has not allowed access to cloud service information
    SKErrorCloudServiceNetworkConnectionFailed API_AVAILABLE(ios(9.3), tvos(9.3), watchos(6.2), macos(11.0)),   // the device could not connect to the nework
    SKErrorCloudServiceRevoked API_AVAILABLE(ios(10.3), tvos(10.3), watchos(6.2), macos(11.0)),                 // user has revoked permission to use this cloud service
    SKErrorPrivacyAcknowledgementRequired API_AVAILABLE(ios(12.2), tvos(12.2), macos(10.14.4), watchos(6.2)),   // The user needs to acknowledge Apple's privacy policy
    SKErrorUnauthorizedRequestData API_AVAILABLE(ios(12.2), macos(10.14.4), watchos(6.2)),                      // The app is attempting to use SKPayment's requestData property, but does not have the appropriate entitlement
    SKErrorInvalidOfferIdentifier API_AVAILABLE(ios(12.2), macos(10.14.4), watchos(6.2)),                       // The specified subscription offer identifier is not valid
    SKErrorInvalidSignature API_AVAILABLE(ios(12.2), macos(10.14.4), watchos(6.2)),                             // The cryptographic signature provided is not valid
    SKErrorMissingOfferParams API_AVAILABLE(ios(12.2), macos(10.14.4), watchos(6.2)),                           // One or more parameters from SKPaymentDiscount is missing
    SKErrorInvalidOfferPrice API_AVAILABLE(ios(12.2), macos(10.14.4), watchos(6.2)),                            // The price of the selected offer is not valid (e.g. lower than the current base subscription price)
    SKErrorOverlayCancelled API_AVAILABLE(ios(12.2), macos(10.14.4), watchos(6.2)),
    SKErrorOverlayInvalidConfiguration API_AVAILABLE(ios(14.0)) API_UNAVAILABLE(macos, watchos) __TVOS_PROHIBITED,
    SKErrorOverlayTimeout API_AVAILABLE(ios(14.0)) API_UNAVAILABLE(macos, watchos) __TVOS_PROHIBITED,
    SKErrorIneligibleForOffer API_AVAILABLE(ios(14.0), macos(11.0), watchos(7.0)),                              // User is not eligible for the subscription offer
    SKErrorUnsupportedPlatform API_AVAILABLE(ios(14.0), macos(11.0), watchos(7.0)) __TVOS_PROHIBITED,
    SKErrorOverlayPresentedInBackgroundScene API_AVAILABLE(ios(14.5)) API_UNAVAILABLE(macos, watchos) __TVOS_PROHIBITED  // Client tried to present an SKOverlay in UIWindowScene not in the foreground
} API_AVAILABLE(ios(3.0), macos(10.7), watchos(6.2));
SKPayment
@interface SKPayment : NSObject  {

+ (instancetype)paymentWithProduct:(SKProduct *)product;
// 在使用SKProductsRequest获取可见的products后使用 +paymentWithProduct:
+ (id)paymentWithProductIdentifier:(NSString *)identifier
// 与appStroe对应的标识符, 必选参数
@property(nonatomic, copy, readonly) NSString *productIdentifier;
// 与商店对应的付款请求数据。可选
@property(nonatomic, copy, readonly, nullable) NSData *requestData;
// 默认1, 优先级.
@property(nonatomic, readonly) NSInteger quantity;
// 使用者信息, 可选
@property(nonatomic, copy, readonly, nullable) NSString *applicationUsername;
// 在沙箱中强制执行此付款的“购买请求”流程 (iOS8.3以上版本)
@property(nonatomic, readonly) BOOL simulatesAskToBuyInSandbox;
// 指定折扣的相关数据将适用于此付款。可选的。(iOS12.2 以上版本)
@property(nonatomic, copy, readonly, nullable) SKPaymentDiscount *paymentDiscount;

@end

@interface SKMutablePayment : SKPayment

@property(nonatomic, copy, readwrite, nullable) NSString *applicationUsername;
@property(nonatomic, copy, readwrite, nullable) SKPaymentDiscount *paymentDiscount;
@property(nonatomic, copy, readwrite) NSString *productIdentifier;
@property(nonatomic, readwrite) NSInteger quantity;
@property(nonatomic, copy, readwrite, nullable) NSData *requestData;
@property(nonatomic, readwrite) BOOL simulatesAskToBuyInSandbox;

@end
SKPaymentQueue
// 与服务器的支付队列交互使用
@interface SKPaymentQueue : NSObject {
// 默认全局队列
+ (instancetype)defaultQueue;

    // 检验设备能否支持内购, YES: 能 默认是NO
+ (BOOL)canMakePayments;

// 异步事件, 添加一个支付事件到服务器队列, 复制payment,将SKPaymentTransaction添加到transaction事务数组中。可以多次添加相同的支付以创建多个事务。
- (void)addPayment:(SKPayment *)payment;

// 异步的。将当前用户已完成的事务添加回要重新完成的队列。将要求用户进行身份验证。观察者将收到0个或更多-paymentQueue:updatedTransactions:,后面跟着任意一个(在部分成功的情况下,仍然可以交付一些事务。)
// -paymentQueueRestoreCompletedTransactionsFinished: on success
// -paymentQueue:restoreCompletedTransactionsFailedWithError: on failure.
- (void)restoreCompletedTransactions;
- (void)restoreCompletedTransactionsWithApplicationUsername:(nullable NSString *)username;

// 异步从队列中删除一个已结束的事务.
- (void)finishTransaction:(SKPaymentTransaction *)transaction;

// 异步下载商品信息
- (void)startDownloads:(NSArray *)downloads;
- (void)pauseDownloads:(NSArray *)downloads;
- (void)resumeDownloads:(NSArray *)downloads;
- (void)cancelDownloads:(NSArray *)downloads;

// 事务数组只有在队列中有观察者时才与服务器同步。这可能需要用户进行身份验证。
- (void)addTransactionObserver:(id )observer;
- (void)removeTransactionObserver:(id )observer;
// 未完成的事务数组. 只有在队列被observers时有效. 更新时一步的.
@property(nonatomic, readonly) NSArray *transactions;

@end


@protocol SKPaymentTransactionObserver 
@required
// 事务数据发生变化的时候调用, 客户应检查交易状态,并在适当时完成。
- (void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions;
@optional
// 从队列中删除事务时调用 (通过此方法引起 finishTransaction:).
- (void)paymentQueue:(SKPaymentQueue *)queue removedTransactions:(NSArray *)transactions;
// 将用户的购买历史记录中的事务添加回队列时遇到错误时发送。
- (void)paymentQueue:(SKPaymentQueue *)queue restoreCompletedTransactionsFailedWithError:(NSError *)error;
// 成功地将用户购买历史记录中的所有事务添加回队列时发送。
- (void)paymentQueueRestoreCompletedTransactionsFinished:(SKPaymentQueue *)queue;
// 当下载状态改变时调用.
- (void)paymentQueue:(SKPaymentQueue *)queue updatedDownloads:(NSArray *)downloads;
// 当用户从应用程序商店启动IAP购买时发送(iOS11新增)
- (BOOL)paymentQueue:(SKPaymentQueue *)queue shouldAddStorePayment:(SKPayment *)payment forProduct:(SKProduct *)product;

@end

NS_ASSUME_NONNULL_END
SKPaymentTransaction

typedef NS_ENUM(NSInteger, SKPaymentTransactionState) {
    SKPaymentTransactionStatePurchasing,    // 事务正在添加到队列中.
    SKPaymentTransactionStatePurchased,     // 事务在队列中,用户已被收费。客户端应完成交易。
    SKPaymentTransactionStateFailed,        // 在添加到队列前事务被取消或者失败
    SKPaymentTransactionStateRestored,      // 从用户购买的历史记录中恢复的事务。客户端应完成交易。
    SKPaymentTransactionStateDeferred,   // 事务在队列中,但其最终状态是外部操作而挂起的.
};

@interface SKPaymentTransaction : NSObject {

// 仅仅在状态为SKPaymentTransactionFailed时有值
@property(nonatomic, readonly, nullable) NSError *error;
// 仅仅在状态为SKPaymentTransactionStateRestored时有效.
@property(nonatomic, readonly, nullable) SKPaymentTransaction *originalTransaction;
// 付款对象
@property(nonatomic, readonly) SKPayment *payment;
// 当前事务有效的下载
@property(nonatomic, readonly) NSArray *downloads;
// 事务添加到队列时的日期。只有在状态为skpaymenttransactionstatepur或skpaymenttransactionstaterestated时才有效
@property(nonatomic, readonly, nullable) NSDate *transactionDate;
// 队列中事物的唯一标识符.  状态为SKPaymentTransactionStatePurchased或SKPaymentTransactionStateRestored时有效
@property(nonatomic, readonly, nullable) NSString *transactionIdentifier;
// Only valid if state is SKPaymentTransactionStatePurchased.
@property(nonatomic, readonly, nullable) NSData *transactionReceipt;
// 事务的状态
@property(nonatomic, readonly) SKPaymentTransactionState transactionState;

@end

你可能感兴趣的:(Store Kit源码阅读)