iOS开发 内购购买详细逻辑代码封装 oc代码
以下是一个iOS开发中内购购买逻辑的代码封装示例(Objective-C代码):
首先,创建一个名为IAPManager的类来封装内购逻辑:
IAPManager.h文件:
#import <Foundation/Foundation.h>
#import <StoreKit/StoreKit.h>
@protocol IAPManagerDelegate <NSObject>
- (void)purchaseSuccessfulWithProductID:(NSString *)productID;
- (void)purchaseFailedWithError:(NSError *)error;
@end
@interface IAPManager : NSObject<SKPaymentTransactionObserver, SKProductsRequestDelegate>
@property (nonatomic, weak) id<IAPManagerDelegate> delegate;
+ (instancetype)sharedInstance;
- (void)startObservingTransactions;
- (void)stopObservingTransactions;
- (BOOL)canMakePurchases;
- (void)requestProductWithID:(NSString *)productID;
- (void)purchaseProduct:(SKProduct *)product;
- (void)restorePurchases;
@end
IAPManager.m文件:
#import "IAPManager.h"
@implementation IAPManager
+ (instancetype)sharedInstance {
static IAPManager *sharedInstance = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sharedInstance = [[self alloc] init];
});
return sharedInstance;
}
- (void)startObservingTransactions {
[[SKPaymentQueue defaultQueue] addTransactionObserver:self];
}
- (void)stopObservingTransactions {
[[SKPaymentQueue defaultQueue] removeTransactionObserver:self];
}
- (BOOL)canMakePurchases {
return [SKPaymentQueue canMakePayments];
}
- (void)requestProductWithID:(NSString *)productID {
SKProductsRequest *request = [[SKProductsRequest alloc] initWithProductIdentifiers:[NSSet setWithObject:productID]];
request.delegate = self;
[request start];
}
- (void)purchaseProduct:(SKProduct *)product {
SKPayment *payment = [SKPayment paymentWithProduct:product];
[[SKPaymentQueue defaultQueue] addPayment:payment];
}
- (void)restorePurchases {
[[SKPaymentQueue defaultQueue] restoreCompletedTransactions];
}
#pragma mark - SKProductsRequestDelegate
- (void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response {
NSArray *products = response.products;
if (products.count > 0) {
SKProduct *product = products[0];
[self purchaseProduct:product];
} else {
// Product not found
}
}
#pragma mark - SKPaymentTransactionObserver
- (void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray<SKPaymentTransaction *> *)transactions {
for (SKPaymentTransaction *transaction in transactions) {
switch (transaction.transactionState) {
case SKPaymentTransactionStatePurchased:
[self completeTransaction:transaction];
break;
case SKPaymentTransactionStateFailed:
[self failedTransaction:transaction];
break;
case SKPaymentTransactionStateRestored:
[self restoreTransaction:transaction];
break;
default:
break;
}
}
}
- (void)completeTransaction:(SKPaymentTransaction *)transaction {
[[SKPaymentQueue defaultQueue] finishTransaction:transaction];
if ([self.delegate respondsToSelector:@selector(purchaseSuccessfulWithProductID:)]) {
[self.delegate purchaseSuccessfulWithProductID:transaction.payment.productIdentifier];
}
}
- (void)failedTransaction:(SKPaymentTransaction *)transaction {
if (transaction.error.code != SKErrorPaymentCancelled) {
NSLog(@"Purchase failed with error: %@", transaction.error.localizedDescription);
}
[[SKPaymentQueue defaultQueue] finishTransaction:transaction];
if ([self.delegate respondsToSelector:@selector(purchaseFailedWithError:)]) {
[self.delegate purchaseFailedWithError:transaction.error];
}
}
- (void)restoreTransaction:(SKPaymentTransaction *)transaction {
[[SKPaymentQueue defaultQueue] finishTransaction:transaction];
if ([self.delegate respondsToSelector:@selector(purchaseSuccessfulWithProductID:)]) {
[self.delegate purchaseSuccessfulWithProductID:transaction.originalTransaction.payment.productIdentifier];
}
}
@end
使用时,只需在需要进行内购的地方调用相应的方法即可。例如,请求某个产品的信息:
[[IAPManager sharedInstance] requestProductWithID:@"com.yourproductid"];
购买某个产品:
[[IAPManager sharedInstance] purchaseProduct:product];
恢复购买:
[[IAPManager sharedInstance] restorePurchases];
同时,需要在需要接收内购结果的类中实现IAPManagerDelegate协议的方法,例如:
@interface YourViewController : UIViewController<IAPManagerDelegate>
@end
@implementation YourViewController
- (void)viewDidLoad {
[super viewDidLoad];
[IAPManager sharedInstance].delegate = self;
}
- (void)purchaseSuccessfulWithProductID:(NSString *)productID {
// 内购成功处理逻辑
}
- (void)purchaseFailedWithError:(NSError *)error {
// 内购失败处理逻辑
}
@end
以上代码封装了基本的内购逻辑,可以根据具体需求进行扩展和修改
原文地址: https://www.cveoy.top/t/topic/iSvt 著作权归作者所有。请勿转载和采集!