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 <StoreKit/SKDownload.h>
#import <StoreKit/SKError.h>

#import <StoreKit/SKPayment.h>
#import <StoreKit/SKPaymentDiscount.h>
#import <StoreKit/SKPaymentQueue.h>
#import <StoreKit/SKPaymentTransaction.h>
// 商品信息
#import <StoreKit/SKProduct.h>
// 商品折扣信息
#import <StoreKit/SKProductDiscount.h>
// 用于查詢APP在蘋果后臺配置的商品信息
#import <StoreKit/SKProductsRequest.h>
// App Store產品頁面上為每臺設備設置促銷的應用內購買的順序
// 詳見:https://developer.apple.com/app-store/promoting-in-app-purchases/
#import <StoreKit/SKProductStorePromotionController.h>
// 刷新票據結果(無用?)
#import <StoreKit/SKReceiptRefreshRequest.h>
// 抽象類,子類SKProductsRequest和SKReceiptRefreshRequest
#import <StoreKit/SKRequest.h>

**廣告網絡歸因**
#import <StoreKit/SKAdImpression.h>
#import <StoreKit/SKAdNetwork.h>
#import <StoreKit/SKArcadeService.h>
#if TARGET_OS_IOS && !TARGET_OS_MACCATALYST
#import <StoreKit/SKANError.h>
#endif

**Apple Music**
// 用于協助用戶進行云服務設置 (例如 Apple Music 訂閱) 的視圖控制器。
#import <StoreKit/SKCloudServiceController.h>
// 用于確定用戶音樂資料庫的當前功能的對象。
#import <StoreKit/SKCloudServiceSetupViewController.h>

**APP 評論和推薦**
// 用于控制請求用戶提供 App Store 評分和評論的流程的對象。
#import <StoreKit/SKStoreReviewController.h>

#import <StoreKit/SKStorefront.h>
// 提供可供用戶從 App Store 購買媒體的頁面的視圖控制器。
#import <StoreKit/SKStoreProductViewController.h>
#import <StoreKit/StoreKitDefines.h>

// 用于顯示疊層的類,可供您用來推薦另一個 App 或輕 App 的相應完整 App。
#import <StoreKit/SKOverlay.h>
#import <StoreKit/SKOverlayConfiguration.h>
#import <StoreKit/SKOverlayTransitionContext.h>

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 <NSCopying, NSMutableCopying> {

+ (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<SKDownload *> *)downloads;
- (void)pauseDownloads:(NSArray<SKDownload *> *)downloads;
- (void)resumeDownloads:(NSArray<SKDownload *> *)downloads;
- (void)cancelDownloads:(NSArray<SKDownload *> *)downloads;

// 事務數組只有在隊列中有觀察者時才與服務器同步。這可能需要用戶進行身份驗證。
- (void)addTransactionObserver:(id <SKPaymentTransactionObserver>)observer;
- (void)removeTransactionObserver:(id <SKPaymentTransactionObserver>)observer;
// 未完成的事務數組. 只有在隊列被observers時有效. 更新時一步的.
@property(nonatomic, readonly) NSArray<SKPaymentTransaction *> *transactions;

@end


@protocol SKPaymentTransactionObserver <NSObject>
@required
// 事務數據發生變化的時候調用, 客戶應檢查交易狀態,并在適當時完成。
- (void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray<SKPaymentTransaction *> *)transactions;
@optional
// 從隊列中刪除事務時調用 (通過此方法引起 finishTransaction:).
- (void)paymentQueue:(SKPaymentQueue *)queue removedTransactions:(NSArray<SKPaymentTransaction *> *)transactions;
// 將用戶的購買歷史記錄中的事務添加回隊列時遇到錯誤時發送。
- (void)paymentQueue:(SKPaymentQueue *)queue restoreCompletedTransactionsFailedWithError:(NSError *)error;
// 成功地將用戶購買歷史記錄中的所有事務添加回隊列時發送。
- (void)paymentQueueRestoreCompletedTransactionsFinished:(SKPaymentQueue *)queue;
// 當下載狀態改變時調用.
- (void)paymentQueue:(SKPaymentQueue *)queue updatedDownloads:(NSArray<SKDownload *> *)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<SKDownload *> *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
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 227,818評論 6 531
  • 序言:濱河連續發生了三起死亡事件,死亡現場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機,發現死者居然都...
    沈念sama閱讀 98,185評論 3 414
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 175,656評論 0 373
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 62,647評論 1 309
  • 正文 為了忘掉前任,我火速辦了婚禮,結果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當我...
    茶點故事閱讀 71,446評論 6 405
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發上,一...
    開封第一講書人閱讀 54,951評論 1 321
  • 那天,我揣著相機與錄音,去河邊找鬼。 笑死,一個胖子當著我的面吹牛,可吹牛的內容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,041評論 3 440
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 42,189評論 0 287
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當地人在樹林里發現了一具尸體,經...
    沈念sama閱讀 48,718評論 1 333
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 40,602評論 3 354
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發現自己被綠了。 大學時的朋友給我發了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 42,800評論 1 369
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 38,316評論 5 358
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質發生泄漏。R本人自食惡果不足惜,卻給世界環境...
    茶點故事閱讀 44,045評論 3 347
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 34,419評論 0 26
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 35,671評論 1 281
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 51,420評論 3 390
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 47,755評論 2 371

推薦閱讀更多精彩內容