如何将应用内购买添加到 iOS 应用程序? [英] How do you add an in-app purchase to an iOS application?

查看:34
本文介绍了如何将应用内购买添加到 iOS 应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将应用内购买添加到 iOS 应用?所有细节是什么,是否有任何示例代码?

这是关于如何向 iOS 应用程序添加应用程序内购买的各种信息

解决方案

Swift 用户

Swift 用户可以查看我对此问题的 Swift 答案.
或者,查看 Yedidya Reiss 的回答,将这段 Objective-C 代码翻译成 Swift.

Objective-C 用户

这个答案的其余部分是用 Objective-C 编写的

应用商店连接

  1. 前往 appstoreconnect.apple.com 并登录
  2. 点击My Apps,然后点击您想要添加购买的应用
  3. 点击Features标题,然后选择左侧的In-App Purchases
  4. 点击中间的+图标
  5. 在本教程中,我们将添加应用内购买以移除广告,因此请选择 non-consumable.如果您要向用户发送实物,或者给他们一些他们可以多次购买的东西,您会选择consumable.
  6. 对于参考名称,输入任何你想要的(但要确保你知道它是什么)
  7. 对于产品 id 把 tld.websitename.appname.referencename 这将是最好的,例如,你可以使用 com.jojodmo.blix.removeads
  8. 选择cleared for sale,然后选择价格等级为 1 (99¢).第 2 层为 1.99 美元,第 3 层为 2.99 美元.如果您点击查看定价矩阵,可以查看完整列表.我建议您使用第 1 层,因为这通常是任何人为移除广告而支付的最高费用.
  9. 点击蓝色的添加语言按钮,输入信息.这一切都会显示给客户,所以不要放任何你不想让他们看到的东西
  10. 对于使用 Apple 托管内容,请选择
  11. 您可以将评论注释暂时留空.
  12. 跳过屏幕截图进行审查 现在,我们跳过的所有内容都会回来.
  13. 点击保存"

您的产品 ID 在 App Store Connect 中注册可能需要几个小时,所以请耐心等待.

设置您的项目

既然您已经在 App Store Connect 上设置了应用内购买信息,请进入您的 Xcode 项目,然后前往应用管理器(方法和头文件所在位置的顶部的蓝色页面状图标)) 在目标下单击您的应用程序(应该是第一个),然后转到常规.在底部,您应该看到 linked frameworks and libraries 单击小加号并添加框架 StoreKit.framework 如果您不这样做,则应用内购买将工作!

如果您使用 Objective-C 作为应用语言,则应该跳过这五个步骤.否则,如果您使用 Swift,您可以关注 My Swift Answer for this question, here,或者,如果您愿意将 Objective-C 用于应用内购买代码,但在您的应用中使用 Swift,您可以执行以下操作:

  1. 通过转至 File > 创建一个新的 .h(头文件)文件 >文件...(Command ⌘ + N).该文件将被称为您的 .h 文件".在本教程的其余部分

  2. 出现提示时,点击创建桥接头.这将是我们的桥接头文件.如果您没有提示,请转到第 3 步.如果您得到提示,请跳过第 3 步并直接转到第 4 步.

  3. 在主项目文件夹中创建另一个名为Bridge.h.h文件,然后转到应用程序管理器(蓝色页面状图标),然后在 Targets 部分选择您的应用,然后点击 Build Settings.找到显示 Swift Compiler - Code Generation 的选项,然后将 Objective-C Bridging Header 选项设置为 Bridge.h

  4. 在您的桥接头文件中,添加行 #import "MyObjectiveCHeaderFile.h",其中 MyObjectiveCHeaderFile 是您创建的头文件的名称在第一步中.因此,例如,如果您将头文件命名为 InAppPurchase.h,则需要将 #import InAppPurchase.h" 行添加到桥头文件中.>

  5. 通过转至 File 创建一个新的 Objective-C 方法 (.m) 文件 > >文件...(Command ⌘ + N).将其命名为您在第 1 步中创建的头文件.例如,如果您在第 1 步中调用该文件 InAppPurchase.h,您将调用这个新文件 InAppPurchase.mem>.该文件将被称为您的 .m 文件".在本教程的其余部分.

编码

现在我们将进入实际的编码.将以下代码添加到您的 .h 文件中:

BOOL areAdsRemoved;- (IBAction)恢复;- (IBAction)tapsRemoveAds;

接下来,您需要将 StoreKit 框架导入到您的 .m 文件中,并添加 SKProductsRequestDelegateSKPaymentTransactionObserver 在你的 @interface 声明之后:

#import //把你的视图控制器的名字代替 MyViewController@interface MyViewController() @结尾@implementation MyViewController//你的视图控制器的名字(同上)//下面的代码会加在这里@结尾

现在将以下内容添加到您的.m文件中,这部分变得复杂,所以我建议您阅读代码中的注释:

//如果你有多个内购,可以同时定义//他们在这里.因此,例如,您可以同时定义 kRemoveAdsProductIdentifier//和 kBuyCurrencyProductIdentifier 及其各自的产品 ID////对于这个例子,我们将只使用一种产品#define kRemoveAdsProductIdentifier @在此处输入您的产品 ID(我们刚刚在 App Store Connect 中制作的那个)";- (IBAction)tapsRemoveAds{NSLog(@用户请求删除广告");if([SKPaymentQueue canMakePayments]){NSLog(@用户可以付款");//如果您有多个应用内购买,并且想要//要让用户购买不同的产品,只需定义//另一个函数并将kRemoveAdsProductIdentifier替换为//其他产品的标识符SKProductsRequest *productsRequest = [[SKProductsRequest alloc] initWithProductIdentifiers:[NSSet setWithObject:kRemoveAdsProductIdentifier]];productsRequest.delegate = self;[产品请求开始];}别的{NSLog(@由于家长控制,用户无法付款");//这称为用户无法付款,很可能是由于家长控制}}- (void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response{SKProduct *validProduct = nil;int count = [response.products count];如果(计数> 0){validProduct = [response.products objectAtIndex:0];NSLog(@产品可用!");[自行购买:有效产品];}否则 if(!validProduct){NSLog(@没有可用的产品");//如果您的产品 ID 无效,则调用此方法,除非发生这种情况,否则不应调用此方法.}}- (void)purchase:(SKProduct *)product{SKPayment *payment = [SKPayment paymentWithProduct:product];[[SKPaymentQueue defaultQueue] addTransactionObserver:self];[[SKPaymentQueue defaultQueue] addPayment:payment];}- (IBAction) 恢复{//当用户恢复购买时调用它,你应该把它挂到一个按钮上[[SKPaymentQueue defaultQueue] addTransactionObserver:self];[[SKPaymentQueue defaultQueue] restoreCompletedTransactions];}- (void) paymentQueueRestoreCompletedTransactionsFinished:(SKPaymentQueue *)queue{NSLog(@"received restore transactions: %i", queue.transactions.count);for(SKPaymentTransaction *队列中的交易.transactions){if(transaction.transactionState == SKPaymentTransactionStateRestored){//当用户成功恢复购买时调用NSLog(@交易状态 -> 恢复");//如果您有多个应用内购买产品,//您为标识符恢复了正确的产品.//例如,你可以使用//if(productID == kRemoveAdsProductIdentifier)//获取产品标识符//恢复购买,可以使用////NSString *productID = transaction.payment.productIdentifier;[self doRemoveAds];[[SKPaymentQueue defaultQueue] finishTransaction:transaction];休息;}}}- (void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions{for(SKPaymentTransaction *交易中的交易){//如果您的应用中有多个应用内购买,//可以得到本次交易的商品标识//通过使用transaction.payment.productIdentifier////然后,根据产品ID检查标识符//您已定义以检查用户的产品//刚刚购买开关(事务.事务状态){case SKPaymentTransactionStatePurchasing: NSLog(@"Transaction state -> Purchasing");//在用户购买过程中调用,这里不要添加任何自己的代码.休息;案例 SKPaymentTransactionStatePurchased://当用户成功购买包裹时调用(Cha-Ching!)[self doRemoveAds];//您可以在此处添加用户购买时想要发生的代码,在本教程中,我们使用删除广告[[SKPaymentQueue defaultQueue] finishTransaction:transaction];NSLog(@交易状态 -> 已购买");休息;案例 SKPaymentTransactionStateRestored:NSLog(@交易状态 -> 恢复");//在此处添加与您从 SKPaymentTransactionStatePurchased 中所做的相同的代码[[SKPaymentQueue defaultQueue] finishTransaction:transaction];休息;案例 SKPaymentTransactionStateFailed://当事务没有完成时调用if(transaction.error.code == SKErrorPaymentCancelled){NSLog(@交易状态 -> 已取消");//用户取消付款;(}[[SKPaymentQueue defaultQueue] finishTransaction:transaction];休息;}}}

现在您要为用户完成交易时将发生的情况添加代码,在本教程中,我们使用删除添加,您必须为横幅视图加载时发生的情况添加自己的代码.

- (void)doRemoveAds{ADBannerView *banner;[横幅 setAlpha:0];areAdsRemoved = YES;removeAdsButton.hidden = YES;removeAdsButton.enabled = NO;[[NSUserDefaults standardUserDefaults] setBool:areAdsRemoved forKey:@areAdsRemoved"];//使用 NSUserDefaults 这样你就可以加载他们是否购买了它//最好使用KeyChain访问,或者更安全的东西//存储用户数据,因为 NSUserDefaults 可以改变.//你是普通的下载者不会很容易改变它,但是//最好还是使用比 NSUserDefaults 更安全的东西.//不过,出于本教程的目的,我们将使用 NSUserDefaults[[NSUserDefaults standardUserDefaults] 同步];}

如果您的应用程序中没有广告,您可以使用任何其他您想要的东西.例如,我们可以将背景的颜色设为蓝色.为此,我们需要使用:

- (void)doRemoveAds{[self.view setBackgroundColor:[UIColor blueColor]];areAdsRemoved = 是//将布尔值设置为他们是否购买了 YES,您可以在此处使用自己的布尔值,但您必须在 .h 文件中声明它[[NSUserDefaults standardUserDefaults] setBool:areAdsRemoved forKey:@areAdsRemoved"];//使用 NSUserDefaults 以便您可以加载他们是否购买它[[NSUserDefaults standardUserDefaults] 同步];}

现在,在您的 viewDidLoad 方法中的某处,您将要添加以下代码:

areAdsRemoved = [[NSUserDefaults standardUserDefaults] boolForKey:@areAdsRemoved"];[[NSUserDefaults standardUserDefaults] 同步];//这将加载他们是否购买了应用内购买如果(areAdsRemoved){[self.view setBackgroundColor:[UIColor blueColor]];//如果他们买了它,将背景设置为蓝色,如果您使用上面的代码将背景设置为蓝色,如果您删除广告,您将不得不在这里制作自己的代码}

现在您已经添加了所有代码,进入您的 .xibstoryboard 文件,并添加两个按钮,一个表示购买,另一个表示恢复.将 tapsRemoveAds IBAction 连接到您刚刚创建的购买按钮,将 restore IBAction 连接到恢复按钮.restore 操作将检查用户之前是否购买了应用内购买,如果他们还没有购买,则免费为他们提供应用内购买.

提交审核

接下来,进入App Store Connect,点击Users and Access,然后点击Sandbox Testers 标题,然后单击左侧显示 Testers+ 符号.您可以随意输入名字和姓氏的内容,并且电子邮件不必是真实的——您只需要能够记住它.输入密码(您必须记住)并填写其余信息.我建议您将 Date of Birth 设为使用户年满 18 岁的日期.App Store Territory 必须位于正确的国家/地区.接下来,退出您现有的 iTunes 帐户(您可以在完成本教程后重新登录).

现在,在您的 iOS 设备上运行您的应用程序,如果您尝试在模拟器上运行它,购买将总是错误,您必须在您的 iOS 上运行它设备.应用程序运行后,点击购买按钮.当系统提示您登录 iTunes 帐户时,请以我们刚刚创建的测试用户身份登录.接下来,当它要求您确认购买 99 美分或任何您设置的价格等级时,拍摄它的屏幕快照这就是您将用于屏幕截图以供审核的内容 在 App Store Connect 上.现在取消付款.

现在,转到App Store Connect,然后转到我的应用 >您在应用内购买的应用 >应用内购买.然后点击您的应用内购买,然后点击应用内购买详情下的编辑.完成后,将您刚在 iPhone 上拍摄的照片导入计算机,并将其作为屏幕截图上传以供审核,然后在审核笔记中输入您的测试用户电子邮件和密码.这将有助于苹果的审查过程.

完成此操作后,返回 iOS 设备上的应用程序,仍以测试用户帐户身份登录,然后单击购买按钮.这次,确认付款别担心,这不会向您的帐户收取任何费用,测试用户帐户免费获得所有应用内购买 确认付款后,请确保会发生什么当用户购买你的产品时实际发生.如果没有,那么您的 doRemoveAds 方法就会出错.同样,我建议使用将背景更改为蓝色来测试应用内购买,但这不应该是您实际的应用内购买.如果一切正常,您就可以开始了!当您将应用内购买上传到 App Store Connect 时,请确保将其包含在您的新二进制文件中!


以下是一些常见错误:

记录: 没有可用的产品

这可能意味着四件事:

  • 您没有在代码中输入正确的应用内购买 ID(对于上述代码中的标识符 kRemoveAdsProductIdentifier
  • 您没有清除在 App Store Connect
  • 上出售的应用内购买项目
  • 您没有等待应用内购买 ID 在 App Store Connect 中注册.创建 ID 后等待几个小时,您的问题应该会得到解决.
  • 您没有完成协议、税务和银行信息的填写.

如果第一次不起作用,请不要沮丧!不要放弃!我花了大约 5 个小时才开始工作,大约 10 个小时才能找到正确的代码!如果您完全使用上面的代码,它应该可以正常工作.如果您有任何问题,请随时发表评论.

我希望这对所有希望在其 iOS 应用程序中添加应用程序内购买的人有所帮助.干杯!

How do you add an in-app purchase to an iOS app? What are all the details and is there any sample code?

This is meant to be a catch-all of sorts for how to add in-app purchases to iOS apps

解决方案

Swift Users

Swift users can check out My Swift Answer for this question.
Or, check out Yedidya Reiss's Answer, which translates this Objective-C code to Swift.

Objective-C Users

The rest of this answer is written in Objective-C

App Store Connect

  1. Go to appstoreconnect.apple.com and log in
  2. Click My Apps then click the app you want do add the purchase to
  3. Click the Features header, and then select In-App Purchases on the left
  4. Click the + icon in the middle
  5. For this tutorial, we are going to be adding an in-app purchase to remove ads, so choose non-consumable. If you were going to send a physical item to the user, or give them something that they can buy more than once, you would choose consumable.
  6. For the reference name, put whatever you want (but make sure you know what it is)
  7. For product id put tld.websitename.appname.referencename this will work the best, so for example, you could use com.jojodmo.blix.removeads
  8. Choose cleared for sale and then choose price tier as 1 (99¢). Tier 2 would be $1.99, and tier 3 would be $2.99. The full list is available if you click view pricing matrix I recommend you use tier 1, because that's usually the most anyone will ever pay to remove ads.
  9. Click the blue add language button, and input the information. This will ALL be shown to the customer, so don't put anything you don't want them seeing
  10. For hosting content with Apple choose no
  11. You can leave the review notes blank FOR NOW.
  12. Skip the screenshot for review FOR NOW, everything we skip we will come back to.
  13. Click 'save'

It could take a few hours for your product ID to register in App Store Connect, so be patient.

Setting up your project

Now that you've set up your in-app purchase information on App Store Connect, go into your Xcode project, and go to the application manager (blue page-like icon at the top of where your methods and header files are) click on your app under targets (should be the first one) then go to general. At the bottom, you should see linked frameworks and libraries click the little plus symbol and add the framework StoreKit.framework If you don't do this, the in-app purchase will NOT work!

If you are using Objective-C as the language for your app, you should skip these five steps. Otherwise, if you are using Swift, you can follow My Swift Answer for this question, here, or, if you prefer to use Objective-C for the In-App Purchase code but are using Swift in your app, you can do the following:

  1. Create a new .h (header) file by going to File > New > File... (Command ⌘ + N). This file will be referred to as "Your .h file" in the rest of the tutorial

  2. When prompted, click Create Bridging Header. This will be our bridging header file. If you are not prompted, go to step 3. If you are prompted, skip step 3 and go directly to step 4.

  3. Create another .h file named Bridge.h in the main project folder, Then go to the Application Manager (the blue page-like icon), then select your app in the Targets section, and click Build Settings. Find the option that says Swift Compiler - Code Generation, and then set the Objective-C Bridging Header option to Bridge.h

  4. In your bridging header file, add the line #import "MyObjectiveCHeaderFile.h", where MyObjectiveCHeaderFile is the name of the header file that you created in step one. So, for example, if you named your header file InAppPurchase.h, you would add the line #import "InAppPurchase.h" to your bridge header file.

  5. Create a new Objective-C Methods (.m) file by going to File > New > File... (Command ⌘ + N). Name it the same as the header file you created in step 1. For example, if you called the file in step 1 InAppPurchase.h, you would call this new file InAppPurchase.m. This file will be referred to as "Your .m file" in the rest of the tutorial.

Coding

Now we're going to get into the actual coding. Add the following code into your .h file:

BOOL areAdsRemoved;

- (IBAction)restore;
- (IBAction)tapsRemoveAds;

Next, you need to import the StoreKit framework into your .m file, as well as add SKProductsRequestDelegate and SKPaymentTransactionObserver after your @interface declaration:

#import <StoreKit/StoreKit.h>

//put the name of your view controller in place of MyViewController
@interface MyViewController() <SKProductsRequestDelegate, SKPaymentTransactionObserver>

@end

@implementation MyViewController //the name of your view controller (same as above)
  //the code below will be added here
@end

and now add the following into your .m file, this part gets complicated, so I suggest that you read the comments in the code:

//If you have more than one in-app purchase, you can define both of
//of them here. So, for example, you could define both kRemoveAdsProductIdentifier
//and kBuyCurrencyProductIdentifier with their respective product ids
//
//for this example, we will only use one product

#define kRemoveAdsProductIdentifier @"put your product id (the one that we just made in App Store Connect) in here"

- (IBAction)tapsRemoveAds{
    NSLog(@"User requests to remove ads");

    if([SKPaymentQueue canMakePayments]){
        NSLog(@"User can make payments");
    
        //If you have more than one in-app purchase, and would like
        //to have the user purchase a different product, simply define 
        //another function and replace kRemoveAdsProductIdentifier with 
        //the identifier for the other product

        SKProductsRequest *productsRequest = [[SKProductsRequest alloc] initWithProductIdentifiers:[NSSet setWithObject:kRemoveAdsProductIdentifier]];
        productsRequest.delegate = self;
        [productsRequest start];
    
    }
    else{
        NSLog(@"User cannot make payments due to parental controls");
        //this is called the user cannot make payments, most likely due to parental controls
    }
}

- (void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response{
    SKProduct *validProduct = nil;
    int count = [response.products count];
    if(count > 0){
        validProduct = [response.products objectAtIndex:0];
        NSLog(@"Products Available!");
        [self purchase:validProduct];
    }
    else if(!validProduct){
        NSLog(@"No products available");
        //this is called if your product id is not valid, this shouldn't be called unless that happens.
    }
}

- (void)purchase:(SKProduct *)product{
    SKPayment *payment = [SKPayment paymentWithProduct:product];

    [[SKPaymentQueue defaultQueue] addTransactionObserver:self];
    [[SKPaymentQueue defaultQueue] addPayment:payment];
}

- (IBAction) restore{
    //this is called when the user restores purchases, you should hook this up to a button
    [[SKPaymentQueue defaultQueue] addTransactionObserver:self];
    [[SKPaymentQueue defaultQueue] restoreCompletedTransactions];
}

- (void) paymentQueueRestoreCompletedTransactionsFinished:(SKPaymentQueue *)queue
{
    NSLog(@"received restored transactions: %i", queue.transactions.count);
    for(SKPaymentTransaction *transaction in queue.transactions){
        if(transaction.transactionState == SKPaymentTransactionStateRestored){
            //called when the user successfully restores a purchase
            NSLog(@"Transaction state -> Restored");

            //if you have more than one in-app purchase product,
            //you restore the correct product for the identifier.
            //For example, you could use
            //if(productID == kRemoveAdsProductIdentifier)
            //to get the product identifier for the
            //restored purchases, you can use
            //
            //NSString *productID = transaction.payment.productIdentifier;
            [self doRemoveAds];
            [[SKPaymentQueue defaultQueue] finishTransaction:transaction];
            break;
        }
    }   
}

- (void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions{
    for(SKPaymentTransaction *transaction in transactions){
        //if you have multiple in app purchases in your app,
        //you can get the product identifier of this transaction
        //by using transaction.payment.productIdentifier
        //
        //then, check the identifier against the product IDs
        //that you have defined to check which product the user
        //just purchased            

        switch(transaction.transactionState){
            case SKPaymentTransactionStatePurchasing: NSLog(@"Transaction state -> Purchasing");
                //called when the user is in the process of purchasing, do not add any of your own code here.
                break;
            case SKPaymentTransactionStatePurchased:
            //this is called when the user has successfully purchased the package (Cha-Ching!)
                [self doRemoveAds]; //you can add your code for what you want to happen when the user buys the purchase here, for this tutorial we use removing ads
                [[SKPaymentQueue defaultQueue] finishTransaction:transaction];
                NSLog(@"Transaction state -> Purchased");
                break;
            case SKPaymentTransactionStateRestored:
                NSLog(@"Transaction state -> Restored");
                //add the same code as you did from SKPaymentTransactionStatePurchased here
                [[SKPaymentQueue defaultQueue] finishTransaction:transaction];
                break;
            case SKPaymentTransactionStateFailed:
                //called when the transaction does not finish
                if(transaction.error.code == SKErrorPaymentCancelled){
                    NSLog(@"Transaction state -> Cancelled");
                    //the user cancelled the payment ;(
                }
                [[SKPaymentQueue defaultQueue] finishTransaction:transaction];
                break;
        }
    }
}

Now you want to add your code for what will happen when the user finishes the transaction, for this tutorial, we use removing adds, you will have to add your own code for what happens when the banner view loads.

- (void)doRemoveAds{
    ADBannerView *banner;
    [banner setAlpha:0];
    areAdsRemoved = YES;
    removeAdsButton.hidden = YES;
    removeAdsButton.enabled = NO;
    [[NSUserDefaults standardUserDefaults] setBool:areAdsRemoved forKey:@"areAdsRemoved"];
    //use NSUserDefaults so that you can load whether or not they bought it
    //it would be better to use KeyChain access, or something more secure
    //to store the user data, because NSUserDefaults can be changed.
    //You're average downloader won't be able to change it very easily, but
    //it's still best to use something more secure than NSUserDefaults.
    //For the purpose of this tutorial, though, we're going to use NSUserDefaults
    [[NSUserDefaults standardUserDefaults] synchronize];
}

If you don't have ads in your application, you can use any other thing that you want. For example, we could make the color of the background blue. To do this we would want to use:

- (void)doRemoveAds{
    [self.view setBackgroundColor:[UIColor blueColor]];
    areAdsRemoved = YES
    //set the bool for whether or not they purchased it to YES, you could use your own boolean here, but you would have to declare it in your .h file

    [[NSUserDefaults standardUserDefaults] setBool:areAdsRemoved forKey:@"areAdsRemoved"];
    //use NSUserDefaults so that you can load wether or not they bought it
    [[NSUserDefaults standardUserDefaults] synchronize];
}

Now, somewhere in your viewDidLoad method, you're going to want to add the following code:

areAdsRemoved = [[NSUserDefaults standardUserDefaults] boolForKey:@"areAdsRemoved"];
[[NSUserDefaults standardUserDefaults] synchronize];
//this will load wether or not they bought the in-app purchase

if(areAdsRemoved){
    [self.view setBackgroundColor:[UIColor blueColor]];
    //if they did buy it, set the background to blue, if your using the code above to set the background to blue, if your removing ads, your going to have to make your own code here
}

Now that you have added all the code, go into your .xib or storyboard file, and add two buttons, one saying purchase, and the other saying restore. Hook up the tapsRemoveAds IBAction to the purchase button that you just made, and the restore IBAction to the restore button. The restore action will check if the user has previously purchased the in-app purchase, and give them the in-app purchase for free if they do not already have it.

Submitting for review

Next, go into App Store Connect, and click Users and Access then click the Sandbox Testers header, and then click the + symbol on the left where it says Testers. You can just put in random things for the first and last name, and the e-mail does not have to be real - you just have to be able to remember it. Put in a password (which you will have to remember) and fill in the rest of the info. I would recommend that you make the Date of Birth a date that would make the user 18 or older. App Store Territory HAS to be in the correct country. Next, log out of your existing iTunes account (you can log back in after this tutorial).

Now, run your application on your iOS device, if you try running it on the simulator, the purchase will always error, you HAVE TO run it on your iOS device. Once the app is running, tap the purchase button. When you are prompted to log into your iTunes account, log in as the test user that we just created. Next,when it asks you to confirm the purchase of 99¢ or whatever you set the price tier too, TAKE A SCREEN SNAPSHOT OF IT this is what your going to use for your screenshot for review on App Store Connect. Now cancel the payment.

Now, go to App Store Connect, then go to My Apps > the app you have the In-app purchase on > In-App Purchases. Then click your in-app purchase and click edit under the in-app purchase details. Once you've done that, import the photo that you just took on your iPhone into your computer, and upload that as the screenshot for review, then, in review notes, put your TEST USER e-mail and password. This will help apple in the review process.

After you have done this, go back onto the application on your iOS device, still logged in as the test user account, and click the purchase button. This time, confirm the payment Don't worry, this will NOT charge your account ANY money, test user accounts get all in-app purchases for free After you have confirmed the payment, make sure that what happens when the user buys your product actually happens. If it doesn't, then thats going to be an error with your doRemoveAds method. Again, I recommend using changing the background to blue for testing the in-app purchase, this should not be your actual in-app purchase though. If everything works and you're good to go! Just make sure to include the in-app purchase in your new binary when you upload it to App Store Connect!


Here are some common errors:

Logged: No Products Available

This could mean four things:

  • You didn't put the correct in-app purchase ID in your code (for the identifier kRemoveAdsProductIdentifier in the above code
  • You didn't clear your in-app purchase for sale on App Store Connect
  • You didn't wait for the in-app purchase ID to be registered in App Store Connect. Wait a couple hours from creating the ID, and your problem should be resolved.
  • You didn't complete filling your Agreements, Tax, and Banking info.

If it doesn't work the first time, don't get frustrated! Don't give up! It took me about 5 hours straight before I could get this working, and about 10 hours searching for the right code! If you use the code above exactly, it should work fine. Feel free to comment if you have any questions at all.

I hope this helps to all of those hoping to add an in-app purchase to their iOS application. Cheers!

这篇关于如何将应用内购买添加到 iOS 应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆