如果已付款(iAP),则自动将用户重定向到视图控制器

时间:2014-03-15 16:20:40

标签: ios objective-c in-app-purchase

这是交易......

对于我的项目,我在iAP工作正常后进行了重定向设置。我现在想要实现的是,如果已经付费的用户再次进入付款视图控制器,它将自动重定向到另一个视图控制器。

现在的问题是用户必须点击购买按钮,iAP说用户已经付款并且可以免费再次购买,这对你问我没有意义; - )

以下是我正在使用的代码:

#pragma mark -
#pragma mark SKProductsRequestDelegate

-(void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response
{

    NSArray *products = response.products;

    if (products.count != 0)
    {
        _product = products[0];
        _buyButton.enabled = YES;
        _productTitle.text = _product.localizedTitle;
        _productDescription.text = _product.localizedDescription;
    } else {
        _productTitle.text = @"Product not found";
    }

    products = response.invalidProductIdentifiers;

    for (SKProduct *product in products)
    {
        NSLog(@"Product not found: %@", product);
    }
}

- (IBAction)buyProduct:(id)sender {
    SKPayment *payment = [SKPayment paymentWithProduct:_product];
    [[SKPaymentQueue defaultQueue] addPayment:payment];
}

#pragma mark -
#pragma mark SKPaymentTransactionObserver

-(void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions
{
    for (SKPaymentTransaction *transaction in transactions)
    {
        switch (transaction.transactionState) {
            case SKPaymentTransactionStatePurchased:
                [self unlockFeature];
                [[SKPaymentQueue defaultQueue]
                 finishTransaction:transaction];
                break;

            case SKPaymentTransactionStateFailed:
                NSLog(@"Transaction Failed");
                [[SKPaymentQueue defaultQueue]
                 finishTransaction:transaction];
                break;

            default:
                break;
        }
    }
}

-(void)unlockFeature
{
    _buyButton.enabled = NO;
    [_buyButton setTitle:@"Purchased"
                forState:UIControlStateDisabled];
    [_homeViewController enableLevel2];

    if([[UIDevice currentDevice].model hasPrefix:@"iPad"])
    {
        UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"LoginStoryboard_iPad" bundle:nil];
        UIViewController *vc = [mainStoryboard instantiateViewControllerWithIdentifier:@"RegisterController"];
        [self presentViewController:vc animated:YES completion:nil];
    }
    else
    {
        UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"LoginStoryboard" bundle:nil];
        UIViewController *vc = [mainStoryboard instantiateViewControllerWithIdentifier:@"RegisterController"];
        [self presentViewController:vc animated:YES completion:nil];
    }
}

2 个答案:

答案 0 :(得分:0)

在不查询服务器的情况下,唯一可以执行此操作的方法是在某处保存变量或标志。最简单的是userDefaults。

只要用户进行购买,该值就会被保存(或者如果该标志不存在,并且您从Apple查询服务器,则无论如何都不会这样做。)

当您打开iAP viewController时,检查此标志,如果已设置,则重定向到另一个VC。

答案 1 :(得分:0)

请使用本教程,如何恢复IAP并保存在userDefaults中:
http://www.raywenderlich.com/21081/introduction-to-in-app-purchases-in-ios-6-tutorial