做自(zì)由與創造的先行者

iOS應用(yòng)内購買

iOS開(kāi)發手冊

IOS應用(yòng)内購買

簡介

應用(yòng)程序内購買是應用(yòng)程序用(yòng)于購買額外(wài)内容或升級功能(néng)。

實例步驟

1.在 iTunes 連接中請(qǐng)确保擁有一個唯一的 App ID(unique App ID ),當創建捆綁的ID( bundle ID)應用(yòng)程序更新時(shí),代碼會(huì)以相應的配置文(wén)件簽名在Xcode上(shàng)

2.創建新的應用(yòng)程序和(hé)更新應用(yòng)程序信息。你(nǐ)可以知(zhī)道(dào)更多有關的,在蘋果的 添加新的應用(yòng)程序 文(wén)檔中

3.在應用(yòng)程序頁的管理(lǐ)應用(yòng)程序( Manage In-App Purchase)中,爲app内付費添加新産品

4.确保設置的應用(yòng)程序爲的銀行詳細。需要将其設置爲在應用(yòng)程序内購買(In-App purchase)。此外(wài)在 iTunes 中使用(yòng)管理(lǐ)用(yòng)戶(Manage Users)選項,創建一個測試用(yòng)戶帳戶連接您的應用(yòng)程序的頁。

5.下(xià)一步是與處理(lǐ)代碼和(hé)爲我們在應用(yòng)程序内購買創建有關的 UI。

6.創建一個單一的視(shì)圖應用(yòng)程序,并在 iTunes 中指定的标識符連接輸入捆綁标識符

7.更新ViewController.xib

8.爲三個标簽創建IBOutlets,且将按鈕分别命名爲 productTitleLabel、 productDescriptionLabel、 productPriceLabel 和(hé) purchaseButton

9.選擇項目文(wén)件,然後選擇目标,然後添加StoreKit.framework

10.更新ViewController.h ,如下(xià)所示

#import

#import

@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 ,如下(xià)所示

#import "ViewController.h"

#define kTutorialPointProductID

@"com.tutorialPoints.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: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]];

[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

注意: 需要修改你(nǐ)創建In-App Pur(應用(yòng)内購買)的 kTutorialPointProductID 。通過修改fetchAvailableProducts産品标識符的 NSSet, 你(nǐ)可以添加多個産品。

網站(zhàn)建設開(kāi)發|APP設計(jì)開(kāi)發|小(xiǎo)程序建設開(kāi)發
下(xià)一篇:iOS地圖開(kāi)發
上(shàng)一篇:iOS整合iAD