OC苹果内购
“大家都知道支付宝,微信的继承与用法,但是大家忘了苹果的自身的支付功能,我也是最近项目因为内购一直被拒,所以,研究了一下内购,好了,废话不多说, 直接上干货”
封装
首先遵守苹果内购的协议
SKPaymentTransactionObserver,SKProductsRequestDelegate
实现单例模式
+ (instancetype)shareSIAPManager{
static STRIAPManager *IAPManager = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken,^{
IAPManager = [[STRIAPManager alloc] init];
});
return IAPManager;
}
购买监听
[[SKPaymentQueue defaultQueue] addTransactionObserver:self]
实现开始购买服务
#pragma mark - 🚪public
- (void)startPurchWithID:(NSString *)purchID completeHandle:(IAPCompletionHandle)handle{
if (purchID) {
if ([SKPaymentQueue canMakePayments]) {
// 开始购买服务
_purchID = purchID;
_handle = handle;
NSSet *nsset = [NSSet setWithArray:@[purchID]];
request = [[SKProductsRequest alloc] initWithProductIdentifiers:nsset];
request.delegate = self;
[request start];
}else{
[self handleActionWithType:SIAPPurchNotArrow data:nil];
}
}
}
购买结果回调
#pragma mark - 🔒private
- (void)handleActionWithType:(SIAPPurchType)type data:(NSData *)data{
switch (type) {
case SIAPPurchSuccess:
NSLog(@"购买成功");
break;
case SIAPPurchFailed:
NSLog(@"购买失败");
break;
case SIAPPurchCancle:
NSLog(@"用户取消购买");
break;
case SIAPPurchVerFailed:
NSLog(@"订单校验失败");
break;
case SIAPPurchVerSuccess:
NSLog(@"订单校验成功");
break;
case SIAPPurchNotArrow:
NSLog(@"不允许程序内付费");
break;
default:
break;
}
if(_handle){
_handle(type,data);
}
}
** 代理方法 ** 划重点了 --------
#pragma mark - 🍐delegate
// 交易结束
- (void)completeTransaction:(SKPaymentTransaction *)transaction{
NSLog(@"交易结束");
NSURL *receiptUrl = [[NSBundle mainBundle] appStoreReceiptURL];
NSData *receiptData = [NSData dataWithContentsOfURL:receiptUrl];
NSString * transactionReceiptString = [receiptData base64EncodedStringWithOptions:0];
// Save(transactionReceiptString, @"JY");
[[SKPaymentQueue defaultQueue] finishTransaction:transaction];
_transaction = transaction;
[self handleActionWithType:SIAPPurchSuccess data:receiptData];
}
- (void)End {
if (_transaction) {
[[SKPaymentQueue defaultQueue] finishTransaction:_transaction];
}
}
// 交易失败
- (void)failedTransaction:(SKPaymentTransaction *)transaction{
[[SKPaymentQueue defaultQueue] finishTransaction:transaction];
if (transaction.error.code != SKErrorPaymentCancelled) {
[self handleActionWithType:SIAPPurchFailed data:nil];
}else{
[self handleActionWithType:SIAPPurchCancle data:nil];
}
}
#pragma mark - SKProductsRequestDelegate
- (void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response{
NSArray *product = response.products;
if([product count] <= 0){
NSLog(@"--------------没有商品------------------");
return;
}
SKProduct *p = nil;
for(SKProduct *pro in product){
if([pro.productIdentifier isEqualToString:_purchID]){
p = pro;
break;
}
}
NSLog(@"productID:%@", response.invalidProductIdentifiers);
NSLog(@"产品付费数量:%lu",(unsigned long)[product count]);
NSLog(@"%@",[p description]);
NSLog(@"%@",[p localizedTitle]);
NSLog(@"%@",[p localizedDescription]);
NSLog(@"%@",[p price]);
NSLog(@"%@",[p productIdentifier]);
NSLog(@"发送购买请求");
SKPayment *payment = [SKPayment paymentWithProduct:p];
[[SKPaymentQueue defaultQueue] addPayment:payment];
}
//请求失败
- (void)request:(SKRequest *)request didFailWithError:(NSError *)error{
NSLog(@"------------------错误-----------------:%@", error);
[self handleActionWithType:SIAPPurchTypeOvertime data:nil];
}
- (void)requestDidFinish:(SKRequest *)request{
NSLog(@"------------反馈信息结束-----------------");
}
#pragma mark - SKPaymentTransactionObserver
- (void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray<SKPaymentTransaction *> *)transactions{
for (SKPaymentTransaction *tran in transactions) {
switch (tran.transactionState) {
case SKPaymentTransactionStatePurchased:
[self completeTransaction:tran];
break;
case SKPaymentTransactionStatePurchasing:
NSLog(@"商品添加进列表");
break;
case SKPaymentTransactionStateRestored:
NSLog(@"已经购买过商品");
[[SKPaymentQueue defaultQueue] finishTransaction:tran];
break;
case SKPaymentTransactionStateFailed:
[self failedTransaction:tran];
break;
default:
break;
}
}
}
都加了注释, 大家可以看看 。
眼尖的小伙伴肯定发现了,没有删除监听方法,
- (void)dealloc {
[self delect];
}
- (void)delect {
[[SKPaymentQueue defaultQueue] removeTransactionObserver:self];
}
在viewController使用方法
#pragma mark - 这是内购吊起
- (void)payWithinBtn:(UIButton *)sender
{
// 实例化内购类
_apiMangager = [STRIAPManager shareSIAPManager];
// 苹果后台配置的产品ID
[_apiMangager startPurchWithID:payId completeHandle:^(SIAPPurchType type, NSData *data) {
// 请求回调类型返回的数据
NSString *rest = [data base64EncodedStringWithOptions:0];
switch (type) {
case SIAPPurchSuccess:
NSLog(@"前段验证购买成功,请后台接口进行验证,加密后的数据为(%@)",rest);
break;
case SIAPPurchFailed:
NSLog(@"购买失败");
break;
case SIAPPurchCancle:
NSLog(@"取消购买");
break;
case SIAPPurchNotArrow:
NSLog(@"此APP不允许内购");
break;
default:
NSLog(@"绝对是苹果的锅,我也无能为力");
break;
}
}];
}
*******************里面的payID 等一会会讲到,不要着急哦
很多小伙伴,肯定在想,怎么吊起内购呢, 为啥写这些就可以吊起, 别着急, 现在就给小伙伴说一下 App Store 里面的设置
E84F454EB251DF106ABFC8EC02A4CC07.png7259E26885C0D4479814EB9F596C385A.png点击用户和访问, 点击沙箱技术 -> 添加一下个号码
9452485E-722B-4B27-8D9C-00CDDDCD13A7.png这里注意一点, 地区, 你是哪里的,就填哪里, 别乱填, 要不不会接受到
B89E33F74A3895D23F0C4F4406980A79.png到这里基本, 你就可以运行代码了
需要注意一点的是 把自己的账号注销掉。 -----千万别登陆。
运行程序,会出现让你选取 现有ID 还是其他ID,
] F833681C69004B319036749F9FC5988B.png
8D19AF49ED02BAEDC3A534B8975104D2.png
到这里基本就完成了, 可以放心大胆的购买, 都不花钱
我是菜鸟, 其中有问题, 记得评论在底下, 我加上~
网友评论