iOS開發(fā) 內(nèi)購流程 手把手教你還不學?

做了很多電商的項目了,都在用各大第三方支付,作為一名iOS開發(fā),蘋果已經(jīng)在強制推行內(nèi)購了,所以,你不得不了解一下啦??赡苓€有些人不知道哪種情況下一定要用到內(nèi)購,簡單說明一下,如果你購買的商品,是在本app中使用和消耗的,就一定要用內(nèi)購,否則會被拒絕上線,例如:游戲幣,在線書籍,app中使用的道具等。如果購買的就是普通的商品,例如淘寶買東西等,就不需要用內(nèi)購,想怎么玩都自己說了算。比較坑的一點就是,內(nèi)購的話,還要和蘋果3/7分成呢,感覺好黑啊,有木有。。。廢話不多說,就讓我把憤怒化為下面的文字吧??

一.向appStore提交資料

第一步
第二步

第三步

操作完第三步然后記得保存

第四步
第五步

先點擊Contact Info 的Set Up

第六步
第七步

都填寫完記得保存,然后回到上一個頁面

第八步
第九步
第十步
第十一步

第十二步

進行十二步的時候可能有些銀行通過蘋果這個方法查不到,就需要借助百度了,一定要準確查詢,否則會有問題。我這里推薦一個地址
https://e.czbank.com/CORPORBANK/query_unionBank_index.jsp

第十三步

這一步需要注意的是,貨幣類型可能有歧義,看你是想收美元還是人民幣了,都說美元合適。不過,我做的時候為了避免事情,還是選擇了CNY,支持國產(chǎn)。還有一點,銀行賬號如果是對公的賬號,需要填寫公司的英文名稱,如果沒有的話,上拼音!然后點擊保存銀行信息就算ok了,然后退回到最開始的頁面

第十四步
第十五步
第十六步
第十七步
第十八步
第十九步

這里要注意的是,雖然這個頁面看起來要填寫的信息很多,其實很多都沒有什么卵用,都是對于美國那邊什么法律需要配合的那些,一般咱們都不用,只需要把該勾選的地方勾選,然后提交就好了。

第二十步
第二十一步
第二十二步

二.創(chuàng)建內(nèi)購項目

第一步
第二步

根據(jù)自己app的需要選擇類型,寫的很詳細,就不多說了

第三步
第四步
第五步
第六步

三、添加項目內(nèi)購測試帳號

第一步
第二步

四、廢話不多說,上代碼

第一步導(dǎo)入StoreKit.framework庫
然后先看.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

然后看.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)品標題 %@" , 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: 
//商品添加進列表
 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(@" 交易恢復(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)購還是有不少問題的,還是需要和后臺多多配合,例如,購買的時候默認的是當前登錄的蘋果id付款,如果想切換到別的id就會出現(xiàn)問題。。原來也沒有退款流程,現(xiàn)在可以退款了,詳細教程,以后我再說明吧。
ps:喜歡我的朋友多多支持,用你的小手點個喜歡或關(guān)注,就是對我最大的鼓勵。??

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 做了很多電商的項目了,都在用各大第三方支付,作為一名iOS開發(fā),蘋果已經(jīng)在強制推行內(nèi)購了,所以,你不得不了解一下啦...
    Xcode_6閱讀 1,303評論 0 0
  • iOS內(nèi)購流程: iOS內(nèi)購 什么時候用到呢? 虛擬產(chǎn)品就需要用到iOS內(nèi)購;購買的商品,是在本app中...
    會跳舞的獅子閱讀 2,869評論 5 24
  • iOS應(yīng)用如果涉及到支付功能,分為兩類:第三方支付和蘋果內(nèi)購。那么什么情況下選擇使用第三方支付,又在什么情況下選擇...
    ZfRee閱讀 39,234評論 36 66
  • 寶寶的小車壞了,爸爸過來修,打開了發(fā)現(xiàn)車生銹了,就說:“這車都生銹了,哪來的這么多水呢?” 女兒:“它渴了,就自己...
    蝸牛一號啊閱讀 445評論 0 0
  • 在一個陌生的地方,陌生的環(huán)境,面對陌生的人總是會有那樣這樣的感觸。 其實,發(fā)表在微信,發(fā)表在空間,發(fā)表在微博,上的...
    狄花秋色閱讀 325評論 0 0

友情鏈接更多精彩內(nèi)容