應(yīng)用程序內(nèi)購買用于購買額外內(nèi)容或應(yīng)用方面的升級(jí)功能。
涉及的步驟
1. 在iTunes連接,確保您擁有一個(gè)獨(dú)特的應(yīng)用程序ID和相應(yīng)的配置文件捆綁ID和代碼簽名在Xcode,當(dāng)我們創(chuàng)建應(yīng)用程序更新。
2. 創(chuàng)建一個(gè)新的應(yīng)用和更新應(yīng)用程序的信息??梢粤私膺@個(gè)蘋果添加新的應(yīng)用程序文檔。
3. 添加一個(gè)新的產(chǎn)品,在管理應(yīng)用程序內(nèi)購買在應(yīng)用程序的頁面應(yīng)用程序內(nèi)購買。
4.確保您設(shè)置銀行為您的應(yīng)用程序的詳細(xì)信息。這需要應(yīng)用程序內(nèi)購買的設(shè)置工作。還可以創(chuàng)建一個(gè)測(cè)試用戶帳戶使用管理用戶“選項(xiàng),應(yīng)用程序在iTunes連接頁面。
5. 接下來的步驟是處理代碼和創(chuàng)建用戶界面為我們的應(yīng)用程序內(nèi)購買。
6. 創(chuàng)建一個(gè)單獨(dú)視圖應(yīng)用程序并進(jìn)入包標(biāo)識(shí)符指定的標(biāo)識(shí)符在iTunes連接。
7. 更新 ViewController.xib 內(nèi)容如下所示:
8. 創(chuàng)建三個(gè)IBOutlets 標(biāo)簽的命名分別為productDescriptionLabel productTitleLabel,productPriceLabel 和 purchaseButton 按鈕。
9. 選擇項(xiàng)目文件,然后選擇目標(biāo),然后添加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 *productDescriptionLabel; 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 kTutorialYiibaiProductID @"com.tutorialYiibais.testApp.testProduct" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // Adding activity indicator activityIndicatorView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge]; activityIndicatorView.center = self.view.center; [activityIndicatorView hidesWhenStopped]; [self.view addSubview:activityIndicatorView]; [activityIndicatorView startAnimating]; //Hide purchase button initially purchaseButton.hidden = YES; [self fetchAvailableProducts]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } -(void)fetchAvailableProducts{ NSSet *productIdentifiers = [NSSet setWithObjects:kTutorialYiibaiProductID,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{ 上一篇:IOS - 文件處理下一篇:iOS - 位置處理