iOS 内购
From: http://blog.csdn.net/u014220518/article/details/55253275
一直觉得苹果生态圈很安全,iOS操作系统也很流畅,而且软件有严格的审核机制,软件来源唯一(当然是你不越狱),但是这也为苹果的霸权铺路;上家公司做了一个APP,可以充值虚拟金币,但是如果是虚拟道具,就必须使用苹果内购,不然审核过不了,而且很黑,三七分;当然,你如果购买真是东西,比如淘宝、京东等等就不需要了!这里我就来说说苹果内购流程,附上图文教程,希望大家喜欢!
1.填写协议
2.请求合同
3.填写地址
4.阅读并同意协议
5.填写联系方式
6.添加地址
7.选择设置地址
8.继续填写第二个
9.绑定银行卡和选择添加
10.选择国家
11.添加国际银行标识
注意:查询银行标识的方法的地址如下。
https://e.czbank.com/CORPORBANK/query_unionBank_index.jsp
12.银行卡信息
13.填写最后一项
14.内容比较多
15.一堆条约
16.继续填写
17.核对信息
18.继续
19.澳大利亚的不要管了
20.加拿大的也不用管了
21.填写完成
1.添加内购项目
2.选择内购类型
3.根据自己APP的需求选择类型
4.继续
5.商品列表
6.选择APP商品
1.选择用户职能
2.添加测试人员
![][60]
[60]:
1.先导入StoreKit.framework库;
2.创建AppPayManager管理类,遵循协议
3.创建单利和支付方法
#import <Foundation/Foundation.h>
#import <StoreKit/StoreKit.h>
@interface AppPayManager : NSObject
//个人信息单例
+ (AppPayManager *)manager;
- (void)buyProductsWithId:(NSString *)productsId andQuantity:(NSInteger)quantity;
@end
4.程序启动添加SKPay观察者,同事像我们的服务器请求商品列表
//1. 程序启动添加SKPay观察者,同时像后端请求产品列表信息
- (void)launch {
[[SKPaymentQueue defaultQueue] addTransactionObserver:self];
[self requestProductList];
}
- (void)requestProductList{
NSLog(@"requestProductList");
}
5.程序结束移除观察者
- (void)terminate {
[[SKPaymentQueue defaultQueue] removeTransactionObserver:self];
}
6.根据商品ID进行购买
- (void)buyProductsWithId:(NSString *)productsId andQuantity:(NSInteger)quantity {
self.productsId = productsId;
self.quantity = quantity;
if ([SKPaymentQueue canMakePayments]) {
//允许程序内付费购买
[self RequestProductData:@[self.productsId]];
} else {
//您的手机没有打开程序内付费购买
UIAlertView *alerView = [[UIAlertView alloc] initWithTitle:@"您的手机没有打开程序内付费购买" message:nil delegate:nil cancelButtonTitle:@"关闭" otherButtonTitles:nil];
[alerView show];
}
}
7.根据商品ID数组,SKProductsRequest请求购买
- (void)RequestProductData:(NSArray *)productsIdArr {
//请求对应的产品信息
NSSet *nsset = [NSSet setWithArray:productsIdArr];
SKProductsRequest *request = [[SKProductsRequest alloc] initWithProductIdentifiers:nsset];
request.delegate = self;
[request start];
}
8.SKProductsRequestDelegate 会接收到请求响应,在此回调中,发送购买请求
- (void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response {
//收到产品反馈信息
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(@" 基本描述: %@", [product description]);
// NSLog(@" IAP的id: %@", product.productIdentifier);
// NSLog(@" 地区编码: %@", product.priceLocale.localeIdentifier);
// NSLog(@" 本地价格: %@", product.price);
// NSLog(@" 语言代码: %@", [product.priceLocale objectForKey:NSLocaleLanguageCode]);
// NSLog(@" 国家代码: %@", [product.priceLocale objectForKey:NSLocaleCountryCode]);
// NSLog(@" 货币代码: %@", [product.priceLocale objectForKey:NSLocaleCurrencyCode]);
// NSLog(@" 货币符号: %@", [product.priceLocale objectForKey:NSLocaleCurgegrencySymbol]);
// NSLog(@" 本地标题: %@", product.localizedTitle);
// NSLog(@" 本地描述: %@", product.localizedDescription);
[self updateProductPriceWithId:product.productIdentifier andPrice:product.price];
if ([[product.priceLocale objectForKey:NSLocaleCurrencyCode] isEqualToString:@"CNY"]) {
self.currencyCode = @"¥";
} else {
self.currencyCode = [product.priceLocale objectForKey:NSLocaleCurrencySymbol];
}
}
//发送购买请求
for (SKProduct *prct in myProduct) {
if ([self.productsId isEqualToString:prct.productIdentifier]) {
SKMutablePayment *payment = nil;
payment = [SKMutablePayment paymentWithProduct:prct];
payment.quantity = self.quantity;
[[SKPaymentQueue defaultQueue] addPayment:payment];
}
}
}
- (void)updateProductPriceWithId:(NSString *)productIdentifier andPrice:(NSDecimalNumber *)price{
NSLog(@"productIdentifier == %@",productIdentifier);
NSLog(@"price == %@",price);
}
9.SKPaymentTransactionObserver 此协议会监听到购买结果,根据购买结果的不同,做出不同的逻辑
#pragma mark - SKPaymentTransactionObserver
//----监听购买结果
- (void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions {
//交易结果
for (SKPaymentTransaction *transaction in transactions) {
switch (transaction.transactionState) {
case SKPaymentTransactionStatePurchased: {
//交易完成
[self completeTransaction:transaction];
}
break;
case SKPaymentTransactionStateFailed: {
//交易失败
[self failedTransaction:transaction];
UIAlertView *alerView = [[UIAlertView alloc] initWithTitle:@"交易失败" message:nil delegate:nil cancelButtonTitle:@"关闭" otherButtonTitles:nil];
[alerView show];
}
break;
case SKPaymentTransactionStateRestored: {
//已经购买过该商品
[self restoreTransaction:transaction];
UIAlertView *alerView = [[UIAlertView alloc] initWithTitle:@"已经购买过该商品" message:nil delegate:nil cancelButtonTitle:@"关闭" otherButtonTitles:nil];
[alerView show];
}
break;
case SKPaymentTransactionStatePurchasing: {
//商品添加进列表
NSLog(@"商品添加进列表");
}
break;
case SKPaymentTransactionStateDeferred: {
NSLog(@"SKPayment Transaction State Deferred");
}
break;
default:
break;
}
}
}
- (void)failedTransaction: (SKPaymentTransaction *)transaction{
NSLog(@"失败");
if (transaction.error.code != SKErrorPaymentCancelled) { }
[[SKPaymentQueue defaultQueue] finishTransaction: transaction];
}
- (void)restoreTransaction:(SKPaymentTransaction *)transaction{
NSLog(@"交易恢复处理");
}
9.购买成功后,将信息上传自己的服务器
- (void)completeTransaction:(SKPaymentTransaction *)transaction{
NSLog(@"-----completeTransaction--------");
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];}
}
}
//记录交易
- (void)recordTransaction:(NSString *)product{
NSLog(@"记录交易--product == %@",product);
}
//处理下载内容
- (void)provideContent:(NSString *)product{
NSLog(@"处理下载内容--product == %@",product);
}
希望对你有所帮助!
install_url
to use ShareThis. Please set it in _config.yml
.