本文主要是介绍[iOS]内购,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
XYIAPKit:https://github.com/MQCCoder/XYIAPKit
iOS内购编程指南:https://www.jianshu.com/p/17e0d11149f3
Demo:https://download.csdn.net/download/u012881779/11865475
后面这只是一个简单Demo,没下载的意义,仅用于验证能否成功完成购买和验证订单.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {// APP内部校验 在此设置内购的票据校验,防止掉单问题的发生 sharedSecretKeyXYStoreiTunesReceiptVerifier *shareRVer = [XYStoreiTunesReceiptVerifier shareInstance];shareRVer.sharedSecretKey = @""; // 部分类型的购买,比如包含自动包月,必须要填写自己在connect上获取的App专用共享密钥[[XYStore defaultStore] registerReceiptVerifier:[XYStoreiTunesReceiptVerifier shareInstance]];// 存储在NSUserDefaults 在此设置内购的票据校验,防止掉单问题的发生[[XYStore defaultStore] registerTransactionPersistor:[XYStoreUserDefaultsPersistence shareInstance]];self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];self.window.backgroundColor = [UIColor whiteColor];HomeViewController *home = [[HomeViewController alloc] initWithNibName:@"HomeViewController" bundle:nil];self.window.rootViewController = [[UINavigationController alloc] initWithRootViewController:home];[self.window makeKeyWindow];return YES;
}
@implementation HomeViewController- (IBAction)tapButton:(id)sender {UIButton *tempBut = (UIButton *)sender;if (tempBut.tag == 100) { // 100书币[self IPABuyWithProductID:@"yqyd_goods_100"];} else if (tempBut.tag == 101) { // 600书币[self IPABuyWithProductID:@"yqyd_goods_600"];} else if (tempBut.tag == 102) { // 3000书币[self IPABuyWithProductID:@"yqyd_goods_3000"];} else if (tempBut.tag == 103) { // 5000书币[self IPABuyWithProductID:@"yqyd_goods_5000"];} else if (tempBut.tag == 104) { // 9800书币[self IPABuyWithProductID:@"yqyd_goods_9800"];} else if (tempBut.tag == 105) { // 32800书币[self IPABuyWithProductID:@"yqyd_goods_32800"];} else if (tempBut.tag == 200) { // 自动续费包月会员[self IPABuyWithProductID:@"yqyd_vip_15"];} else if (tempBut.tag == 201) { // 季度会员[self IPABuyWithProductID:@"yqyd_vip_40"];} else if (tempBut.tag == 202) { // 半年会员[self IPABuyWithProductID:@"yqyd_vip_68"];} else if (tempBut.tag == 203) { // 包年会员
// [self IPABuyWithProductID:@"yqyd_vip_128"];XYStoreiTunesReceiptVerifier *ver = [XYStoreiTunesReceiptVerifier shareInstance];if ([ver isSubscribedWithAutoRenewProduct:@"yqyd_vip_15"]) {NSLog(@"11");} else {NSLog(@"22");}}
}// 使用商品ID内购
- (void)IPABuyWithProductID:(NSString *)pid {// 请求在线商品,并存储于内存中NSSet *set = [[NSSet alloc] initWithArray:@[pid]];[[XYStore defaultStore] requestProducts:set success:^(NSArray *products, NSArray *invalidProductIdentifiers) {NSString *pIdentifier = @"";if (products && products.count > 0) {SKProduct *pModel = products.firstObject;pIdentifier = pModel.productIdentifier;} else {pIdentifier = invalidProductIdentifiers.firstObject;}// 添加购买[[XYStore defaultStore] addPayment:pIdentifier success:^(SKPaymentTransaction *transaction) {NSLog(@"%@",transaction);NSLog(@"");} failure:^(SKPaymentTransaction *transaction, NSError *error) {NSLog(@"添加购买失败 %@",error);NSLog(@"");}];} failure:^(NSError *error) {NSLog(@"请求在线商品失败 %@",error);}];
}@end
这篇关于[iOS]内购的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!