• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

objective-c - 为应用内购买实现一种以上的购买类型

[复制链接]
菜鸟教程小白 发表于 2022-12-13 13:48:39 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

这是根据建议使用 Apple 文档编辑的表单。 我用 2 个独立的 IBAction 创建了 2 个独立的按钮。

UIButton *buyCredit1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    buyCredit1.frame = scrollViewFrame;
    [buyCredit1 setTitle"A bundle of 10 credits - 99¢" forState:UIControlStateNormal];
    buyCredit1.tag = 10;
    [scrollView addSubview:buyCredit1];
    [buyCredit1 addTarget:self
                   actionselector(purchase10credit
         forControlEvents:UIControlEventTouchUpInside];

UIButton *buyCredit2 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    buyCredit2.frame = scrollViewFrame;
    [buyCredit2 setTitle"A bundle of 30 credits - 1.99¢" forState:UIControlStateNormal];
    buyCredit2.tag = 30;
    [scrollView addSubview:buyCredit2];
    [buyCredit2 addTarget:self
                   actionselector(purchase30credit
         forControlEvents:UIControlEventTouchUpInside];

-(IBAction)purchase10creditid)sender{
    SKMutablePayment *payment = [[SKMutablePayment alloc] init];
    payment.productIdentifier = @"Bundle.10.credits";

    [[SKPaymentQueue defaultQueue] addPayment:payment];
    [[SKPaymentQueue defaultQueue] addTransactionObserver:self];
    [[SKPaymentQueue defaultQueue] addPayment:payment];
}
-(IBAction)purchase30creditid)sender{
    SKMutablePayment *payment = [[SKMutablePayment alloc] init];
    payment.productIdentifier = @"Bundle.30.credits";

    [[SKPaymentQueue defaultQueue] addPayment:payment];
    [[SKPaymentQueue defaultQueue] addTransactionObserver:self];
    [[SKPaymentQueue defaultQueue] addPayment:payment];
}
-(void) productsRequestSKProductsRequest *)request didReceiveResponseSKProductsResponse *)response{
    SKProduct *validProduct = nil;
    int count = [response.products count];
    if (count > 0) {
        validProduct = [response.products objectAtIndex:0];
    }
    else if (!validProduct) {
        NSLog(@"No Products Available");
    }
}

这是 Apple 反射(reflect)的应用内购买文档

-(void) productsRequestSKProductsRequest *)request didReceiveResponseSKProductsResponse *)response{
    SKProduct *validProduct = nil;
    int count = [response.products count];
    if (count > 0) {
        validProduct = [response.products objectAtIndex:0];
    }
    else if (!validProduct) {
        NSLog(@"No Products Available");
    }
}
- (void)paymentQueueSKPaymentQueue *)queue updatedTransactionsNSArray *)transactions
{
    for (SKPaymentTransaction *transaction in transactions)
    {
        switch (transaction.transactionState)
        {
            case SKPaymentTransactionStatePurchased:
                [self completeTransaction:transaction];
                break;
            case SKPaymentTransactionStateFailed:
                [self failedTransaction:transaction];
                break;
            case SKPaymentTransactionStateRestored:
                [self restoreTransaction:transaction];
            default:
                break;
        }
    }
}

- (void) updateCredit: (NSString *)productIdentifier { 
    //Adding to plist
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0]; 
    NSString *path = [documentsDirectory stringByAppendingPathComponent"data.plist"]; 

    NSMutableArray *currPlist = [[NSMutableArray alloc] initWithContentsOfFile: path];

    NSString *lastEx = [currPlist objectAtIndex:0];
    int lastScore = [[currPlist objectAtIndex:1] intValue];
    int numberOfTries = [[currPlist objectAtIndex:2] intValue];
    int totalScore = [[currPlist objectAtIndex:3] intValue];
    int avg = [[currPlist objectAtIndex:4] intValue];
    int credit = [[currPlist objectAtIndex:5] intValue];

    credit += 10;


    NSString *currentCredit = [NSString stringWithFormat"%d credits",credit];
    creditShow.text = currentCredit;

    NSMutableArray *updatePlist = [[NSMutableArray alloc] init];
    [updatePlist addObject:lastEx];
    [updatePlist addObject:[NSNumber numberWithInt:lastScore]];
    [updatePlist addObject:[NSNumber numberWithInt:numberOfTries]];
    [updatePlist addObject:[NSNumber numberWithInt:totalScore]];
    [updatePlist addObject:[NSNumber numberWithInt:avg]];
    [updatePlist addObject:[NSNumber numberWithInt:credit]];
    [updatePlist writeToFile: path atomically:YES]; 
}

- (void) completeTransaction: (SKPaymentTransaction *)transaction
{
    // Your application should implement these two methods.
    [self updateCredit:transaction.payment.productIdentifier];

    // Remove the transaction from the payment queue.
    [[SKPaymentQueue defaultQueue] finishTransaction: transaction];
}


- (void) restoreTransaction: (SKPaymentTransaction *)transaction
{

    [[SKPaymentQueue defaultQueue] finishTransaction: transaction];
}


- (void) failedTransaction: (SKPaymentTransaction *)transaction
{
    if (transaction.error.code != SKErrorPaymentCancelled) {
        // Optionally, display an error here.
    }
    [[SKPaymentQueue defaultQueue] finishTransaction: transaction];
}

在 - (void) updateCredit: (NSString *)productIdentifier 中,我如何创建 2 个单独的信用更新? 1 个积分 += 10(购买 0.99 美分,积分 += 30(1.99 美元)?



Best Answer-推荐答案


检查 purchase: 中的 (id)sender 并确定按下了哪个按钮(您可以为按钮设置标签属性,例如 tag=1 表示 10 个积分,tag=5 表示50 学分)。然后您可以设置另一个应用内购买:

UIButton *tempButton = (UIButton *)sender;
if (tempButton.tag == 1)
payment.productIdentifier = @"Bundle.10.credits";
else
payment.productIdentifier = @"Bundle.50.credits";

至于更新状态是推荐的方式:

- (void)paymentQueueSKPaymentQueue *)queue updatedTransactionsNSArray *)transactions
{
    for (SKPaymentTransaction *transaction in transactions)
    {
        switch (transaction.transactionState)
        {
            case SKPaymentTransactionStatePurchased:
                [self completeTransaction:transaction];
                break;
            case SKPaymentTransactionStateFailed:
                [self failedTransaction:transaction];
                break;
            case SKPaymentTransactionStateRestored:
                [self restoreTransaction:transaction];
            default:
                break;
        }
    }
}


- (void) completeTransaction: (SKPaymentTransaction *)transaction
{
    // Your application should implement these two methods.
    [self recordTransaction:transaction];
    [self provideContent:transaction.payment.productIdentifier];

    // Remove the transaction from the payment queue.
    [[SKPaymentQueue defaultQueue] finishTransaction: transaction];
}


- (void) restoreTransaction: (SKPaymentTransaction *)transaction
{
    [self recordTransaction: transaction];
    [self provideContent: transaction.originalTransaction.payment.productIdentifier];
    [[SKPaymentQueue defaultQueue] finishTransaction: transaction];
}


- (void) failedTransaction: (SKPaymentTransaction *)transaction
{
    if (transaction.error.code != SKErrorPaymentCancelled) {
        // Optionally, display an error here.
    }
    [[SKPaymentQueue defaultQueue] finishTransaction: transaction];
}

应用内购买的正确实现见Apple official In-App Purchase Programming Guide.

关于objective-c - 为应用内购买实现一种以上的购买类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10611514/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap