-1

使用 RW 的教程,我已经设法在用户购买/购买所选 IAP 并显示一个勾号时实现 IAP。它还成功列出了我所有 4 个可用的 IAP

我决定以一种更简单且需要更少代码(我认为)的方式来解决这个问题 - 而不是添加更多代码以允许用户下载苹果托管的内容,然后再编写代码以将内容传输回应用程序和进入选定的视图

相反,我选择在应用程序中嵌入内容,而只是锁定链接到适当视图控制器的视图控制器或按钮,直到用户购买了内容/IAP。我不想改变 RW 的方式,因为到目前为止它工作得很好,我只想添加它,以便在用户购买内容并勾选 IAP 后,它会解锁与该特定 IAP 相关的视图控制器。

到目前为止,我如何继续添加代码以实现这一目标?我有 4 个应用内购买(意味着 4 个单独的视图控制器链接到此代码)我认为我需要扩展的代码如下......

- (void)productPurchased:(NSNotification *)notification {

    NSString * productIdentifier = notification.object;
    [_products enumerateObjectsUsingBlock:^(SKProduct * product, NSUInteger idx, BOOL *stop) {
        if ([product.productIdentifier isEqualToString:productIdentifier]) {
            [self.tableView reloadRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:idx inSection:0]] withRowAnimation:UITableViewRowAnimationFade];
            *stop = YES;
        }
    }];

}

- (void)reload {
    _products = nil;
    [self.tableView reloadData];
    [[SecretsIAPHelper sharedInstance] requestProductsWithCompletionHandler:^(BOOL success, NSArray *products) {
        if (success) {
            _products = products;
            [self.tableView reloadData];
        }
        [self.refreshControl endRefreshing];
    }];
}

#pragma mark - Table View

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _products.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];

    SKProduct * product = (SKProduct *) _products[indexPath.row];
    cell.textLabel.text = product.localizedTitle;
    [_priceFormatter setLocale:product.priceLocale];
    cell.detailTextLabel.text = [_priceFormatter stringFromNumber:product.price];

    if ([[SecretsIAPHelper sharedInstance] productPurchased:product.productIdentifier]) {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
        cell.accessoryView = nil;
    } else {
        UIButton *buyButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        buyButton.frame = CGRectMake(0, 0, 72, 37);
        [buyButton setTitle:@"Buy" forState:UIControlStateNormal];
        buyButton.tag = indexPath.row;
        [buyButton addTarget:self action:@selector(buyButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
        cell.accessoryType = UITableViewCellAccessoryNone;
        cell.accessoryView = buyButton;
    }

    return cell;
}

- (void)buyButtonTapped:(id)sender {

    UIButton *buyButton = (UIButton *)sender;
    SKProduct *product = _products[buyButton.tag];

    NSLog(@"Buying %@...", product.productIdentifier);
    [[SecretsIAPHelper sharedInstance] buyProduct:product];

}
4

1 回答 1

0

我认为您应该解决键入以下内容:

    - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:@"showDetail"]) {
    DetailViewController * detailViewController = (DetailViewController *) segue.destinationViewController;
    SKProduct * product = (SKProduct *) _products[self.tableView.indexPathForSelectedRow.row];
    if ([[RageIAPHelper sharedInstance] productPurchased:product.productIdentifier]) {

        if ([product.productIdentifier isEqualToString:@"it.horizonmedia.testiap.ghesemmo"]) {

            detailViewController.image = [UIImage imageNamed:@"drummer.png"];
            detailViewController.message = @"Bravo che l'hai COMPRATA!!!";
        }


          else if ([product.productIdentifier isEqualToString:@"it.horizonmedia.testiap.prodottotest3"]) {
              detailViewController.message = @"Bravo che l'hai COMPRATA!!!";
              detailViewController.image = [UIImage imageNamed:@"iphonerage.png"];

        } else if ([product.productIdentifier isEqualToString:@"it.horizonmedia.testiap.prodottotest"]) {
            detailViewController.message = @"You made a Good Purchase!";
            detailViewController.image = [UIImage imageNamed:@"iphonerage.png"];


        }
    } else {
        detailViewController.image = nil;
        detailViewController.message = @"Purchase to see comic!";
    }
}

}

请记住,正如我猜您在教程中所做的那样,您必须在代码中替换您的真实产品 ID。

让我知道它是否有效!

于 2013-08-27T15:31:59.110 回答