3

我有一个调用应用内购买功能的按钮。我会在加载产品时显示一个加载轮,并在 UIAlertView 出现以确认购买时将其关闭。我正在使用 MBAlertView 在我的应用程序中显示其他消息,我也会在这里使用它。我该怎么做?我想在用户按下按钮时显示它,并在收到响应时将其关闭。

这是我目前的代码!

- (IBAction)buyCoffeeInAppPurchase:(id)sender {
    SKProductsRequest *request= [[SKProductsRequest alloc]
                                 initWithProductIdentifiers: [NSSet setWithObject:     @"com.giovannibalestra.emergencycall.Thankyoudeveloper"]];
    request.delegate = self;
    [request start];
    // I should add something like this line of code to show the activity indicator but I can only set hidesAfter some seconds
    // [MBHUDView hudWithBody:@"Wait." type:MBAlertViewHUDTypeActivityIndicator hidesAfter:4.0 show:YES];
}
- (void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response {
    [[SKPaymentQueue defaultQueue] addTransactionObserver:self];

    NSArray *myProduct = response.products;
    NSLog(@"%@",[[myProduct objectAtIndex:0] productIdentifier]);

    SKPayment *newPayment = [SKPayment paymentWithProduct:[myProduct objectAtIndex:0]];
    [[SKPaymentQueue defaultQueue] addPayment:newPayment];

}

- (void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)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 {
    [[SKPaymentQueue defaultQueue] finishTransaction: transaction];
}

- (void)restoreTransaction:(SKPaymentTransaction *)transaction {
    [[SKPaymentQueue defaultQueue] finishTransaction: transaction];
}

- (void)failedTransaction:(SKPaymentTransaction *)transaction {
    if (transaction.error.code != SKErrorPaymentCancelled)
    {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Purchase Unsuccessful"
                                                        message:@"Your purchase failed. Please try again."
                                                       delegate:self
                                              cancelButtonTitle:@"OK"
                                              otherButtonTitles:nil];
        [alert show];
    }
    [[SKPaymentQueue defaultQueue] finishTransaction: transaction];
}
4

3 回答 3

3

对于显示加载,您还可以使用 MBProgressHUD。它是一种显示加载过程的简单方法。从 Internet 下载 MBProgressHUD.h 和 .m 文件并复制到 xcode 项目中。

HOW TO USE:在您的 .h 和 .m 文件中导入此 #import "MBProgressHUD.h" 以及此 MBProgressHUD *HUD; 在 .h 文件中。然后在 .m 文件中,您的代码如下所示:

- (IBAction)buyCoffeeInAppPurchase:(id)sender {

    HUD = [[MBProgressHUD alloc] initWithFrame:CGRectMake(0, 0, 320, 460) ];
       HUD.labelText = @"Fetching...";
        [self.view addSubview:HUD];
        [HUD show:YES];


    SKProductsRequest *request= [[SKProductsRequest alloc]
                                 initWithProductIdentifiers: [NSSet setWithObject:     @"com.giovannibalestra.emergencycall.Thankyoudeveloper"]];
    request.delegate = self;
    [request start];
    // I should add something like this line of code to show the activity indicator but I can only set hidesAfter some seconds
    // [MBHUDView hudWithBody:@"Wait." type:MBAlertViewHUDTypeActivityIndicator hidesAfter:4.0 show:YES];
}
- (void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response {
    [[SKPaymentQueue defaultQueue] addTransactionObserver:self];

    NSArray *myProduct = response.products;
    NSLog(@"%@",[[myProduct objectAtIndex:0] productIdentifier]);

    SKPayment *newPayment = [SKPayment paymentWithProduct:[myProduct objectAtIndex:0]];
    [[SKPaymentQueue defaultQueue] addPayment:newPayment];

    [HUD hide:YES];
    [HUD removeFromSuperViewOnHide];

}

- (void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)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 {
    [[SKPaymentQueue defaultQueue] finishTransaction: transaction];
}

- (void)restoreTransaction:(SKPaymentTransaction *)transaction {
    [[SKPaymentQueue defaultQueue] finishTransaction: transaction];
}

- (void)failedTransaction:(SKPaymentTransaction *)transaction {
    if (transaction.error.code != SKErrorPaymentCancelled)
    {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Purchase Unsuccessful"
                                                        message:@"Your purchase failed. Please try again."
                                                       delegate:self
                                              cancelButtonTitle:@"OK"
                                              otherButtonTitles:nil];
        [alert show];
    }
    [[SKPaymentQueue defaultQueue] finishTransaction: transaction];
}
于 2013-04-11T06:26:57.870 回答
0
- (void)paymentQueue:(SKPaymentQueue *)queue 
 updatedTransactions:(NSArray *)transactions 
{
    for (SKPaymentTransaction *transaction in transactions)
    {
        if (transaction.transactionState == SKPaymentTransactionStatePurchased) {
            [self completeTransaction:transaction];
        }
        if (transaction.transactionState == SKPaymentTransactionStateFailed) {
            [self failedTransaction:transaction];
        }
        if (transaction.transactionState == SKPaymentTransactionStateRestored) {
            [self restoreTransaction:transaction];
        }
        else {
            // Do something.
        }

        // You can dismiss your Activity Indicator (MBHUDView) here.
    }
}
于 2013-04-11T06:29:04.433 回答
0

嗨,只需在用户单击购买按钮时应用活动指示器并在此方法后停止活动指示器 - (void)completeTransaction:(SKPaymentTransaction *)transaction。不要忘记在失败的交易中也停止,其他明智的活动指示器开始动画。有时活动指示器需要通过线程调用,因为它们不可见。所以也请尝试一下。

于 2013-04-11T06:57:31.450 回答