版本記錄
版本號 | 時間 |
---|---|
V1.0 | 2017.05.03 |
前言
大家都知道ios上架apple store喇喉,app內(nèi)部的虛擬商品是需要走內(nèi)購的硅堆,和蘋果三七開,否則是上不了架的姓蜂,一般比如游戲里的金幣萎羔,寶石之類的都是需要走內(nèi)購的液走,下面說一下內(nèi)購的流程,下面是原文地址 —— app內(nèi)購,謝謝分享缘眶。
詳細(xì)流程
一嘱根、協(xié)議的填寫
直接上圖了。
第1步
第2步
第3步
第4步
第5步
第6步
第7步
第8步
第9步
第10步
第11步
第12步
第13步
第14步
第15步
第16步
第17步
第18步
第19步
第20步
第21步
二巷懈、創(chuàng)建內(nèi)購項目
第1步
第2步
第3步
第4步
第5步
第6步
第7步
三该抒、添加內(nèi)購項目測試賬號
第1步:創(chuàng)建測試賬號
第2步:添加沙盒測試員
第3步:填寫賬號信息
四、代碼實現(xiàn)
//首先導(dǎo)入StoreKit.framework庫
1 .h文件
#import <StoreKit/StoreKit.h>
enum{
IAP0p20=20,
IAP1p100,
IAP4p600,
IAP9p1000,
IAP24p6000,
} buyCoinsTag;
//代理
@interface RechargeVC : UIViewController <SKPaymentTransactionObserver,SKProductsRequestDelegate >
{
int buyType;
}
- (void) requestProUpgradeProductData;
- (void)RequestProductData;
- (void)buy:(int)type;
- (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;
- (void)provideContent:(NSString *)product;
- (void)recordTransaction:(NSString *)product;
@end
2 .m文件
#import "RechargeVC.h"
//在內(nèi)購項目中創(chuàng)的商品單號
#define ProductID_IAP0p20 @"Nada.JPYF01"http://20
#define ProductID_IAP1p100 @"Nada.JPYF02" //100
#define ProductID_IAP4p600 @"Nada.JPYF03" //600
#define ProductID_IAP9p1000 @"Nada.JPYF04" //1000
#define ProductID_IAP24p6000 @"Nada.JPYF05" //6000
@interface RechargeVC ()
@end
@implementation RechargeVC
- (void)viewDidLoad
{
[super viewDidLoad];
[[SKPaymentQueue defaultQueue] addTransactionObserver:self];
[self buy:IAP0p20];
}
- (void)buy:(int)type
{
buyType = type;
if ([SKPaymentQueue canMakePayments]) {
[self RequestProductData];
NSLog(@"允許程序內(nèi)付費購買");
}
else {
NSLog(@"不允許程序內(nèi)付費購買");
UIAlertView *alerView = [[UIAlertView alloc] initWithTitle:@"提示"
message:@"您的手機沒有打開程序內(nèi)付費購買"
delegate:nil cancelButtonTitle:NSLocalizedString(@"關(guān)閉",nil) otherButtonTitles:nil];
[alerView show];
}
}
- (void)RequestProductData
{
NSLog(@"---------請求對應(yīng)的產(chǎn)品信息------------");
NSArray *product = nil;
switch (buyType) {
case IAP0p20:
product=[[NSArray alloc] initWithObjects:ProductID_IAP0p20,nil];
break;
case IAP1p100:
product=[[NSArray alloc] initWithObjects:ProductID_IAP1p100,nil];
break;
case IAP4p600:
product=[[NSArray alloc] initWithObjects:ProductID_IAP4p600,nil];
break;
case IAP9p1000:
product=[[NSArray alloc] initWithObjects:ProductID_IAP9p1000,nil];
break;
case IAP24p6000:
product=[[NSArray alloc] initWithObjects:ProductID_IAP24p6000,nil];
break;
default:
break;
}
NSSet *nsset = [NSSet setWithArray:product];
SKProductsRequest *request=[[SKProductsRequest alloc] initWithProductIdentifiers: nsset];
request.delegate=self;
[request start];
}
//<SKProductsRequestDelegate> 請求協(xié)議
//收到的產(chǎn)品信息
- (void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response{
NSLog(@"-----------收到產(chǎn)品反饋信息--------------");
NSArray *myProduct = response.products;
NSLog(@"產(chǎn)品Product ID:%@",response.invalidProductIdentifiers);
NSLog(@"產(chǎn)品付費數(shù)量: %d", (int)[myProduct count]);
// populate UI
for(SKProduct *product in myProduct){
NSLog(@"product info");
NSLog(@"SKProduct 描述信息%@", [product description]);
NSLog(@"產(chǎn)品標(biāo)題 %@" , product.localizedTitle);
NSLog(@"產(chǎn)品描述信息: %@" , product.localizedDescription);
NSLog(@"價格: %@" , product.price);
NSLog(@"Product id: %@" , product.productIdentifier);
}
SKPayment *payment = nil;
switch (buyType) {
case IAP0p20:
payment = [SKPayment paymentWithProductIdentifier:ProductID_IAP0p20]; //支付25
break;
case IAP1p100:
payment = [SKPayment paymentWithProductIdentifier:ProductID_IAP1p100]; //支付108
break;
case IAP4p600:
payment = [SKPayment paymentWithProductIdentifier:ProductID_IAP4p600]; //支付618
break;
case IAP9p1000:
payment = [SKPayment paymentWithProductIdentifier:ProductID_IAP9p1000]; //支付1048
break;
case IAP24p6000:
payment = [SKPayment paymentWithProductIdentifier:ProductID_IAP24p6000]; //支付5898
break;
default:
break;
}
NSLog(@"---------發(fā)送購買請求------------");
[[SKPaymentQueue defaultQueue] addPayment:payment];
}
- (void)requestProUpgradeProductData
{
NSLog(@"------請求升級數(shù)據(jù)---------");
NSSet *productIdentifiers = [NSSet setWithObject:@"com.productid"];
SKProductsRequest* productsRequest = [[SKProductsRequest alloc] initWithProductIdentifiers:productIdentifiers];
productsRequest.delegate = self;
[productsRequest start];
}
//彈出錯誤信息
- (void)request:(SKRequest *)request didFailWithError:(NSError *)error{
NSLog(@"-------彈出錯誤信息----------");
UIAlertView *alerView = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Alert",NULL) message:[error localizedDescription]
delegate:nil cancelButtonTitle:NSLocalizedString(@"Close",nil) otherButtonTitles:nil];
[alerView show];
}
- (void) requestDidFinish:(SKRequest *)request
{
NSLog(@"----------反饋信息結(jié)束--------------");
}
- (void) PurchasedTransaction: (SKPaymentTransaction *)transaction
{
NSLog(@"-----PurchasedTransaction----");
NSArray *transactions =[[NSArray alloc] initWithObjects:transaction, nil];
[self paymentQueue:[SKPaymentQueue defaultQueue] updatedTransactions:transactions];
}
//<SKPaymentTransactionObserver> 千萬不要忘記綁定顶燕,代碼如下:
//----監(jiān)聽購買結(jié)果
//[[SKPaymentQueue defaultQueue] addTransactionObserver:self];
- (void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions//交易結(jié)果
{
NSLog(@"-----paymentQueue--------");
for (SKPaymentTransaction *transaction in transactions)
{
switch (transaction.transactionState)
{
case SKPaymentTransactionStatePurchased:{//交易完成
[self completeTransaction:transaction];
NSLog(@"-----交易完成 --------");
UIAlertView *alerView = [[UIAlertView alloc] initWithTitle:@""
message:@"購買成功"
delegate:nil cancelButtonTitle:NSLocalizedString(@"關(guān)閉",nil) otherButtonTitles:nil];
[alerView show];
} break;
case SKPaymentTransactionStateFailed://交易失敗
{ [self failedTransaction:transaction];
NSLog(@"-----交易失敗 --------");
UIAlertView *alerView2 = [[UIAlertView alloc] initWithTitle:@"提示"
message:@"購買失敗凑保,請重新嘗試購買"
delegate:nil cancelButtonTitle:NSLocalizedString(@"關(guān)閉",nil) otherButtonTitles:nil];
[alerView2 show];
}break;
case SKPaymentTransactionStateRestored://已經(jīng)購買過該商品
[self restoreTransaction:transaction];
NSLog(@"-----已經(jīng)購買過該商品 --------");
case SKPaymentTransactionStatePurchasing: //商品添加進(jìn)列表
NSLog(@"-----商品添加進(jìn)列表 --------");
break;
default:
break;
}
}
}
- (void) completeTransaction: (SKPaymentTransaction *)transaction
{
NSLog(@"-----completeTransaction--------");
// 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(@"-----記錄交易--------");
}
//處理下載內(nèi)容
- (void)provideContent:(NSString *)product
{
NSLog(@"-----下載--------");
}
- (void) failedTransaction: (SKPaymentTransaction *)transaction{
NSLog(@"失敗");
if (transaction.error.code != SKErrorPaymentCancelled)
{
}
[[SKPaymentQueue defaultQueue] finishTransaction: transaction];
}
- (void) paymentQueueRestoreCompletedTransactionsFinished: (SKPaymentTransaction *)transaction
{
}
- (void) restoreTransaction: (SKPaymentTransaction *)transaction
{
NSLog(@" 交易恢復(fù)處理");
}
- (void) paymentQueue:(SKPaymentQueue *) paymentQueue restoreCompletedTransactionsFailedWithError:(NSError *)error{
NSLog(@"-------paymentQueue----");
}
#pragma mark connection delegate
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
NSLog(@"%@", [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]);
}
- (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");
}
- (void)dealloc
{
[[SKPaymentQueue defaultQueue] removeTransactionObserver:self];//解除監(jiān)聽
}
@end
需要說明的是:內(nèi)購項目不再是單獨提交審核了,只要app提交審核內(nèi)購項目自然跟著改變狀態(tài)涌攻,不消耗物品請不要選擇 托管那一項欧引,不然會出現(xiàn)問題。
后記
再次謝謝技術(shù)達(dá)人的分享恳谎,希望每一個熱愛技術(shù)的工程師都可以從中受益芝此,共同成長。未完因痛,待續(xù)~~~~
靜