应用内购买用于购买应用程序的其他内容或升级功能。
涉及步骤
步骤1 - 在iTunes connect中,确保您具有唯一的应用ID ,并且当无涯教程使用捆绑程序ID 和代码签名创建应用程序更新时在Xcode中具有相应的配置文件。
步骤2 - 创建一个新的应用程序并更新应用程序信息,您可以在Apple的添加新应用文档中了解更多信息。
步骤3 - 在应用程序页面的管理应用内购买中添加新产品供应用内购买。
步骤4 - 确保为您的应用设置银行详细信息,必须进行设置才能使应用内购买正常工作。另外,使用应用程序的iTunes连接页面中的管理用户选项创建一个测试用户帐户。
步骤5 - 下一步与处理代码和为无涯教程的应用内购买创建用户界面有关。
步骤6 - 创建一个单视图应用程序,并输入捆绑包标识符,该标识符是iTunes connect中指定的标识符。
步骤7 - 如下所示更新 ViewController.xib -
步骤8 - 为这三个标签和按钮分别创建 IBOutlets ,分别将其命名为productTitleLabel,product描述Label,productPriceLabel和purchaseButton。
步骤9 - 选择您的项目文件,然后选择目标 ,然后添加 StoreKit.framework 。
步骤10 - 如下更新 ViewController.h -
#import <UIKit/UIKit.h> #import <StoreKit/StoreKit.h> @interface ViewController : UIViewController< SKProductsRequestDelegate,SKPaymentTransactionObserver> { SKProductsRequest *productsRequest; NSArray *validProducts; UIActivityIndicatorView *activityIndicatorView; IBOutlet UILabel *productTitleLabel; IBOutlet UILabel *productRemarkLabel; IBOutlet UILabel *productPriceLabel; IBOutlet UIButton *purchaseButton; } - (void)fetchAvailableProducts; - (BOOL)canMakePurchases; - (void)purchaseMyProduct:(SKProduct*)product; - (IBAction)purchase:(id)sender; @end
步骤11 - 如下更新 ViewController.m -
#import "ViewController.h" #define kTutorialPointProductID @"com.tutorialPoints.testApp.testProduct" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; //添加活动指示器 activityIndicatorView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge]; activityIndicatorView.center = self.view.center; [activityIndicatorView hidesWhenStopped]; [self.view addSubview:activityIndicatorView]; [activityIndicatorView startAnimating]; //最初隐藏购买按钮 purchaseButton.hidden = YES; [self fetchAvailableProducts]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; //Dispose of any resources that can be recreated. } -(void)fetchAvailableProducts { NSSet *productIdentifiers = [NSSet setWithObjects:kTutorialPointProductID,nil]; productsRequest = [[SKProductsRequest alloc] initWithProductIdentifiers:productIdentifiers]; productsRequest.delegate = self; [productsRequest start]; } - (BOOL)canMakePurchases { return [SKPaymentQueue canMakePayments]; } - (void)purchaseMyProduct:(SKProduct*)product { if ([self canMakePurchases]) { SKPayment *payment = [SKPayment paymentWithProduct:product]; [[SKPaymentQueue defaultQueue] addTransactionObserver:self]; [[SKPaymentQueue defaultQueue] addPayment:payment]; } else { UIAlertView *alertView = [[UIAlertView alloc]initWithTitle: @"Purchases are disabled in your device" message:nil delegate: self cancelButtonTitle:@"Ok" otherButtonTitles: nil]; [alertView show]; } } -(IBAction)purchase:(id)sender { [self purchaseMyProduct:[validProducts objectAtIndex:0]]; purchaseButton.enabled = NO; } #pragma mark StoreKit Delegate -(void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions { for (SKPaymentTransaction *transaction in transactions) { switch (transaction.transactionState) { case SKPaymentTransactionStatePurchasing: NSLog(@"Purchasing"); break; case SKPaymentTransactionStatePurchased: if ([transaction.payment.productIdentifier isEqualToString:kTutorialPointProductID]) { NSLog(@"Purchased "); UIAlertView *alertView = [[UIAlertView alloc]initWithTitle: @"Purchase is completed succesfully" message:nil delegate: self cancelButtonTitle:@"Ok" otherButtonTitles: nil]; [alertView show]; } [[SKPaymentQueue defaultQueue] finishTransaction:transaction]; break; case SKPaymentTransactionStateRestored: NSLog(@"Restored "); [[SKPaymentQueue defaultQueue] finishTransaction:transaction]; break; case SKPaymentTransactionStateFailed: NSLog(@"Purchase failed "); break default: break; } } } -(void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response { SKProduct *validProduct = nil; int count = [response.products count]; if (count>0) { validProducts = response.products; validProduct = [response.products objectAtIndex:0]; if ([validProduct.productIdentifier isEqualToString:kTutorialPointProductID]) { [productTitleLabel setText:[NSString stringWithFormat: @"Product Title: %@",validProduct.localizedTitle]]; [productRemarkLabel setText:[NSString stringWithFormat: @"Product Desc: %@",validProduct.localizedRemark]]; [productPriceLabel setText:[NSString stringWithFormat: @"Product Price: %@",validProduct.price]]; } } else { UIAlertView *tmp = [[UIAlertView alloc] initWithTitle:@"Not Available" message:@"No products to purchase" delegate:self cancelButtonTitle:nil otherButtonTitles:@"Ok", nil]; [tmp show]; } [activityIndicatorView stopAnimating]; purchaseButton.hidden = NO; } @end
您必须将kTutorialPointProductID更新为为应用内购买创建的productID。您可以通过更新fetchAvailableProducts中的productIdentifiers的NSSet添加多个产品。类似地,为您添加的产品ID处理与购买相关的操作。
运行应用程序时,将获得以下输出-
确保您已在设置屏幕中退出帐户。单击"InitiatePurchase"后,选择"使用现有Apple ID"。输入有效的测试帐户用户名和密码。您将收到以下提示。
成功购买产品后,您将收到以下提示。在显示此提示的地方,您可以看到用于更新应用程序功能的相关代码。
参考链接
https://www.learnfk.com/ios/ios-in-app-purchase.html
标签:教程,nil,步骤,void,OC,无涯,应用程序,ViewController,self From: https://blog.51cto.com/u_14033984/7705787