美文网首页
内购代码

内购代码

作者: 不吃饭会饿 | 来源:发表于2017-10-09 15:07 被阅读0次

内购

#import"ViewController.h"

#import

@interfaceViewController ()

@property(nonatomic,strong) NSArray *profuctIdArr;

@property(nonatomic,copy) NSString *currentProId;

@end

@implementationViewController

- (void)viewDidLoad {

[superviewDidLoad];

self.view.backgroundColor = [UIColor whiteColor];

UIButton * btn = [[UIButton alloc]initWithFrame:CGRectMake(100,100,100,100)];

btn.backgroundColor = [UIColor redColor];

[btn addTarget:selfaction:@selector(ClickOn:) forControlEvents:UIControlEventTouchUpInside];

[btn setTitle:@"6元"forState:UIControlStateNormal];

btn.tag =100;

[self.view addSubview:btn];

[[SKPaymentQueue defaultQueue] addTransactionObserver:self];

}

- (void) ClickOn:(UIButton *)sender{

NSString *product =self.profuctIdArr[sender.tag-100];

_currentProId = product;

if([SKPaymentQueue canMakePayments]) {

NSLog(@"yes");

[selfgetRequestAppleProduct];

}else{

NSLog(@"no");

}

}

- (void)getRequestAppleProduct{

NSArray *product = [[NSArray alloc] initWithObjects:@"123456",@"com.CYTestAppStore123",nil];

NSSet * nsset = [NSSet setWithArray:product];

SKProductsRequest * request = [[SKProductsRequest alloc]initWithProductIdentifiers:nsset];

request.delegate =self;

[request start];

}

#pragma SKProductsRequestDelegate

- (void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response{

NSArray * product = response.products;

NSLog(@"%@",response.invalidProductIdentifiers);

if(product.count ==0) {

NSLog(@"nothing");

return;

}

SKProduct *requestProduct =nil;

for(SKProduct * proinproduct) {

NSLog(@"%@",[pro description]);

NSLog(@"%@",[pro localizedTitle]);

NSLog(@"%@",[pro localizedDescription]);

NSLog(@"%@",[pro price]);

NSLog(@"%@",[pro productIdentifier]);

if([pro.productIdentifier isEqualToString:@"123456"]) {

requestProduct = pro;

}

}

SKPayment * payment = [SKPayment paymentWithProduct:requestProduct];

[[SKPaymentQueue defaultQueue]addPayment:payment];

}

- (void)request:(SKRequest *)request didFailWithError:(NSError *)error{

NSLog(@"error:%@",error);

}

- (void)requestDidFinish:(SKRequest *)request{

NSLog(@"信息反馈结束");

}

- (void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions{

for(SKPaymentTransaction * tranintransactions) {

switch(tran.transactionState) {

caseSKPaymentTransactionStatePurchased:

NSLog(@"交易完成");

[selfverifyPurchaseWithPaymentTransaction];

[[SKPaymentQueue defaultQueue] finishTransaction:tran];

break;

caseSKPaymentTransactionStatePurchasing:

NSLog(@"商品添加进列表");

break;

caseSKPaymentTransactionStateRestored:

NSLog(@"已经购买过商品");

break;

caseSKPaymentTransactionStateFailed:

NSLog(@"交易失败");

[[SKPaymentQueue defaultQueue] finishTransaction:tran];

break;

default:

break;

}

}

}

//沙盒测试环境验证

#define SANDBOX @"https://sandbox.itunes.apple.com/verifyReceipt"

//正式环境验证

#define AppStore @"https://buy.itunes.apple.com/verifyReceipt"

/**

*验证购买,避免越狱软件模拟苹果请求达到非法购买问题

*

*/

-(void)verifyPurchaseWithPaymentTransaction{

//从沙盒中获取交易凭证并且拼接成请求体数据

NSURL *receiptUrl=[[NSBundle mainBundle] appStoreReceiptURL];

NSData *receiptData=[NSData dataWithContentsOfURL:receiptUrl];

NSString *receiptString=[receiptData base64EncodedStringWithOptions:NSDataBase64EncodingEndLineWithLineFeed];//转化为base64字符串

NSString *bodyString = [NSString stringWithFormat:@"{\"receipt-data\" : \"%@\"}", receiptString];//拼接请求数据

NSData *bodyData = [bodyString dataUsingEncoding:NSUTF8StringEncoding];

//创建请求到苹果官方进行购买验证

NSURL *url=[NSURL URLWithString:SANDBOX];

NSMutableURLRequest *requestM=[NSMutableURLRequest requestWithURL:url];

requestM.HTTPBody=bodyData;

requestM.HTTPMethod=@"POST";

//创建连接并发送同步请求

NSError *error=nil;

NSData *responseData=[NSURLConnection sendSynchronousRequest:requestM returningResponse:nilerror:&error];

if(error) {

NSLog(@"验证购买过程中发生错误,错误信息:%@",error.localizedDescription);

return;

}

NSDictionary *dic=[NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingAllowFragments error:nil];

NSLog(@"%@",dic);

if([dic[@"status"] intValue]==0){

NSLog(@"购买成功!");

NSDictionary *dicReceipt= dic[@"receipt"];

NSDictionary *dicInApp=[dicReceipt[@"in_app"] firstObject];

NSString *productIdentifier= dicInApp[@"product_id"];//读取产品标识

//如果是消耗品则记录购买数量,非消耗品则记录是否购买过

NSUserDefaults *defaults=[NSUserDefaults standardUserDefaults];

if([productIdentifier isEqualToString:@"123"]) {

intpurchasedCount=[defaults integerForKey:productIdentifier];//已购买数量

[[NSUserDefaults standardUserDefaults] setInteger:(purchasedCount+1) forKey:productIdentifier];

}else{

[defaults setBool:YESforKey:productIdentifier];

}

//在此处对购买记录进行存储,可以存储到开发商的服务器端

}else{

NSLog(@"购买失败,未通过验证!");

}

}

//交易结束

- (void)completeTransaction:(SKPaymentTransaction *)transaction{

NSLog(@"交易结束");

[[SKPaymentQueue defaultQueue] finishTransaction:transaction];

}

- (NSString *)evironmentForReceipt:(NSString *)str{

str = [str stringByReplacingOccurrencesOfString:@"\r\n"withString:@""];

str = [str stringByReplacingOccurrencesOfString:@"\n"withString:@""];

str = [str stringByReplacingOccurrencesOfString:@"\t"withString:@""];

str = [str stringByReplacingOccurrencesOfString:@" "withString:@""];

str = [str stringByReplacingOccurrencesOfString:@"\""withString:@""];

NSArray * arr = [str componentsSeparatedByString:@";"];

NSString * environment = arr[2];

returnenvironment;

}

- (void)dealloc

{

[[SKPaymentQueue defaultQueue] removeTransactionObserver:self];

}

相关文章

  • 内购代码

    内购 #import"ViewController.h" #import @interfaceViewContro...

  • 内购 代码

    不管有没有用 参考一下 总没错。。 /********一下是iap内购*************/ 测试地址 ht...

  • iOS 苹果内购流程

    本文参考: iOS开发之内购完全笔记 iOS开发内购全套图文教程 iOS应用程序内购/内付费(一) 代码...

  • Swift 内购代码

    文章中仅有swift4.0 实现内购代码部分,其他设置网上很容易搜到

  • iOS 内购代码

    1:配置一个明确的APPID 2:配置和相关的项目 配置内购的物品 配置测试账号 配置银行相关 3:代码实现...

  • 内购代码和充值界面的封装

    前言:项目中用到了内购,这里主要想记录一下内购代码封装和充值界面的封装,其实内购代码也参考了网上其他的人的,只是好...

  • iOS 内购最新讲解

    一.总说内购的内容 协议、税务和银行业务 信息填写 内购商品的添加 添加沙盒测试账号 内购代码的具体实现 内购的注...

  • iOS 内购最新讲解

    一.总说内购的内容 协议、税务和银行业务 信息填写 内购商品的添加 添加沙盒测试账号 内购代码的具体实现 内购的注...

  • iOS内购从放弃到上架

    前段时间开发内购,踩了很多坑,发现网上教程大多是内购的代码,内购开发的代码不难,网上一搜一大片,填写协议、税务和银...

  • App内购通关:(二)代码篇

    一:内购流程 二:代码实现:内购工具类的集成 1.导入库 2.遵守协议 3.内购工具类的启动与注销 程序启动就开启...

网友评论

      本文标题:内购代码

      本文链接:https://www.haomeiwen.com/subject/kghpyxtx.html