多多色-多人伦交性欧美在线观看-多人伦精品一区二区三区视频-多色视频-免费黄色视屏网站-免费黄色在线

中國最全I(xiàn)T社區(qū)平臺(tái) 聯(lián)系我們 | 收藏本站
阿里云優(yōu)惠2

ios教程

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

閱讀 (2145)

IOS應(yīng)用內(nèi)購買


簡(jiǎn)介

應(yīng)用程序內(nèi)購買是應(yīng)用程序用于購買額外內(nèi)容或升級(jí)功能。

實(shí)例步驟

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

2.創(chuàng)建新的應(yīng)用程序和更新應(yīng)用程序信息。你可以知道更多有關(guān)的,在蘋果的 添加新的應(yīng)用程序 文檔中

3.在應(yīng)用程序頁的管理應(yīng)用程序( Manage In-App Purchase)中,為app內(nèi)付費(fèi)添加新產(chǎn)品

4.確保設(shè)置的應(yīng)用程序?yàn)榈你y行詳細(xì)。需要將其設(shè)置為在應(yīng)用程序內(nèi)購買(In-App purchase)。此外在 iTunes 中使用管理用戶(Manage Users)選項(xiàng),創(chuàng)建一個(gè)測(cè)試用戶帳戶連接您的應(yīng)用程序的頁。

5.下一步是與處理代碼和為我們?cè)趹?yīng)用程序內(nèi)購買創(chuàng)建有關(guān)的 UI。

6.創(chuàng)建一個(gè)單一的視圖應(yīng)用程序,并在 iTunes 中指定的標(biāo)識(shí)符連接輸入捆綁標(biāo)識(shí)符

7.更新ViewController.xib ,如下所示

InAppPurchase_OutputInterface

8.為三個(gè)標(biāo)簽創(chuàng)建IBOutlets,且將按鈕分別命名為 productTitleLabel、 productDescriptionLabel、 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 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

注意: 需要修改你創(chuàng)建In-App Pur(應(yīng)用內(nèi)購買)的 kTutorialPointProductID 。通過修改fetchAvailableProducts產(chǎn)品標(biāo)識(shí)符的 NSSet, 你可以添加多個(gè)產(chǎn)品。

輸出

運(yùn)行該應(yīng)用程序,輸出結(jié)果如下

InAppPurchase_Output1

確保已經(jīng)中登錄。單擊購買選擇現(xiàn)有的Apple ID。輸入有效的測(cè)試帳戶的用戶名和密碼。幾秒鐘后,顯示下面的信息

InAppPurchase_Output2

一旦產(chǎn)品成功購買,將獲得以下信息。可以在顯示此信息的地方,更新應(yīng)用功能相關(guān)的代碼

InAppPurchase_Output3

關(guān)閉
程序員人生
主站蜘蛛池模板: 高清午夜线观看免费 | 九色国产在线 | 日韩图区 | 亚洲人成在线观看男人自拍 | 国产日韩欧美一区二区三区综合 | 都市 校园 春色 亚洲 | 亚洲欧美日韩网站 | 国产中文字幕在线播放 | 亚洲天堂久久 | 欧美最猛性xxxxx短视频 | 福利片中文 | 久操欧美 | 欧美5o老妇性xxx | 激情专区 | 日韩欧美中文字幕一区 | 亚洲综合小说久久另类区 | 国产一级毛片国语普通话对白 | 免费xxxxx在线观看网站 | 中国高清色视频www 中国国产成人精品久久 | 网址黄| 能免费看的黄色网址 | 久久精品久 | 91久色视频| 亚洲国产精品免费在线观看 | 欧美精品 日韩 | 逼逼网| 亚洲精品国产字幕久久不卡 | 中文字幕无线码中文字幕免费 | 国产亚洲精品自在久久不卡 | 羞羞免费网站在线 | 欧美男人天堂网 | 久久男女| h网站免费看 | 伊人网在线免费视频 | 最近最新的中文字幕大全3 最近最新高清免费中文字幕 | 69视频网址| 在线播放免费人成毛片乱码 | 999毛片免费观看 | 欧美激情精品久久久久久久久久 | 日本三级成人中文字幕乱码 | 国产jizzjizz免费看麻豆 |