如何在应用购买时加载活动指示器?

时间:2013-04-11 05:46:41

标签: iphone ios ipad in-app-purchase

我有一个调用应用内购买功能的按钮。我会在装载产品时显示加载轮,并在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];
}

3 个答案:

答案 0 :(得分:2)

对于show loading,您还可以使用MBProgressHUD。它是一种显示加载过程的简便方法。下载MBProgressHUD.h& .m文件来自互联网&只需复制xcode项目。

HOW TO USE:在你的.h&中输入#import“MBProgressHUD.h”。 .m文件&还有这个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];
}

答案 1 :(得分: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.
    }
}

答案 2 :(得分:0)

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