鍍金池/ 教程/ iOS/ iOS - 應(yīng)用程序內(nèi)購買
iOS - Switches(切換/開關(guān))
iOS - Labels(標(biāo)簽)
iOS - Table View(表格視圖)
IOS - 攝像頭管理
iOS - Text View(文本視圖)
IOS - 開發(fā)環(huán)境配置
iOS教程
iOS - Twitter & Facebook
iOS - UI元素
iOS - iAd 整合
IOS - 應(yīng)用程序調(diào)試
iOS - Split View(分割視圖)
iOS - Status Bar(狀態(tài)欄)
iOS - Navigation Bar(導(dǎo)航欄)
iOS - Tab bar(標(biāo)簽欄)
IOS - 文件處理
IOS - 自動(dòng)布局
iOS - Image View(圖像視圖)
iOS - 應(yīng)用程序內(nèi)購買
iOS - Pickers(選取器)
iOS - Delegates實(shí)例
iOS - 創(chuàng)建第一個(gè)iPhone應(yīng)用
iOS (iPhone, iPad)教程
iOS - 發(fā)送電子郵箱(Email)
iOS - View Transitions(視圖轉(zhuǎn)換)
iOS - 內(nèi)存管理
iOS - Icons(圖標(biāo))
iOS - 音頻和視頻
iOS - Storyboards(演示圖板演)
iOS - Buttons(按鈕)
iOS - Text Field(文本域)
iOS - Sliders(滑動(dòng)條)
iOS - Scroll View(滾動(dòng)視圖)
IOS - 輸入類型 文本字段
iOS - 位置處理
iOS - Accelerometer(加速度傳感器)
IOS - 快速入門
iOS - SQLite 數(shù)據(jù)庫
iOS - GameKit
IOS - 訪問地圖
iOS - Objective-C基礎(chǔ)
iOS - Toolbar(工具欄)
IOS - 動(dòng)作和插座(Outlets)
iOS - Alerts(警示)
IOS - 通用應(yīng)用程序

iOS - 應(yīng)用程序內(nèi)購買

介紹

應(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)容如下所示:

iOS Tutorial

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 - 位置處理