為一名iOS開發(fā),蘋果已經(jīng)在強制推行內(nèi)購了待侵,所以丢早,你不得不了解一下啦。可能還有些人不知道哪種情況下一定要用到內(nèi)購怨酝,簡單說明一下傀缩,如果你購買的商品,是在本app中使用和消耗的农猬,就一定要用內(nèi)購赡艰,否則會被拒絕上線,例如:游戲幣斤葱,在線書籍慷垮,app中使用的道具等
- 1. 向appStore提交帳戶資料
- 1.1 第一步
- 1.2 第二步
- 1.3 第三步
操作完第三步然后記得保存!
- 1.4 第四步
- 1.5 第五步
先點擊Contact Info 的Set Up
- 1.6 第六步
- 1.7 第七步
都填寫完記得保存,然后回到上一個頁面
- 1.8 第八步
- 1.9 第九步
- 1.10 第十步
- 1.11 第十一步
- 1.12 第十二步
進行十二步的時候可能有些銀行通過蘋果這個方法查不到揍堕,就需要借助百度了料身,一定要準確查詢,否則會有問題衩茸。我這里推薦一個地址https://e.czbank.com/CORPORBANK/query_unionBank_index.jsp
- 1.13 第十三步
這一步需要注意的是惯驼,貨幣類型可能有歧義,看你是想收美元還是人民幣了递瑰,都說美元合適祟牲。不過,我做的時候為了避免事情抖部,還是選擇了CNY说贝,支持國產(chǎn)脆炎。還有一點厢漩,銀行賬號如果是對公的賬號,需要填寫公司的英文名稱尔觉,如果沒有的話俯萎,上拼音傲宜!然后點擊保存銀行信息就算ok了,然后退回到最開始的頁面!
- 1.14 第十四步
- 1.15 第十五步
- 1.16 第十六步
- 1.17 第十七步
- 1.18 第十八步
- 1.19 第十九步
這里要注意的是夫啊,雖然這個頁面看起來要填寫的信息很多函卒,其實很多都沒有什么卵用,都是對于美國那邊什么法律需要配合的那些撇眯,一般咱們都不用报嵌,只需要把該勾選的地方勾選,然后提交就好了
- 1.20 第二十步
- 1.21 第二十一步
- 1.22 第二十二步
第二部分 熊榛,創(chuàng)建內(nèi)購項目
- 2.1 第一步
- 2.2 第二步
- 2.3 第三步
- 2.4 第四步
- 2.5 第五步
- 2.6 第六步
到這里內(nèi)購商品添加完成【新版界面可能有所不同锚国,但功能是一致的】
第三部分 添加內(nèi)購測試帳號
- 3.1 第一步
- 3.2 第二步
記住不能是用戶使用過的的AppleID帳號!不然無效···
第四部分玄坦,客戶端代碼集成部分血筑!
在工程中添加 storeKit 庫
#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
然后看.m文件
#import "RechargeVC.h"http://在內(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(@"---------請求對應的產(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)品標題 %@" , 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];
//支付20
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];
//交易結(jié)果
- (void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions
{
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:
{
//商品添加進列表 NSLog(@"-----商品添加進列表 --------");
}
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(@" 交易恢復處理");
}
-(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
{
//解除監(jiān)聽
[[SKPaymentQueue defaultQueue] removeTransactionObserver:self];
}
@end