App内购 Code 8步流程

注意: 本文,只写大体重要几个步骤,不是很详细。
仔细研究建议观看:
In-App Purchase for Developer
巧哥的(iOS应用内付费(IAP)开发步骤列表
Yi'mouleng
如何正确使用IAP
等等......

创建你的 SkpayManager

导入 StoreKit库

签订<SKPaymentTransactionObserver, SKProductsRequestDelegate>协议

SkpayManager:

1. 程序启动添加SKPay观察者,同时像后端请求产品列表信息
- (void)launch {
    [[SKPaymentQueue defaultQueue] addTransactionObserver:self];
    [self requestProductList];
}
2. 程序结束后,移除SKpay观察者
- (void)terminate {
    [[SKPaymentQueue defaultQueue] removeTransactionObserver:self];
}
3. 这时候已请求拿到我们服务端给的产品信息,购买页面可进行UI布局展示
UI展示
4. 进入购买页面,通过用户选择的产品ID,以及quantity进行购买
- (void)buyProductsWithId:(NSString *)productsId andQuantity:(NSInteger)quantity {

    self.productsId = productsId;
    self.quantity = quantity;
    if ([SKPaymentQueue canMakePayments]) {
        //允许程序内付费购买
        [self RequestProductData:@[self.productsId]];
    } else {
        [[ZToastManager ShardInstance] showtoast:@"您的手机没有打开程序内付费购买"];
    }
}
5. 然后通过用户选中的产品ID数组,SKProductsRequest 开始请求购买
- (void)RequestProductData:(NSArray *)productsIdArr {
    //请求对应的产品信息
    NSSet *nsset = [NSSet setWithArray:productsIdArr];
    SKProductsRequest *request = [[SKProductsRequest alloc] initWithProductIdentifiers:nsset];
    request.delegate = self;
    [request start];
}
6. SKProductsRequestDelegate 会接收到请求响应,在此回调中,发送购买请求
//收到的产品信息
- (void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response {
    //收到产品反馈信息
    NSArray *myProduct = response.products;
//    NSLog(@"产品Product ID:%@", response.invalidProductIdentifiers);
//    NSLog(@"产品付费数量: %d", (int) [myProduct count]);
    // populate UI
    for (SKProduct *product in myProduct) {
//        NSLog(@"product info");
//        NSLog(@"  基本描述: %@", [product description]);
//        NSLog(@"  IAP的id: %@", product.productIdentifier);
//        NSLog(@"  地区编码: %@", product.priceLocale.localeIdentifier);
//        NSLog(@"  本地价格: %@", product.price);
//        NSLog(@"  语言代码: %@", [product.priceLocale objectForKey:NSLocaleLanguageCode]);
//        NSLog(@"  国家代码: %@", [product.priceLocale objectForKey:NSLocaleCountryCode]);
//        NSLog(@"  货币代码: %@", [product.priceLocale objectForKey:NSLocaleCurrencyCode]);
//        NSLog(@"  货币符号: %@", [product.priceLocale objectForKey:NSLocaleCurgegrencySymbol]);
//        NSLog(@"  本地标题: %@", product.localizedTitle);
//        NSLog(@"  本地描述: %@", product.localizedDescription);
        [self updateProductPriceWithId:product.productIdentifier andPrice:product.price];
        if ([[product.priceLocale objectForKey:NSLocaleCurrencyCode] isEqualToString:@"CNY"]) {
            self.CurrencyCode = @"¥";
        } else {
            self.CurrencyCode = [product.priceLocale objectForKey:NSLocaleCurrencySymbol];
        }
    }
    //发送购买请求
    for (SKProduct *prct in myProduct) {
        if ([self.productsId isEqualToString:prct.productIdentifier]) {
            SKMutablePayment *payment = nil;
            payment = [SKMutablePayment paymentWithProduct:prct];
            payment.quantity = self.quantity;
            [[SKPaymentQueue defaultQueue] addPayment:payment];
        }
    }
}
7. SKPaymentTransactionObserver 此协议会监听到购买结果,根据购买结果的不同,做出不同的逻辑。
#pragma mark - SKPaymentTransactionObserver

//----监听购买结果
- (void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions {
    //交易结果
    for (SKPaymentTransaction *transaction in transactions) {
        switch (transaction.transactionState) {
            case SKPaymentTransactionStatePurchased: {
                //交易完成
                [self completeTransaction:transaction];
            }
                break;
            case SKPaymentTransactionStateFailed: {
                //交易失败
                [self hideHud];
                [self failedTransaction:transaction];
                [[ZToastManager ShardInstance] showtoast:@"交易失败"];
            }
                break;
            case SKPaymentTransactionStateRestored: {
                //已经购买过该商品
                [self hideHud];
                [self restoreTransaction:transaction];
                [[ZToastManager ShardInstance] showtoast:@"已经购买过该商品"];
            }
                break;
            case SKPaymentTransactionStatePurchasing: {
                //商品添加进列表
                NSLog(@"商品添加进列表");
            }
                break;
            case SKPaymentTransactionStateDeferred: {
                NSLog(@"SKPayment Transaction State Deferred");
            }
                break;
            default:
                break;
        }
    }
}

8. 购买成功之后,需将receipt 上传至自己的服务端
- (void)completeTransaction:(SKPaymentTransaction *)transaction {
    // Your application should implement these two methods.
    //NSString *product = transaction.payment.productIdentifier;
    [self verifyReceipt:transaction];
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • -我要在app里添加IAP,必须要注册自己的产品标识符(product identifiers)。产品标识符是什么...
    Levan_li阅读 6,357评论 2 6
  • 如今,不同系统平台都有专属的商店应用,Android平台有Google Play,Windows平台有Window...
    图灵教育阅读 3,839评论 0 1
  • 1、什么是Socket? Socket又称之为“套接字”,是系统提供的用于网络通信的方法。它的实质并不是一种协议,...
    木旁_G阅读 871评论 0 1
  • 我去寻找------ 我去寻找我的朋友, 朋友不在, 她去了哪里? 她已化作天上的云彩。 我去寻找------ 我...
    暖风夕阳阅读 365评论 0 0
  • 记得以前在电视里看到那些穿着学士服的人,心里除了羡慕再无其他。也在幻想什么时候自己也可以如他们一般,穿上这身衣服呢...
    指尖的雅香阅读 523评论 0 1