iOS內(nèi)購(gòu)一條龍------項(xiàng)目代碼 (4)

步驟一 iOS內(nèi)購(gòu)一條龍------賬戶信息填寫(xiě)(1)
步驟二 iOS內(nèi)購(gòu)一條龍------配置內(nèi)購(gòu)產(chǎn)品ID (2)
步驟三 iOS內(nèi)購(gòu)一條龍------內(nèi)購(gòu)測(cè)試賬號(hào) (3)

首先在項(xiàng)目中開(kāi)啟In-App Purchase,項(xiàng)目使用對(duì)應(yīng)內(nèi)購(gòu)項(xiàng)目的sku跟證書(shū),然后開(kāi)始copy代碼吧

該例子適用消耗型內(nèi)購(gòu),非消耗型請(qǐng)自行修改
1.首先在項(xiàng)目工程中加入“storekit.framework”,加入頭文件#import <StoreKit/StoreKit.h>
2.添加代理監(jiān)聽(tīng) <SKPaymentTransactionObserver,SKProductsRequestDelegate>

IAPManager.h

//
//  IAPManager.h
//  Purchase
//
//  Created by ice on 17/5/3.
//  Copyright ? 2017年 ice. All rights reserved.
//

#import <Foundation/Foundation.h>

typedef enum {
    kIAPPurchSuccess = 0,       // 購(gòu)買(mǎi)成功
    kIAPPurchFailed = 1,        // 購(gòu)買(mǎi)失敗
    kIAPPurchCancle = 2,        // 取消購(gòu)買(mǎi)
    KIAPPurchVerFailed = 3,     // 訂單校驗(yàn)失敗
    KIAPPurchVerSuccess = 4,    // 訂單校驗(yàn)成功
    kIAPPurchNotArrow = 5,      // 不允許內(nèi)購(gòu)
}IAPPurchType;

typedef void (^IAPCompletionHandle)(IAPPurchType type,NSData *data);

@interface IAPManager : NSObject

- (void)startPurchWithID:(NSString *)purchID completeHandle:(IAPCompletionHandle)handle;

@end

IAPManager.m

//
//  IAPManager.m
//  Purchase
//
//  Created by ice on 17/5/3.
//  Copyright ? 2017年 ice. All rights reserved.
//

#import "IAPManager.h"
#import <StoreKit/StoreKit.h>

@interface IAPManager () <SKPaymentTransactionObserver,SKProductsRequestDelegate>
@property (nonatomic,strong) NSString *purchID;
@property (nonnull,strong) IAPCompletionHandle handle;
@end

@implementation IAPManager

#pragma mark - system lifecycle
- (instancetype)init{
    self = [super init];
    if (self) {
        // 購(gòu)買(mǎi)監(jiān)聽(tīng)寫(xiě)在程序入口,程序掛起時(shí)移除監(jiān)聽(tīng),這樣如果有未完成的訂單將會(huì)自動(dòng)執(zhí)行并回調(diào) paymentQueue:updatedTransactions:方法
        [[SKPaymentQueue defaultQueue] addTransactionObserver:self];
    }
    return self;
}

- (void)dealloc{
    [[SKPaymentQueue defaultQueue] removeTransactionObserver:self];
}

#pragma mark - Public Method
- (void)startPurchWithID:(NSString *)purchID completeHandle:(IAPCompletionHandle)handle{
    if (purchID) {
        if ([SKPaymentQueue canMakePayments]) {
            // 開(kāi)始購(gòu)買(mǎi)服務(wù)
            self.purchID = purchID;
            self.handle = handle;
            NSSet *nsset = [NSSet setWithArray:@[purchID]];
            SKProductsRequest *request = [[SKProductsRequest alloc] initWithProductIdentifiers:nsset];
            request.delegate = self;
            [request start];
        }else{
            [self handleActionWithType:kIAPPurchNotArrow data:nil];
        }
    }
}

#pragma mark - Private Method
- (void)handleActionWithType:(IAPPurchType)type data:(NSData *)data{
#if DEBUG
    switch (type) {
        case kIAPPurchSuccess:
            NSLog(@"購(gòu)買(mǎi)成功");
            break;
        case kIAPPurchFailed:
            NSLog(@"購(gòu)買(mǎi)失敗");
            break;
        case kIAPPurchCancle:
            NSLog(@"用戶取消購(gòu)買(mǎi)");
            break;
        case KIAPPurchVerFailed:
            NSLog(@"訂單校驗(yàn)失敗");
            break;
        case KIAPPurchVerSuccess:
            NSLog(@"訂單校驗(yàn)成功");
            break;
        case kIAPPurchNotArrow:
            NSLog(@"不允許程序內(nèi)付費(fèi)");
            break;
        default:
            break;
    }
#endif
    if(self.handle){
        self.handle(type,data);
    }
}
// 交易結(jié)束
- (void)completeTransaction:(SKPaymentTransaction *)transaction{
    [self verifyPurchaseWithPaymentTransaction:transaction isTestServer:NO];
}

// 交易失敗
- (void)failedTransaction:(SKPaymentTransaction *)transaction{
    if (transaction.error.code != SKErrorPaymentCancelled) {
        [self handleActionWithType:kIAPPurchFailed data:nil];
    }else{
        [self handleActionWithType:kIAPPurchCancle data:nil];
    }
    
    [[SKPaymentQueue defaultQueue] finishTransaction:transaction];
}

- (void)verifyPurchaseWithPaymentTransaction:(SKPaymentTransaction *)transaction isTestServer:(BOOL)flag{
    //交易驗(yàn)證
    NSURL *recepitURL = [[NSBundle mainBundle] appStoreReceiptURL];
    NSData *receipt = [NSData dataWithContentsOfURL:recepitURL];
    
    if(!receipt){
        // 交易憑證為空驗(yàn)證失敗
        [self handleActionWithType:KIAPPurchVerFailed data:nil];
        return;
    }
    // 購(gòu)買(mǎi)成功將交易憑證發(fā)送給服務(wù)端進(jìn)行再次校驗(yàn)
    [self handleActionWithType:kIAPPurchSuccess data:receipt];
    
    NSError *error;
    NSDictionary *requestContents = @{
                                      @"receipt-data": [receipt base64EncodedStringWithOptions:0]
                                      };
    NSData *requestData = [NSJSONSerialization dataWithJSONObject:requestContents
                                                          options:0
                                                            error:&error];
    
    if (!requestData) { // 交易憑證為空驗(yàn)證失敗
        [self handleActionWithType:KIAPPurchVerFailed data:nil];
        return;
    }
    
    //In the test environment, use https://sandbox.itunes.apple.com/verifyReceipt
    //In the real environment, use https://buy.itunes.apple.com/verifyReceipt
    
    NSString *serverString = @"https://buy.itunes.apple.com/verifyReceipt";
    if (flag) {
        serverString = @"https://sandbox.itunes.apple.com/verifyReceipt";
    }
    NSURL *storeURL = [NSURL URLWithString:serverString];
    NSMutableURLRequest *storeRequest = [NSMutableURLRequest requestWithURL:storeURL];
    [storeRequest setHTTPMethod:@"POST"];
    [storeRequest setHTTPBody:requestData];
    
    NSOperationQueue *queue = [[NSOperationQueue alloc] init];
    [NSURLConnection sendAsynchronousRequest:storeRequest queue:queue
                           completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
                               if (connectionError) {
                                   // 無(wú)法連接服務(wù)器,購(gòu)買(mǎi)校驗(yàn)失敗
                                   [self handleActionWithType:KIAPPurchVerFailed data:nil];
                               } else {
                                   NSError *error;
                                   NSDictionary *jsonResponse = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
                                   if (!jsonResponse) {
                                       // 蘋(píng)果服務(wù)器校驗(yàn)數(shù)據(jù)返回為空校驗(yàn)失敗
                                       [self handleActionWithType:KIAPPurchVerFailed data:nil];
                                   }
                                   
                                   // 先驗(yàn)證正式服務(wù)器,如果正式服務(wù)器返回21007再去蘋(píng)果測(cè)試服務(wù)器驗(yàn)證,沙盒測(cè)試環(huán)境蘋(píng)果用的是測(cè)試服務(wù)器
                                   NSString *status = [NSString stringWithFormat:@"%@",jsonResponse[@"status"]];
                                   if (status && [status isEqualToString:@"21007"]) {
                                       [self verifyPurchaseWithPaymentTransaction:transaction isTestServer:YES];
                                   }else if(status && [status isEqualToString:@"0"]){
                                       [self handleActionWithType:KIAPPurchVerSuccess data:nil];
                                   }
#if DEBUG
                                   NSLog(@"----驗(yàn)證結(jié)果 %@",jsonResponse);
#endif
                               }
                           }];
    
    
    // 驗(yàn)證成功與否都注銷交易,否則會(huì)出現(xiàn)虛假憑證信息一直驗(yàn)證不通過(guò),每次進(jìn)程序都得輸入蘋(píng)果賬號(hào)
    [[SKPaymentQueue defaultQueue] finishTransaction:transaction];
}

#pragma mark - SKProductsRequestDelegate
- (void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response{
    NSArray *product = response.products;
    if([product count] <= 0){
#if DEBUG
        NSLog(@"--------------沒(méi)有商品------------------");
#endif
        return;
    }
    
    SKProduct *p = nil;
    for(SKProduct *pro in product){
        if([pro.productIdentifier isEqualToString:self.purchID]){
            p = pro;
            break;
        }
    }
    
#if DEBUG
    NSLog(@"productID:%@", response.invalidProductIdentifiers);
    NSLog(@"產(chǎn)品付費(fèi)數(shù)量:%lu",(unsigned long)[product count]);
    NSLog(@"%@",[p description]);
    NSLog(@"%@",[p localizedTitle]);
    NSLog(@"%@",[p localizedDescription]);
    NSLog(@"%@",[p price]);
    NSLog(@"%@",[p productIdentifier]);
    NSLog(@"發(fā)送購(gòu)買(mǎi)請(qǐng)求");
#endif
    
    SKPayment *payment = [SKPayment paymentWithProduct:p];
    [[SKPaymentQueue defaultQueue] addPayment:payment];
}

//請(qǐng)求失敗
- (void)request:(SKRequest *)request didFailWithError:(NSError *)error{
#if DEBUG
    NSLog(@"------------------錯(cuò)誤-----------------:%@", error);
#endif
}

- (void)requestDidFinish:(SKRequest *)request{
#if DEBUG
    NSLog(@"------------反饋信息結(jié)束-----------------");
#endif
}

#pragma mark - SKPaymentTransactionObserver
- (void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray<SKPaymentTransaction *> *)transactions{
    for (SKPaymentTransaction *tran in transactions) {
        switch (tran.transactionState) {
            case SKPaymentTransactionStatePurchased:
                [self completeTransaction:tran];
                break;
            case SKPaymentTransactionStatePurchasing:
#if DEBUG
                NSLog(@"商品添加進(jìn)列表");
#endif
                break;
            case SKPaymentTransactionStateRestored:
#if DEBUG
                NSLog(@"已經(jīng)購(gòu)買(mǎi)過(guò)商品");
#endif
                // 消耗型不支持恢復(fù)購(gòu)買(mǎi)
                [[SKPaymentQueue defaultQueue] finishTransaction:tran];
                break;
            case SKPaymentTransactionStateFailed:
                [self failedTransaction:tran];
                break;
            default:
                break;
        }
    }
}


@end

在ViewController中使用如下

//
//  ViewController.m
//  Purchase
//
//  Created by ice on 17/5/2.
//  Copyright ? 2017年 ice. All rights reserved.
//

#import "ViewController.h"
#import "IAPManager.h"

@interface ViewController ()
@property (nonatomic,strong) IAPManager *iapManager;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(100, 200, 100, 60)];
    [button setTitle:@"購(gòu)買(mǎi)" forState:UIControlStateNormal];
    button.backgroundColor = [UIColor redColor];
    [button addTarget:self action:@selector(purchaseAction) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
}


- (void)purchaseAction {
 
    if (!_iapManager) {
        _iapManager = [[IAPManager alloc] init];
    }
    
    // iTunesConnect 蘋(píng)果后臺(tái)配置的產(chǎn)品ID
    [_iapManager startPurchWithID:@"com.bb.helper_advisory" completeHandle:^(IAPPurchType type,NSData *data) {
        
    }];
}

@end

注意事項(xiàng):
1.沙盒環(huán)境測(cè)試appStore內(nèi)購(gòu)流程的時(shí)候,請(qǐng)使用沒(méi)越獄的設(shè)備。
2.請(qǐng)務(wù)必使用真機(jī)來(lái)測(cè)試,一切以真機(jī)為準(zhǔn)。
3.項(xiàng)目的Bundle identifier需要與您申請(qǐng)AppID時(shí)填寫(xiě)的bundleID一致,不然會(huì)無(wú)法請(qǐng)求到商品信息。
4.如果是你自己的設(shè)備上已經(jīng)綁定了自己的AppleID賬號(hào)請(qǐng)先注銷掉,否則你哭爹喊娘都不知道是怎么回事。
5.訂單校驗(yàn) 蘋(píng)果審核app時(shí),仍然在沙盒環(huán)境下測(cè)試,所以需要先進(jìn)行正式環(huán)境驗(yàn)證,如果發(fā)現(xiàn)是沙盒環(huán)境則轉(zhuǎn)到沙盒驗(yàn)證。
識(shí)別沙盒環(huán)境訂單方法:

1.根據(jù)字段 environment = sandbox。
2.根據(jù)驗(yàn)證接口返回的狀態(tài)碼,如果status=21007,則表示當(dāng)前為沙盒環(huán)境。

蘋(píng)果反饋的狀態(tài)碼:

21000App Store無(wú)法讀取你提供的JSON數(shù)據(jù)
21002 訂單數(shù)據(jù)不符合格式
21003 訂單無(wú)法被驗(yàn)證
21004 你提供的共享密鑰和賬戶的共享密鑰不一致
21005 訂單服務(wù)器當(dāng)前不可用
21006 訂單是有效的,但訂閱服務(wù)已經(jīng)過(guò)期。當(dāng)收到這個(gè)信息時(shí),解碼后的收據(jù)信息也包含在返回內(nèi)容中
21007 訂單信息是測(cè)試用(sandbox),但卻被發(fā)送到產(chǎn)品環(huán)境中驗(yàn)證
21008 訂單信息是產(chǎn)品環(huán)境中使用,但卻被發(fā)送到測(cè)試環(huán)境中驗(yàn)證
最后編輯于
?著作權(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)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

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

  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫(kù)、插件、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 15,828評(píng)論 4 61
  • Swift版本點(diǎn)擊這里歡迎加入QQ群交流: 594119878最新更新日期:18-09-17 About A cu...
    ylgwhyh閱讀 26,326評(píng)論 7 249
  • 人們常說(shuō),結(jié)了婚,女人就像天使落入了凡間,但我希望,各位能夠憑借記憶和智慧,找回自己的天使本性。
    炙星火閱讀 264評(píng)論 0 0
  • 基礎(chǔ)命令 home下建立share文件夾,test是子文件夾: [root@localhost home]# mk...
    游戲人生7閱讀 490評(píng)論 0 3
  • 他搞笑是假的,他是真的;你認(rèn)真是真的,你說(shuō)的是真的,做的也是真的,只是我還不愿接受,我還醒不來(lái)。 虛幻的活著,靠著...
    哈里登登閱讀 193評(píng)論 0 0

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