IOS AppStore内付费教程
转自:http://www.bubuko.com/infodetail-580520.html
这个图是我在已经发布的app上面截取的,添加了3个商品,已经是通过的的状态了(显示绿色),当您刚提交的时候,因为通过苹果的审查需要一段时间所以会显示黄色的等待状态,所以不必担心是不是商品编辑错了。如图:
=====================================第三部分======================================
这部分,我主要给大家演示一下,如何申请测试账号,利用苹果的沙盒测试环境来模拟AppStore的购买流程。
在ItunesConnect中选择“用户和职能”选项~
随后在左上角的选项中选择沙盒测试者,点击左上角的加号图标增加一位测试者,如图:
编辑好相应的内容,点击保存,就创建了一个测试账号,是不是很简单啊!当然这个账号如果你忘记了密码可以重新生成一个,无关紧要。
顺带多句嘴,千万不要在正式的appstore上面用沙盒测试的账号来登录,不然后果很严重,千万要牢记在心,此账号只用于测试环境下~
=====================================第四部分===========================================
接下来就是代码部分啦~
1.首先在项目工程中加入“storekit.framework”,加入头文件#import <StoreKit/StoreKit.h>
2.在.h文件中加入“SKPaymentTransactionObserver,SKProductsRequestDelegate”监听机制
下面贴上内购的核心代码,就几个函数,我在这边就不在做更多详细的解释了,各位看官可以运行跑一下就一目了然了。
.h文件
- //
- // PaymentViewController.h
- // IAPPayTest
- //
- // Created by silicon on 14-10-28.
- // Copyright (c) 2014年 silicon. All rights reserved.
- //
- #import <UIKit/UIKit.h>
- #import <StoreKit/StoreKit.h>
- @interface PaymentViewController : UIViewController<SKPaymentTransactionObserver,SKProductsRequestDelegate>
- @property (strong, nonatomic) IBOutlet UITextField *productID;
- @property (strong, nonatomic) IBOutlet UIButton *purchase;
- - (IBAction)purchaseFunc:(id)sender;
- @end
.m文件
- //
- // PaymentViewController.m
- // IAPPayTest
- //
- // Created by silicon on 14-10-28.
- // Copyright (c) 2014年 silicon. All rights reserved.
- //
- #import "PaymentViewController.h"
- @interface PaymentViewController ()
- @end
- @implementation PaymentViewController
- - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
- {
- self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
- if (self) {
- // Custom initialization
- }
- return self;
- }
- - (void)viewDidLoad
- {
- [super viewDidLoad];
- // Do any additional setup after loading the view from its nib.
- [[SKPaymentQueue defaultQueue] addTransactionObserver:self];
- self.productID.text = @"com.games.ztyxs.product_point.1";
- }
- - (void)didReceiveMemoryWarning
- {
- [super didReceiveMemoryWarning];
- // Dispose of any resources that can be recreated.
- }
- - (IBAction)purchaseFunc:(id)sender {
- NSString *product = self.productID.text;
- if([SKPaymentQueue canMakePayments]){
- [self requestProductData:product];
- }else{
- NSLog(@"不允许程序内付费");
- }
- }
- //请求商品
- - (void)requestProductData:(NSString *)type{
- NSLog(@"-------------请求对应的产品信息----------------");
- NSArray *product = [[NSArray alloc] initWithObjects:type, nil nil];
- NSSet *nsset = [NSSet setWithArray:product];
- SKProductsRequest *request = [[SKProductsRequest alloc] initWithProductIdentifiers:nsset];
- request.delegate = self;
- [request start];
- }
- //收到产品返回信息
- - (void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response{
- NSLog(@"--------------收到产品反馈消息---------------------");
- NSArray *product = response.products;
- if([product count] == 0){
- NSLog(@"--------------没有商品------------------");
- return;
- }
- NSLog(@"productID:%@", response.invalidProductIdentifiers);
- NSLog(@"产品付费数量:%d",[product count]);
- SKProduct *p = nil;
- for (SKProduct *pro in product) {
- NSLog(@"%@", [pro description]);
- NSLog(@"%@", [pro localizedTitle]);
- NSLog(@"%@", [pro localizedDescription]);
- NSLog(@"%@", [pro price]);
- NSLog(@"%@", [pro productIdentifier]);
- if([pro.productIdentifier isEqualToString:self.productID.text]){
- p = pro;
- }
- }
- SKPayment *payment = [SKPayment paymentWithProduct:p];
- NSLog(@"发送购买请求");
- [[SKPaymentQueue defaultQueue] addPayment:payment];
- }
- //请求失败
- - (void)request:(SKRequest *)request didFailWithError:(NSError *)error{
- NSLog(@"------------------错误-----------------:%@", error);
- }
- - (void)requestDidFinish:(SKRequest *)request{
- NSLog(@"------------反馈信息结束-----------------");
- }
- //监听购买结果
- - (void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transaction{
- for(SKPaymentTransaction *tran in transaction){
- switch (tran.transactionState) {
- case SKPaymentTransactionStatePurchased:
- NSLog(@"交易完成");
- break;
- case SKPaymentTransactionStatePurchasing:
- NSLog(@"商品添加进列表");
- break;
- case SKPaymentTransactionStateRestored:
- NSLog(@"已经购买过商品");
- break;
- case SKPaymentTransactionStateFailed:
- NSLog(@"交易失败");
- break;
- default:
- break;
- }
- }
- }
- //交易结束
- - (void)completeTransaction:(SKPaymentTransaction *)transaction{
- NSLog(@"交易结束");
- [[SKPaymentQueue defaultQueue] finishTransaction:transaction];
- }
- - (void)dealloc{
- [[SKPaymentQueue defaultQueue] removeTransactionObserver:self];
- [super dealloc];
- }
- @end
代码就这么多,到这边我们的IOS内购教程就接近尾声了,在测试的时候还有几点因素要注意一下:
1.沙盒环境测试appStore内购流程的时候,请使用没越狱的设备。
2.请务必使用真机来测试,一切以真机为准。
3.项目的Bundle identifier需要与您申请AppID时填写的bundleID一致,不然会无法请求到商品信息。
讲了这么多,附上几张测试截屏给大家展示一下:
请求商品时的打印日志
交易成功后:
手机截屏:
要求输入AppStore帐密,使用测试生成的即可
确定购买:
交易完成:
好了,所有的内购流程基本上讲完了,原谅我在图片上的涂抹,因为关系到产品的敏感词汇所以希望大家能够不介意。赶这篇博客的时间比较匆忙,如果有童鞋还有什么疑问或者我写的那个地方不对欢迎私信我或者评论,我会在第一时间回复。谢谢~