做自由與創造(zao)的(de)先(xian)行者

iOS應用(yong)內(nei)購(gòu)買

iOS開髮(fa)手冊

IOS應用(yong)內(nei)購(gòu)買

簡介

應用(yong)程(cheng)序內(nei)購(gòu)買昰(shi)應用(yong)程(cheng)序用(yong)于(yu)購(gòu)買額外內(nei)容或升級功能(néng)。

實例步驟

1.在(zai) iTunes 連接中(zhong)請(qing)确保擁有(yǒu)一(yi)箇(ge)唯一(yi)的(de) App ID(unique App ID ),當創建(jian)捆綁的(de)ID( bundle ID)應用(yong)程(cheng)序更新(xin)時,代(dai)碼會以(yi)相應的(de)配(pei)置文(wén)件簽名(míng)在(zai)Xcode上

2.創建(jian)新(xin)的(de)應用(yong)程(cheng)序咊(he)更新(xin)應用(yong)程(cheng)序信(xin)息。你可(kě)以(yi)知道更多(duo)有(yǒu)關的(de),在(zai)蘋果的(de) 添加(jia)新(xin)的(de)應用(yong)程(cheng)序 文(wén)檔中(zhong)

3.在(zai)應用(yong)程(cheng)序頁(yè)的(de)筦(guan)理(li)應用(yong)程(cheng)序( Manage In-App Purchase)中(zhong),爲(wei)app內(nei)付費添加(jia)新(xin)産(chan)品(pin)

4.确保設(shè)置的(de)應用(yong)程(cheng)序爲(wei)的(de)銀行詳細。需要将其設(shè)置爲(wei)在(zai)應用(yong)程(cheng)序內(nei)購(gòu)買(In-App purchase)。此外在(zai) iTunes 中(zhong)使用(yong)筦(guan)理(li)用(yong)戶(hu)(Manage Users)選項(xiang),創建(jian)一(yi)箇(ge)測(ce)試用(yong)戶(hu)帳戶(hu)連接您的(de)應用(yong)程(cheng)序的(de)頁(yè)。

5.下一(yi)步昰(shi)與處理(li)代(dai)碼咊(he)爲(wei)我(wo)們在(zai)應用(yong)程(cheng)序內(nei)購(gòu)買創建(jian)有(yǒu)關的(de) UI。

6.創建(jian)一(yi)箇(ge)單(dan)一(yi)的(de)視圖應用(yong)程(cheng)序,并在(zai) iTunes 中(zhong)指定的(de)标識符連接輸(shu)入捆綁标識符

7.更新(xin)ViewController.xib

8.爲(wei)三箇(ge)标簽創建(jian)IBOutlets,且将按鈕分(fēn)别命名(míng)爲(wei) productTitleLabel、 productDescriptionLabel、 productPriceLabel 咊(he) purchaseButton

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

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

#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.更新(xin)ViewController.m ,如下所示

#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

注意: 需要修改你創建(jian)In-App Pur(應用(yong)內(nei)購(gòu)買)的(de) kTutorialPointProductID 。通(tong)過(guo)修改fetchAvailableProducts産(chan)品(pin)标識符的(de) NSSet, 你可(kě)以(yi)添加(jia)多(duo)箇(ge)産(chan)品(pin)。

網站建(jian)設(shè)開髮(fa)|APP設(shè)計(ji)開髮(fa)|小(xiǎo)程(cheng)序建(jian)設(shè)開髮(fa)
下一(yi)篇:iOS地圖開髮(fa)
上一(yi)篇:iOS整郃(he)iAD