IOS应用内购买


简介

应用程序内购买是应用程序用于购买额外内容或升级功能。

实例步骤

1.在 iTunes 连接中请确保拥有一个唯一的 App ID(unique App ID ),当创建捆绑的ID( bundle ID)应用程序更新时,代码会以相应的配置文件签名在Xcode上

2.创建新的应用程序和更新应用程序信息。你可以知道更多有关的,在苹果的 添加新的应用程序 文档中

3.在应用程序页的管理应用程序( Manage In-App Purchase)中,为app内付费添加新产品

4.确保设置的应用程序为的银行详细。需要将其设置为在应用程序内购买(In-App purchase)。此外在 iTunes 中使用管理用户(Manage Users)选项,创建一个测试用户帐户连接您的应用程序的页。

5.下一步是与处理代码和为我们在应用程序内购买创建有关的 UI。

6.创建一个单一的视图应用程序,并在 iTunes 中指定的标识符连接输入捆绑标识符

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

InAppPurchase_OutputInterface

8.为三个标签创建IBOutlets,且将按钮分别命名为 productTitleLabel、 productDescriptionLabel、 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 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

注意:需要修改你创建In-App Pur(应用内购买)的 kTutorialPointProductID 。通过修改fetchAvailableProducts产品标识符的 NSSet, 你可以添加多个产品。

输出

运行该应用程序,输出结果如下

InAppPurchase_Output1

确保已经中登录。单击购买选择现有的Apple ID。输入有效的测试帐户的用户名和密码。几秒钟后,显示下面的信息

InAppPurchase_Output2

一旦产品成功购买,将获得以下信息。可以在显示此信息的地方,更新应用功能相关的代码

InAppPurchase_Output3

若文章对你有帮助,可以点赞或打赏支持我们。发布者:lyh会员,转载请注明出处:http://61.174.243.28:13541/AY-knowledg-hub/ios%e5%ba%94%e7%94%a8%e5%86%85%e8%b4%ad%e4%b9%b0/

(0)
lyhlyh会员认证作者
上一篇 2023年 4月 1日 下午8:57
下一篇 2023年 4月 1日 下午9:06

相关推荐

  • pv

    文章目录pv补充说明下载安装语法选项实例注意 pv 显示当前在命令行执行的命令的进度信息,管道查看器 补充说明 pv命令 Pipe Viewer 的简称,由Andrew Wood …

    入门教程 2024年 3月 1日
  • poweroff

    文章目录poweroff补充说明语法选项例子 poweroff 关闭Linux系统,关闭记录会被写入到/var/log/wtmp日志文件中 补充说明 grename命令 可以重命名…

    入门教程 2024年 3月 1日
  • 变量

    文章目录变量什么是变量声明单一变量声明一个带初值的变量类型推导多变量声明简短声明 变量 上一节:第二篇 Hello World下一节:第四篇 类型 这是本Golang系列教程的第三…

    2023年 12月 5日
  • badblocks

    文章目录badblocks补充说明语法选项参数实例其他 badblocks 查找磁盘中损坏的区块 补充说明 badblock命令 用于查找磁盘中损坏的区块。 硬盘是一个损耗设备,当…

    入门教程 2023年 12月 6日
  • ssh-agent

    文章目录ssh-agent补充说明语法选项实例 ssh-agent ssh密钥管理器 补充说明 ssh-agent命令 是一种控制用来保存公钥身份验证所使用的私钥的程序。ssh-a…

    入门教程 2024年 3月 11日
  • volname

    文章目录volname补充说明语法参数 volname 显示指定的ISO-9660格式的设备的卷名称 补充说明 volname命令 用于显示指定的“ISO-9660”格式的设备的卷…

    入门教程 2024年 3月 11日
  • uniq

    文章目录uniq概要主要用途选项参数返回值例子注意 uniq 显示或忽略重复的行。 概要 uniq [OPTION]… [INPUT [OUTPUT]] 主要用途 将输入文件(…

    入门教程 2024年 3月 11日
  • lpc

    文章目录lpc补充说明语法实例 lpc 命令行方式打印机控制程序 补充说明 lpc命令 式命令行方式打印机控制程序,有5个内置命令。 语法 lpc 实例 [root@localho…

    入门教程 2023年 12月 19日
  • hostname

    文章目录hostname补充说明语法选项实例 hostname 显示和设置系统的主机名 补充说明 hostname命令用于显示和设置系统的主机名称。 环境变量 HOSTNAME 也…

    入门教程 2023年 12月 15日
  • gcc

    文章目录gcc补充说明语法选项参数实例 gcc 基于C/C++的编译器 补充说明 gcc命令 使用GNU推出的基于 C/C++ 的编译器,是开放源代码领域应用最广泛的编译器,具有功…

    入门教程 2023年 12月 14日
Translate »