做了很多電商的項目了,都在用各大第三方支付,作為一名iOS開發,蘋果已經在強制推行內購了,所以,你不得不了解一下啦。可能還有些人不知道哪種情況下一定要用到內購,簡單說明一下,如果你購買的商品,是在本app中使用和消耗的,就一定要用內購,否則會被拒絕上線,例如:游戲幣,在線書籍,app中使用的道具等。如果購買的就是普通的商品,例如淘寶買東西等,就不需要用內購,想怎么玩都自己說了算。比較坑的一點就是,內購的話,還要和蘋果3/7分成呢,感覺好黑啊,有木有。。。廢話不多說,就讓我把憤怒化為下面的文字吧??
一.向appStore提交資料
第一步
第二步
第三步
操作完第三步然后記得保存
第四步
第五步
先點擊Contact Info 的Set Up
第六步
第七步
都填寫完記得保存,然后回到上一個頁面
第八步
第九步
第十步
第十一步
第十二步
進行十二步的時候可能有些銀行通過蘋果這個方法查不到,就需要借助百度了,一定要準確查詢,否則會有問題。我這里推薦一個地址
https://e.czbank.com/CORPORBANK/query_unionBank_index.jsp
第十三步
這一步需要注意的是,貨幣類型可能有歧義,看你是想收美元還是人民幣了,都說美元合適。不過,我做的時候為了避免事情,還是選擇了CNY,支持國產。還有一點,銀行賬號如果是對公的賬號,需要填寫公司的英文名稱,如果沒有的話,上拼音!然后點擊保存銀行信息就算ok了,然后退回到最開始的頁面
第十四步
第十五步
第十六步
第十七步
第十八步
第十九步
這里要注意的是,雖然這個頁面看起來要填寫的信息很多,其實很多都沒有什么卵用,都是對于美國那邊什么法律需要配合的那些,一般咱們都不用,只需要把該勾選的地方勾選,然后提交就好了。
第二十步
第二十一步
第二十二步
二.創建內購項目
第一步
第二步
根據自己app的需要選擇類型,寫的很詳細,就不多說了
第三步
第四步
第五步
第六步
三、添加項目內購測試帳號
第一步
第二步
四、廢話不多說,上代碼
第一步導入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"
//在內購項目中創的商品單號
#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(@"允許程序內付費購買");
}else{
NSLog(@"不允許程序內付費購買");
UIAlertView *alerView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"您的手機沒有打開程序內付費購買"
delegate:nil cancelButtonTitle:NSLocalizedString(@"關閉",nil) otherButtonTitles:nil];
[alerView show]; }
}
-(void)RequestProductData{
NSLog(@"---------請求對應的產品信息------------");
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> 請求協議//收到的產品信息
- (void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response{
NSLog(@"-----------收到產品反饋信息--------------");
NSArray *myProduct = response.products;
NSLog(@"產品Product ID:%@",response.invalidProductIdentifiers);
NSLog(@"產品付費數量: %d", (int)[myProduct count]);
// populate UI
for(SKProduct *product in myProduct){
NSLog(@"product info");
NSLog(@"SKProduct 描述信息%@", [product description]);
NSLog(@"產品標題 %@" , product.localizedTitle);
NSLog(@"產品描述信息: %@" , 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(@"---------發送購買請求------------");
[[SKPaymentQueue defaultQueue] addPayment:payment];
}
- (void)requestProUpgradeProductData{
NSLog(@"------請求升級數據---------");
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(@"----------反饋信息結束--------------");
}
-(void) PurchasedTransaction: (SKPaymentTransaction *)transaction{
NSLog(@"-----PurchasedTransaction----");
NSArray *transactions =[[NSArray alloc] initWithObjects:transaction, nil];
[ self paymentQueue:[SKPaymentQueue defaultQueue] updatedTransactions:transactions];
}
//<SKPaymentTransactionObserver>千萬不要忘記綁定,代碼如下:
//----監聽購買結果
//[[SKPaymentQueue defaultQueue] addTransactionObserver:self];
- (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(@"關閉",nil) otherButtonTitles:nil];
[alerView show];
} break;
case SKPaymentTransactionStateFailed://交易失敗
{
[self failedTransaction:transaction];
NSLog(@"-----交易失敗 --------");
UIAlertView *alerView2 = [[UIAlertView alloc] initWithTitle:@"提示" message:@"購買失敗,請重新嘗試購買" delegate:nil cancelButtonTitle:NSLocalizedString(@"關閉",nil) otherButtonTitles:nil];
[alerView2 show];
}break;
case SKPaymentTransactionStateRestored://已經購買過該商品 [self restoreTransaction:transaction];
NSLog(@"-----已經購買過該商品 --------");
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(@"-----記錄交易--------");
}
//處理下載內容
-(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{
[[SKPaymentQueue defaultQueue] removeTransactionObserver:self];
//解除監聽
}
@end
后記
蘋果內購還是有不少問題的,還是需要和后臺多多配合,例如,購買的時候默認的是當前登錄的蘋果id付款,如果想切換到別的id就會出現問題。。原來也沒有退款流程,現在可以退款了,詳細教程,以后我再說明吧。
ps:喜歡我的朋友多多支持,用你的小手點個喜歡或關注,就是對我最大的鼓勵。??