1. 创建应用

首先进入iTunes Connect然后按下 Manage Your Applications

接下来按下Add New Applicationbutton创建应用

2. 在应用中创建IAP

创建应用之后,在Manage Your Applications中点应用的图示,进入应用

就会看到上图画面点击Manage In App Purchases就能够进入IAP的管理画面

在这边要注意左边的Bundle ID,在Xcode项目中,info.plist中的设定需与此Bundle同样

(此Bundle ID会在创建应用时填入)

此为IAP的管理画面,仅仅要按下Create Newbutton就可创建IAP

在本图中我已经创建好三个IAP

而当中要注意的是Product ID,仅仅要用Product ID就能够请求IAP的相关讯息及交易

Consumable

消耗品,每次下载都需付费

Non-Consumable

一次性付费,通经常使用在升级pro版或是移除广告等

Auto-Renewable Subscriptions

自己主动更新订阅

Free Subscription

免费订阅

Non-Renewing Subscription

非自己主动更新订阅

3. 怎样创建沙盒測试用户

进入iTunes Connect,点击Manage Users

在这个画面中点下Test User

按下Add New User就能够了,Email Address就是登入沙盒測试的username

password的部份在Add New User时可自行创建

4.代码

/*
* CBiOSStoreManager.h
* CloudBox Cross-Platform Framework Project
*
* Created by Cloud on 2012/10/30.
* Copyright 2011 Cloud Hsu. All rights reserved.
*
*/ #import <UIKit/UIKit.h>
#import <StoreKit/StoreKit.h> @interface CBiOSStoreManager : NSObject<SKProductsRequestDelegate,SKPaymentTransactionObserver>
{
NSString* _buyProductIDTag;
} + (CBiOSStoreManager*) sharedInstance; - (void) buy:(NSString*)buyProductIDTag;
- (bool) CanMakePay;
- (void) initialStore;
- (void) releaseStore;
- (void) requestProductData:(NSString*)buyProductIDTag;
- (void) provideContent:(NSString *)product;
- (void) recordTransaction:(NSString *)product; - (void) requestProUpgradeProductData:(NSString*)buyProductIDTag;
- (void) paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions;
- (void) purchasedTransaction: (SKPaymentTransaction *)transaction;
- (void) completeTransaction: (SKPaymentTransaction *)transaction;
- (void) failedTransaction: (SKPaymentTransaction *)transaction;
- (void) paymentQueueRestoreCompletedTransactionsFinished: (SKPaymentTransaction *)transaction;
- (void) paymentQueue:(SKPaymentQueue *) paymentQueue restoreCompletedTransactionsFailedWithError:(NSError *)error;
- (void) restoreTransaction: (SKPaymentTransaction *)transaction; @end

/*
* CBiOSStoreManager.mm
* CloudBox Cross-Platform Framework Project
*
* Created by Cloud on 2012/10/30.
* Copyright 2011 Cloud Hsu. All rights reserved.
*
*/ #import "CBiOSStoreManager.h" @implementation CBiOSStoreManager static CBiOSStoreManager* _sharedInstance = nil; +(CBiOSStoreManager*)sharedInstance
{
@synchronized([CBiOSStoreManager class])
{
if (!_sharedInstance)
[[self alloc] init]; return _sharedInstance;
}
return nil;
} +(id)alloc
{
@synchronized([CBiOSStoreManager class])
{
NSAssert(_sharedInstance == nil, @"Attempted to allocate a second instance of a singleton.\n");
_sharedInstance = [super alloc];
return _sharedInstance;
}
return nil;
} -(id)init {
self = [super init];
if (self != nil) {
// initialize stuff here
}
return self;
} -(void)initialStore
{
[[SKPaymentQueue defaultQueue] addTransactionObserver:self];
}
-(void)releaseStore
{
[[SKPaymentQueue defaultQueue] removeTransactionObserver:self];
} -(void)buy:(NSString*)buyProductIDTag
{
[self requestProductData:buyProductIDTag];
} -(bool)CanMakePay
{
return [SKPaymentQueue canMakePayments];
} -(void)requestProductData:(NSString*)buyProductIDTag
{
NSLog(@"---------Request product information------------\n");
_buyProductIDTag = [buyProductIDTag retain];
NSArray *product = [[NSArray alloc] initWithObjects:buyProductIDTag,nil];
NSSet *nsset = [NSSet setWithArray:product];
SKProductsRequest *request=[[SKProductsRequest alloc] initWithProductIdentifiers: nsset];
request.delegate=self;
[request start];
[product release];
} - (void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response{ NSLog(@"-----------Getting product information--------------\n");
NSArray *myProduct = response.products;
NSLog(@"Product ID:%@\n",response.invalidProductIdentifiers);
NSLog(@"Product count: %d\n", [myProduct count]);
// populate UI
for(SKProduct *product in myProduct){
NSLog(@"Detail product info\n");
NSLog(@"SKProduct description: %@\n", [product description]);
NSLog(@"Product localized title: %@\n" , product.localizedTitle);
NSLog(@"Product localized descitption: %@\n" , product.localizedDescription);
NSLog(@"Product price: %@\n" , product.price);
NSLog(@"Product identifier: %@\n" , product.productIdentifier);
}
SKPayment *payment = nil;
payment = [SKPayment paymentWithProduct:[response.products objectAtIndex:0]];
NSLog(@"---------Request payment------------\n");
[[SKPaymentQueue defaultQueue] addPayment:payment];
[request autorelease]; }
- (void)requestProUpgradeProductData:(NSString*)buyProductIDTag
{
NSLog(@"------Request to upgrade product data---------\n");
NSSet *productIdentifiers = [NSSet setWithObject:buyProductIDTag];
SKProductsRequest* productsRequest = [[SKProductsRequest alloc] initWithProductIdentifiers:productIdentifiers];
productsRequest.delegate = self;
[productsRequest start]; } - (void)request:(SKRequest *)request didFailWithError:(NSError *)error
{
NSLog(@"-------Show fail message----------\n");
UIAlertView *alerView = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Alert",NULL) message:[error localizedDescription]
delegate:nil cancelButtonTitle:NSLocalizedString(@"Close",nil) otherButtonTitles:nil];
[alerView show];
[alerView release];
} -(void) requestDidFinish:(SKRequest *)request
{
NSLog(@"----------Request finished--------------\n"); } -(void) purchasedTransaction: (SKPaymentTransaction *)transaction
{
NSLog(@"-----Purchased Transaction----\n");
NSArray *transactions =[[NSArray alloc] initWithObjects:transaction, nil];
[self paymentQueue:[SKPaymentQueue defaultQueue] updatedTransactions:transactions];
[transactions release];
} - (void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions
{
NSLog(@"-----Payment result--------\n");
for (SKPaymentTransaction *transaction in transactions)
{
switch (transaction.transactionState)
{
case SKPaymentTransactionStatePurchased:
[self completeTransaction:transaction];
NSLog(@"-----Transaction purchased--------\n");
UIAlertView *alerView = [[UIAlertView alloc] initWithTitle:@"Congratulation"
message:@"Transaction suceed!"
delegate:nil cancelButtonTitle:NSLocalizedString(@"Close",nil) otherButtonTitles:nil]; [alerView show];
[alerView release];
break;
case SKPaymentTransactionStateFailed:
[self failedTransaction:transaction];
NSLog(@"-----Transaction Failed--------\n");
UIAlertView *alerView2 = [[UIAlertView alloc] initWithTitle:@"Failed"
message:@"Sorry, your transcation failed, try again."
delegate:nil cancelButtonTitle:NSLocalizedString(@"Close",nil) otherButtonTitles:nil]; [alerView2 show];
[alerView2 release];
break;
case SKPaymentTransactionStateRestored:
[self restoreTransaction:transaction];
NSLog(@"----- Already buy this product--------\n");
case SKPaymentTransactionStatePurchasing:
NSLog(@"-----Transcation puchasing--------\n");
break;
default:
break;
}
}
} - (void) completeTransaction: (SKPaymentTransaction *)transaction
{
NSLog(@"-----completeTransaction--------\n");
// Your application should implement these two methods.
NSString *product = transaction.payment.productIdentifier;
if ([product length] > 0) { NSArray *tt = [product componentsSeparatedByString:@"."];
NSString *bookid = [tt lastObject];
if ([bookid length] > 0) {
[self recordTransaction:bookid];
[self provideContent:bookid];
}
} // Remove the transaction from the payment queue.
[[SKPaymentQueue defaultQueue] finishTransaction: transaction]; } -(void)recordTransaction:(NSString *)product
{
NSLog(@"-----Record transcation--------\n");
// Todo: Maybe you want to save transaction result into plist.
} -(void)provideContent:(NSString *)product
{
NSLog(@"-----Download product content--------\n");
} - (void) failedTransaction: (SKPaymentTransaction *)transaction
{
NSLog(@"Failed\n");
if (transaction.error.code != SKErrorPaymentCancelled)
{
}
[[SKPaymentQueue defaultQueue] finishTransaction: transaction];
}
-(void) paymentQueueRestoreCompletedTransactionsFinished: (SKPaymentTransaction *)transaction
{ } - (void) restoreTransaction: (SKPaymentTransaction *)transaction
{
NSLog(@"-----Restore transaction--------\n");
} -(void) paymentQueue:(SKPaymentQueue *) paymentQueue restoreCompletedTransactionsFailedWithError:(NSError *)error
{
NSLog(@"-------Payment Queue----\n");
} #pragma mark connection delegate
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
NSLog(@"%@\n", [[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] autorelease]);
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{ } - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
switch([(NSHTTPURLResponse *)response statusCode]) {
case 200:
case 206:
break;
case 304:
break;
case 400:
break;
case 404:
break;
case 416:
break;
case 403:
break;
case 401:
case 500:
break;
default:
break;
}
} - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
NSLog(@"test\n");
} -(void)dealloc
{
[super dealloc];
} @end
[[SKPaymentQueue defaultQueue] addTransactionObserver:self];

因为交易是永久有效的,建议addTransactionObserver这个于应用启动后即可监听,而不要在用户想买东西时才监听

- (void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions

交易结果会于此处显示

苹果准备沙盒測试环境需一段时间,若是创建IAP后能够取得资讯,却无法交易成功,要稍待一下

另外若是模拟器能够购买,但真机购买却失败的话,做Hard Reset就能够了,一直按住Home键跟电源键就能够做Hard Reset

按住后会出现关机画面,不要理会它,继续等,直到等到白苹果出现后,就能够放开等它重开机完成后再測试

5. IAP运作流程图

6.范例下载

下载连结

iOS IAP教程的更多相关文章

  1. [[iso教程]] 《4个月ios实体教程》全网最新、最全ios视频教程

    全网最新.最全ios视频教程 内容简介 <ios实体教程>主要介绍如何使用iOS提供的强大工具集创建iOS应用.全视频对iOS操作系统做了全面的介绍,首先讲解如何构建应用程序的用户界面,涵 ...

  2. fir.im Weekly - 给女朋友的 iOS 开发教程

    俗话说:技多不压身,功到自然成.本期 fir.im Weekly 收集的热度资源,大部分关于Android.iOS 开发工具和源码,还有一些有关设计的 Tips ,希望对你有帮助. 给女朋友的 iOS ...

  3. IOS 学习教程

    IOS 学习教程http://www.gaixue.com/course/236#### 讲课http://wenku.baidu.com/view/6786064fe518964bcf847c63. ...

  4. IOS编程教程(八):在你的应用程序添加启动画面

    IOS编程教程(八):在你的应用程序添加启动画面   虽然你可能认为你需要编写闪屏的代码,苹果已经可以非常轻松地把它做在Xcode中.不需要任何编码.你只需要做的是设置一些配置. 什么是闪屏 对于那些 ...

  5. 新手必看,史上最全的iOS开发教程集锦,没有之一!

    最近大火的iPhone XS Max和iPhone XS,不知道有没有同学已经下手了呢?一万三的价位确实让很多人望而却步啊.据说为了赢得中国的用户,专门出了双卡双待的,可想而知中国市场这块“肥肉”人人 ...

  6. Unity3D for iOS初级教程:Part 2/3

    转自Unity3D for iOS 这篇文章还可以在这里找到 英语 Learn how to use Unity to make a simple 3D iOS game! 这篇教材是来自教程团队成员 ...

  7. IOS IAP APP内支付 Java服务端代码

    IOS IAP APP内支付 Java服务端代码   场景:作为后台需要为app提供服务,在ios中,app内进行支付购买时需要进行二次验证. 基础:可以参考上一篇转载的博文In-App Purcha ...

  8. ios外包公司——技术分享:IOS开发教程

        iOS入门培训,适合已经有C/C++/Java/C#基础的人学习.   本大仙主讲,总共4讲(第4讲尚在制作中),这仅仅是iOS开发的入门而已.学完本教程,应该已经足够你自学并开发app了. ...

  9. iOS CoreBluetooth 教程

    去App Store搜索并下载“LightBlue”这个App,对调试你的app和理解Core Bluetooth会很有帮助. ================================ Cor ...

随机推荐

  1. JavaScript学习心得(十)

    Ajax Ajax是浏览器中使用JavaScript进行服务器后台请求,读取附加信息或者导致服务器响应的过程. Ajax广泛用于从服务器读取数据,并用所得到的数据更新页面,以及向服务器发送数据 Aja ...

  2. Apache 支持.htaccess

    ******************************************************************************* Apache 服务器 ********* ...

  3. 【python之旅】python简介和入门

    python简介: 一.什么是python python的创始人为吉多·范罗苏姆(Guido van Rossum).1989年的圣诞节期间,吉多·范罗苏姆为了打发时间,决心开发一个新的脚本解释程序, ...

  4. sourceInsight的技巧

    在用sourceInsight看代码...在这里积累技巧,慢慢积累吧 1.如何高亮显示所有要搜的东西,例如 1.aaaaaa 2. bbbbbbbbaaaaaaa 3. ccccccc 4. aaaa ...

  5. 安装linux系统后要做的事情

    基本安装0 http://www.kali.org.cn/thread-20517-1-1.html 基本安装1 http://defcon.cn/1618.html 基本安装2 http://www ...

  6. iOS:不同属性声明方式的解析

    代码: /* 属性声明方式说明: ----------------------- 1 @interface ... { id name } @end 这样声明的属性其实可以认为是private属性,因 ...

  7. Python如何读取指定文件夹下的所有图像

    (1)数据准备 数据集介绍: 数据集中存放的是1223幅图像,其中756个负样本(图像名称为0.1~0.756),458个正样本(图像名称为1.1~1.458),其中:"."前的标 ...

  8. Zoj 3868 GCD Expectation

    给一个集合,大小为n , 求所有子集的gcd 的期望和 . 期望的定义为 这个子集的最大公约数的K次方 : 每个元素被选中的概率是等可能的 即概率 p = (发生的事件数)/(总的事件数); 总的事件 ...

  9. 从零开始学习MySQL2---MySQL的安装与配置(只有Windows)

    因为我电脑只装了Windows系统,故而,只整理了在Windows系统下的安装方式 截图比较麻烦,故而多引用百度经验. Windows平台下安装与配置MySQL 5.6 下载,网址:http://de ...

  10. ARCI--做事情的重要方法论

    很多朋友都可能碰到这样的情况,有一个任务曾经开会讨论过,目标,时间,参与人都有提到,但是最终就是不了了之,没有下文了,而且后面想起来,要追究责任的时候,发现似乎都没有责任,无从追究.如果这种情况出现, ...