苹果服务器维护时间表2019,ios 内购详解(2019)

#import "EInAppPurchasing.h"

#import 

#if TARGET_IPHONE_SIMULATOR

// 开发时模拟器使用的验证服务器地址

#define ITMS_VERIFY_RECEIPT_URL    @""

#elif TARGET_OS_IPHONE

//真机验证的服务器地址

#define ITMS_VERIFY_RECEIPT_URL        @""

#endif

@interface EInAppPurchasing ()

@property (nonatomic, strong) SKProductsRequest *request;

@end

@implementationEInAppPurchasing

+ (instancetype)sharedInstance

{

staticEInAppPurchasing* instance =NULL;

staticdispatch_once_tonceToken;

dispatch_once(&onceToken, ^{

instance = [EInAppPurchasingnew];

});

returninstance;

}

- (instancetype)init

{

self= [superinit];

if(self) {

[[SKPaymentQueue defaultQueue] addTransactionObserver:self];

}

return self;

}

- (void)dealloc

{

[self releaseRequest];

[[SKPaymentQueue defaultQueue] removeTransactionObserver:self];

}

- (void)releaseRequest

{

if(_request) {

[_requestcancel];

_request.delegate=nil;

_request=nil;

}

}

- (void)identifyCanMakePayments:(NSArray*)requestArray

{

if(requestArray.count==0) {

if(self.delegate&& [self.delegaterespondsToSelector:@selector(updatedTransactions:)]) {

[self.delegate updatedTransactions:EPaymentTransactionStateAddPaymentFailed];

}

return;

}

if ([SKPaymentQueue canMakePayments]) {

[self releaseRequest];

self.request = [[SKProductsRequest alloc] initWithProductIdentifiers:[NSSet setWithArray:requestArray]];

_request.delegate=self;

[_requeststart];

}

else

{

if(self.delegate&& [self.delegaterespondsToSelector:@selector(updatedTransactions:)]) {

[self.delegate updatedTransactions:EPaymentTransactionStateNoPaymentPermission];

}

}

}

#pragma mark - SKProductsRequestDelegate

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

{

NSLog(@"-----------收到产品反馈信息-------------- 产品ID:%@ 产品数量:%ld",response.invalidProductIdentifiers,response.products.count);

NSArray * myProducts = response.products;

for(SKProduct*productinmyProducts){

NSLog(@"SKProduct 描述信息%@", [productdescription]);

NSLog(@"产品标题 %@", product.localizedTitle);

NSLog(@"产品描述信息: %@" , product.localizedDescription);

NSLog(@"价格: %@", product.price);

NSLog(@"Product id: %@" , product.productIdentifier);

}

if(myProducts && myProducts.count>0) {

SKProduct*product = [myProductsobjectAtIndex:0];

if(self.delegate&& [self.delegaterespondsToSelector:@selector(isProductIdentifierAvailable:)]) {

if ([self.delegate isProductIdentifierAvailable:product.productIdentifier]) {

SKPayment*payment = [SKPaymentpaymentWithProduct:product];

[[SKPaymentQueue defaultQueue] addPayment:payment];

return;

}

}

}

if(self.delegate&& [self.delegaterespondsToSelector:@selector(updatedTransactions:)]) {

[self.delegate updatedTransactions:EPaymentTransactionStateAddPaymentFailed];

}

}

/** 请求失败 */

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

NSLog(@"---购买失败");

}

#pragma mark - SKPaymentTransactionObserver

/** 监听购买结果 */

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

{

for(SKPaymentTransaction*transactionintransactions)

{

EPaymentTransactionState state;

switch(transaction.transactionState){

case SKPaymentTransactionStatePurchasing:

{

// 连接appStore

state =EPaymentTransactionStatePurchasing;

}

break;

case SKPaymentTransactionStatePurchased:

{

state =EPaymentTransactionStatePurchased;

//交易完成

if(isServiceVerify) {

[selfcompleteTransaction:transaction];

}

else

{

//本地作校验

[selfverifyPurchase:transaction];

}

}

break;

case SKPaymentTransactionStateFailed:

{

//交易失败

if(transaction.error.code!=SKErrorPaymentCancelled)

{

state =EPaymentTransactionStateFailed;

}else

{

state =EPaymentTransactionStateCancel;

}

[selffinshTransaction:transaction];

}

break;

case SKPaymentTransactionStateRestored:

{

state =EPaymentTransactionStateRestored;

//已经购买过该商品

[selffinshTransaction:transaction];

}

break;

case SKPaymentTransactionStateDeferred:

{

state =EPaymentTransactionStateDeferred;

}

break;

default:

break;

}

if(self.delegate&& [self.delegaterespondsToSelector:@selector(updatedTransactions:)]) {

[self.delegate updatedTransactions:state];

}

}

}

// Sent when transactions are removed from the queue (via finishTransaction:).

- (void)paymentQueue:(SKPaymentQueue*)queue removedTransactions:(NSArray *)transactions

{

NSLog(@"---removedTransactions");

}

// Sent when an error is encountered while adding transactions from the user's purchase history back to the queue.

- (void)paymentQueue:(SKPaymentQueue*)queue restoreCompletedTransactionsFailedWithError:(NSError*)error

{

NSLog(@"restoreCompletedTransactionsFailedWithError");

}

// Sent when all transactions from the user's purchase history have successfully been added back to the queue.

- (void)paymentQueueRestoreCompletedTransactionsFinished:(SKPaymentQueue*)queue

{

NSLog(@"paymentQueueRestoreCompletedTransactionsFinished");

}

// Sent when the download state has changed.

- (void)paymentQueue:(SKPaymentQueue*)queue updatedDownloads:(NSArray *)downloads

{

NSLog(@"updatedDownloads");

}

#pragma mark - Private

#pragma mark 验证购买

// 验证购买,在每一次购买完成之后,需要对购买的交易进行验证

// 所谓验证,是将交易的凭证进行"加密",POST请求传递给苹果的服务器,苹果服务器对"加密"数据进行验证之后,

// 会返回一个json数据,供开发者判断凭据是否有效

// 有些“内购助手”同样会拦截验证凭据,返回一个伪造的验证结果

// 所以在开发时,对凭据的检验要格外小心

- (void)verifyPurchase:(SKPaymentTransaction*)transaction

{

//ios7开始支持

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

NSData*receiptData = [NSDatadataWithContentsOfURL:receiptURL];

NSString *encodeStr = [receiptData base64EncodedStringWithOptions:NSDataBase64EncodingEndLineWithLineFeed];

NSURL *url = [NSURL URLWithString:ITMS_VERIFY_RECEIPT_URL];

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:10.0f];

NSString *payload = [NSString stringWithFormat:@"{\"receipt-data\" : \"%@\"}", encodeStr];

NSData *payloadData = [payload dataUsingEncoding:NSUTF8StringEncoding];

[requestsetHTTPBody:payloadData];

[requestsetHTTPMethod:@"POST"];

NSURLResponse*response =nil;

// 此请求返回的是一个json结果  将数据反序列化为数据字典

NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:nil];

if(data ==nil) {

return;

}

NSDictionary *jsonResponse = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];

if(jsonResponse !=nil) {

if([[jsonResponseobjectForKey:@"status"]intValue] ==0)

{

//通常需要校验:bid,product_id,purchase_date,status

}

else

{

//验证失败,检查你的机器是否越狱

}

}

//结束交易

[selffinshTransaction:transaction];

}

- (void)completeTransaction:(SKPaymentTransaction*)transaction

{

//服务器校验

}

- (void)finshTransaction:(SKPaymentTransaction*)transaction

{

//结束交易

[[SKPaymentQueue defaultQueue] finishTransaction: transaction];

}

@end

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值