iOS開發(fā)之內(nèi)購(gòu)-AppStore

所謂內(nèi)購(gòu)就是在App內(nèi)購(gòu)買商品,如在游戲App中的購(gòu)買道具、皮膚等;在電商App中的購(gòu)買衣食住行的各種商品,如淘寶、京東。內(nèi)購(gòu)是指的在App內(nèi)購(gòu)買商品所使用的一種支付方式。

購(gòu)買商品如何支付?
第三方支付: 支付寶等
快捷/網(wǎng)銀付款:各種銀行儲(chǔ)蓄卡、信用卡
蘋果的內(nèi)購(gòu)

內(nèi)購(gòu)簡(jiǎn)介
什么時(shí)候使用內(nèi)購(gòu)?
對(duì)于App中銷售的商品(如道具、皮膚、金幣、會(huì)員、關(guān)卡等)只能在App內(nèi)使用,就必須使用內(nèi)購(gòu)支付方式,這種情況下蘋果強(qiáng)制使用內(nèi)購(gòu)方式,否則審核拒絕;
對(duì)于在App中購(gòu)買的商品不在App內(nèi)使用的如淘寶、京東等購(gòu)買的商品實(shí)是在真實(shí)世界中使用的,這種情況蘋果不強(qiáng)制必須使用內(nèi)購(gòu)方式,你想使用什么方式都可以;
內(nèi)購(gòu)說明:
內(nèi)購(gòu)蘋果要3/7分成 需要特別注意的是使用內(nèi)購(gòu)方式支付,蘋果是要收3/7分成。比如用戶某買了一個(gè)道具10元RMB,蘋果拿3元RMB,App開發(fā)商拿7元RMB。蘋果這是什么都不干還想要錢?。?br> 用戶購(gòu)買商品需要綁定銀行卡(過程麻煩,用戶在綁定過程中很有可能會(huì)放棄)
商品價(jià)格不能自定義,只有固定的價(jià)格等級(jí)

通常我們除了集成內(nèi)購(gòu)方式還會(huì)集成其他支付方式,通過服務(wù)器接口做一個(gè)開關(guān)配置需要顯示的支付方式,當(dāng)蘋果審核時(shí)服務(wù)器端接口只配置內(nèi)購(gòu)一種支付方式,當(dāng)蘋果審核通過了,服務(wù)器端將內(nèi)購(gòu)方式隱藏,將其他種類的支付方式顯示,這樣蘋果你甭想收錢了!
內(nèi)購(gòu)開發(fā)流程
協(xié)議、稅務(wù)和銀行業(yè)務(wù)
請(qǐng)求協(xié)議
添加新的法人實(shí)體
Set Up(設(shè)置) 聯(lián)系人(Contact Info)、稅務(wù)(Tax Info)、銀行信息(Bank Info) [圖片上傳失敗...(image-f0606e-1583920866648)]

[圖片上傳失敗...(image-e70c38-1583920866648)]
[圖片上傳失敗...(image-faee9f-1583920866648)]
[圖片上傳失敗...(image-6864ea-1583920866648)]
[圖片上傳失敗...(image-1d817b-1583920866648)]
[圖片上傳失敗...(image-40d580-1583920866648)]
[圖片上傳失敗...(image-74263a-1583920866648)]
[圖片上傳失敗...(image-4f7774-1583920866648)]
[圖片上傳失敗...(image-b3a5c1-1583920866648)]
[圖片上傳失敗...(image-3ed9d-1583920866648)]
[圖片上傳失敗...(image-4ef801-1583920866648)]
[圖片上傳失敗...(image-e5ce0f-1583920866648)]
[圖片上傳失敗...(image-76a729-1583920866648)]
[圖片上傳失敗...(image-67529d-1583920866648)]
[圖片上傳失敗...(image-938903-1583920866648)]

配置允許購(gòu)買的項(xiàng)目(消耗型項(xiàng)目和非消耗型項(xiàng)目) 消耗型項(xiàng)目:購(gòu)買一次,只能使用一次,用過就沒有了,例如 子彈、手榴彈 非消耗型項(xiàng)目:購(gòu)買一次,可以一直使用,例如 狙擊槍,槍買一次就可以了,但槍上的子彈射擊一次就消耗了一枚 我的App—>功能—>App 內(nèi)購(gòu)買項(xiàng)目 SKProduct: 產(chǎn)品ID(字符串:一般是Bundle ID + 商品ID,例如com.domain.appname.zidan)、商品名稱、商品描述、商品圖片、價(jià)格等級(jí)、

配置測(cè)試賬號(hào) 用戶和職能—>沙箱技術(shù)測(cè)試員
代碼集成 導(dǎo)入StoreKit.framework庫(kù):從蘋果服務(wù)器請(qǐng)求可以銷售的商品列表 從自己的App服務(wù)器中獲取需要銷售的商品列表
將這些商品拿到蘋果服務(wù)器進(jìn)行驗(yàn)證,獲取允許真正銷售的項(xiàng)目,展示在UITableView上

Request Amendments(請(qǐng)求協(xié)議)
示例代碼
XXProduct

#import <Foundation/Foundation.h>
@interface XXProduct : NSObject
@property(nonatomic, readonly, assign) NSInteger *ID;
@property(nonatomic, readonly) NSString *identifier;
@property(nonatomic, readonly) NSString *title;@property(nonatomic, readonly) NSString *image;
@property(nonatomic, readonly) NSString*descriptionInfo;@property(nonatomic, readonly) NSDecimalNumber *price;
@property(nonatomic, readonly) NSInteger quantity;
@property (assign, nonatomic) NSInteger type; // 商品類型: 消耗型 非消耗型@property (assign, nonatomic) BOOL isBuy; // 是否已經(jīng)購(gòu)買
@end


import "XXProduct.h"
@implementation XXProduct@end
ViewController
#import <UIKit/UIKit.h>@interface ViewController : UITableViewController
@end
#import "ViewController.h"
#import "XXProduct.h"
#import <AFNetworking.h>
#import <MJExtension.h>
#import <UIImageView+WebCache.h>
#import <StoreKit/StoreKit.h>
@interface ViewController () <SKProductsRequestDelegate, SKPaymentTransactionObserver>
@property(strong, nonatomic) NSArray<XXProduct *> *productList;
@property(strong, nonatomic) NSArray<SKProduct *> *products;
@property(strong, nonatomic) NSArray<XXProduct *> *dataSource;
@end
@implementation ViewControllerstatic 
NSString * const ID = @"ViewControllerProductCell";
- (void)viewDidLoad {
      [super viewDidLoad]; 
      [self.tableView registerClass:[UITableViewCell class]forCellReuseIdentifier:ID]; 
      [self requestProductListAndValidate]; // 恢復(fù)購(gòu)買:以下兩行代碼執(zhí)行后會(huì)執(zhí)行到paymentQueue:updatedTransactions:方法中的switch--case SKPaymentTransactionStateRestored:語(yǔ)句 // 作用是如果yoghurt已經(jīng)購(gòu)買過的非消耗型的商品就補(bǔ)再展示“購(gòu)買”按鈕了,可以用“已經(jīng)購(gòu)買”進(jìn)行提示 [[SKPaymentQueue defaultQueue] restoreCompletedTransactions]; [[SKPaymentQueue defaultQueue] addTransactionObserver:self];}
#pragma mark -#pragma mark - SKProductsRequestDelegate// 當(dāng)請(qǐng)求完畢,蘋果服務(wù)器返回?cái)?shù)據(jù)時(shí)調(diào)用
 - (void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response { 
      NSArray<SKProduct *> *products = response.products; // 可以銷售的商品 
      self.products = products; NSArray<NSString *> *invalidProductIdentifiers = response.invalidProductIdentifiers; // 無效的商品ID // 因SKProduct中沒有商品圖片,一般商品列表中要展示每個(gè)商品對(duì)應(yīng)的圖片的,所以UITableView的數(shù)據(jù)源最終還是要自己服務(wù)器中的商品列表數(shù)據(jù) // 這里將無效的商品過濾掉,剩下的都是可以允許銷售的商品 
      NSMutableArray *productList = [NSMutableArray array]; 
      for (XXProduct *product in self.productList) {
           if (![invalidProductIdentifiers containsObject:product.identifier]) {                   [productList addObject:product]; } } 
          self.dataSource = productList;}
#pragma mark - Table view data source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
         return self.dataSource.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];     XXProduct *product = self.dataSource[indexPath.row]; 
        [cell.imageView sd_setImageWithURL:[NSURL URLWithString:product.image]]; 
        cell.textLabel.text = product.title;                  
        cell.detailTextLabel.text = [NSString stringWithFormat:@"%@-%@", product.descriptionInfo, product.price]; // 模擬已經(jīng)購(gòu)買了非消耗型商品不展示購(gòu)買按鈕 
        if (product.isBuy) { 
            cell.backgroundColor = [UIColor grayColor]; 
        } 
      return cell;
}  
// 模擬購(gòu)買
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 
        // 1. 獲取要購(gòu)買的商品 
        SKProduct *product = self.products[indexPath.row]; 
        // 2.0 判斷當(dāng)前支付環(huán)境能否支付 
        if ([SKPaymentQueue canMakePayments]) { 
              // 2.1 獲取商品對(duì)應(yīng)的小票 
              SKPayment *payment = [SKPayment paymentWithProduct:product]; 
            // 2.2 拿著小票去排隊(duì)付款
             [[SKPaymentQueue defaultQueue] addPayment:payment]; 
            // 2.3 監(jiān)聽交易的整個(gè)過程 
            [[SKPaymentQueue defaultQueue] addTransactionObserver:self]; 
        } 
        else {
             UIAlertView *alerView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"您的手機(jī)沒有打開程序內(nèi)付費(fèi)購(gòu)買" delegate:nil cancelButtonTitle:NSLocalizedString(@"關(guān)閉",nil) otherButtonTitles:nil]; [alerView show]; 
        }
}
#pragma mark -#pragma mark - SKPaymentTransactionObserver// 當(dāng)交易狀態(tài)發(fā)生改變的時(shí)候調(diào)用
- (void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray<SKPaymentTransaction *> *)transactions {
           for (SKPaymentTransaction *paymentTransaction in transactions) { 
              switch (paymentTransaction.transactionState) { 
              case SKPaymentTransactionStateDeferred: // 延遲購(gòu)買:下單后長(zhǎng)時(shí)間步支付 
                  break; 
              case SKPaymentTransactionStatePurchasing: // 正在支付 
                  break; 
              case SKPaymentTransactionStatePurchased: {
                    // 支付成功 // 將該交易成功記錄到自己App服務(wù)器里 
                  XXProduct *product = [self getProductByIdentifier:paymentTransaction]; 
                  [self addTransactionSuccessRecored:product]; 
                  UIAlertView *alerView = [[UIAlertView alloc] initWithTitle:@"" message:@"購(gòu)買成功" delegate:nil cancelButtonTitle:NSLocalizedString(@"關(guān)閉",nil) otherButtonTitles:nil]; [alerView show]; // 從交易隊(duì)列中移除掉該交易 [queue finishTransaction:paymentTransaction]; } 
                break; 
              case SKPaymentTransactionStateFailed: { // 支付失敗 // 將交易失敗記錄在自己App的服務(wù)器中 
                    XXProduct *product = [self getProductByIdentifier:paymentTransaction]; 
                    [self addTransactionFailRecored:product]; 
                    UIAlertView *alerView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"購(gòu)買失敗,請(qǐng)重新嘗試購(gòu)買" delegate:nil cancelButtonTitle:NSLocalizedString(@"關(guān)閉",nil) otherButtonTitles:nil];  
                   [alerView show]; // 從交易隊(duì)列中移除掉該交易
                   [queue finishTransaction:paymentTransaction]; 
                } 
                    break; 
              case SKPaymentTransactionStateRestored: { 
                    // 恢復(fù)購(gòu)買 // 對(duì)于非消耗型商品已經(jīng)購(gòu)買過了應(yīng)該展示 “已經(jīng)購(gòu)買過了”, 不要展示購(gòu)買按鈕了 
                  XXProduct *product = [self getProductByIdentifier:paymentTransaction]; product.isBuy = YES;   
                  [self.tableView reloadData]; 
                  // 從交易隊(duì)列中移除掉該交易 
                  [queue finishTransaction:paymentTransaction]; 
              } 
              break; 
            } 
        }
    }
// 驗(yàn)證商品列表
- (void)requestProductListAndValidate{ 
          // 1. 從App服務(wù)器中獲取商品列表 
          // 2. 在蘋果服務(wù)器上驗(yàn)證這些商品,獲取真正允許銷售的商品 
            AFHTTPSessionManager *sessionManager = [AFHTTPSessionManager manager]; 
            [sessionManager GET:@"http:www.xxx.com/product/list" parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, NSDictionary * _Nullable responseObject) {
            NSArray<XXProduct *> *products = [XXProduct mj_objectArrayWithKeyValuesArray:responseObject]; 
            NSSet<NSString *> *productIdentifiers = [products valueForKeyPath:@"identifier"]; 
            self.productList = products; SKProductsRequest *productsRequest = [[SKProductsRequest alloc] initWithProductIdentifiers:productIdentifiers]; productsRequest.delegate = self; 
            [productsRequest start];
     } 
      failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { }];
  }
- (XXProduct *)getProductByIdentifier:(SKPaymentTransaction *)paymentTransaction { 
        NSString *productIdentifier = paymentTransaction.payment.productIdentifier; 
        // 搜索productIdentifier對(duì)應(yīng)的XXProduct 
        NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF == %@", productIdentifier]; 
        NSArray *tempArray = [self.dataSource filteredArrayUsingPredicate:predicate]; 
        XXProduct *product = [tempArray firstObject]; 
        return product;
}
// 添加交易成功記錄
- (void)addTransactionSuccessRecored:(XXProduct *)product { 
          NSLog(@"將交易記錄記錄在自己服務(wù)器中的數(shù)據(jù)庫(kù)中...");
}
// 添加交易失敗記錄
- (void)addTransactionFailRecored:(XXProduct *)product {
          NSLog(@"將交易記錄記錄在自己服務(wù)器中的數(shù)據(jù)庫(kù)中...");
}
- (void)dealloc { 
        [[SKPaymentQueue defaultQueue] removeTransactionObserver:self];
}
@end
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容