iOS - 應用程序內購買
介紹
應用程序內購買用於購買額外內容或應用方面的升級功能。
涉及的步驟
1. 在iTunes連接,確保您擁有一個獨特的應用程序ID和相應的配置文件捆綁ID和代碼簽名在Xcode,當我們創建應用程序更新。
2. 創建一個新的應用和更新應用程序的信息。可以瞭解這個蘋果添加新的應用程序文檔。
3. 添加一個新的產品,在管理應用程序內購買在應用程序的頁面應用程序內購買。
4.確保您設置銀行爲您的應用程序的詳細信息。這需要應用程序內購買的設置工作。還可以創建一個測試用戶帳戶使用管理用戶「選項,應用程序在iTunes連接頁面。
5. 接下來的步驟是處理代碼和創建用戶界面爲我們的應用程序內購買。
6. 創建一個單獨視圖應用程序並進入包標識符指定的標識符在iTunes連接。
7. 更新 ViewController.xib 內容如下所示:
8. 創建三個IBOutlets 標籤的命名分別爲productDescriptionLabel productTitleLabel,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 *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{ [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:kTutorialYiibaiProductID]) { 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:kTutorialYiibaiProductID]) { [productTitleLabel setText:[NSString stringWithFormat: @"Product Title: %@",validProduct.localizedTitle]]; [productDescriptionLabel setText:[NSString stringWithFormat: @"Product Desc: %@",validProduct.localizedDescription]]; [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
注意事項:
創建在應用程序內購買的ProductID更新kTutorialYiibaiProductID。可以添加一個以上的產品通過更新productIdentifiers的NSSet fetchAvailableProducts。同樣處理產品ID添加購買相關動作。
輸出
現在,當我們運行程序時,我們會得到下面的輸出。
請確定登錄您的帳戶設置屏幕。在點擊啓動購買請選擇「使用現有的Apple ID。輸入您的有效的測試帳戶的用戶名和密碼。在幾秒鐘內將顯示以下警告。
所購買的產品一旦成功,會得到以下警示。可以更新顯示此警報的應用程序功能的相關代碼。