【问题标题】:How to display an activity indicator while loading in app purchase?加载应用购买时如何显示活动指示器?
【发布时间】:2013-04-11 14:57:31
【问题描述】:

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

【问题讨论】:

  • InAppPurchase 类的每个函数中设置breakpoints 以了解InApp 的流程并相应地显示/隐藏MBAlertView
  • buyCoffeeInAppPurchase ....--- >show and completeTransaction ----> hide ,failedTransaction --- > hide
  • 你得到了解决方案老兄.. ?

标签: iphone ios ipad in-app-purchase


【解决方案1】:

对于节目加载,您还可以使用 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];
}

【讨论】:

  • 谢谢。我今晚要试试这个。我会告诉你它是否有效。
【解决方案2】:
- (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.
    }
}

【讨论】:

  • 即使在 SKPaymentTransactionStatePurchasing 和 SKPaymentTransactionStateDeferred 状态下,这也会关闭活动指示器。此外,它不处理并行事务。
【解决方案3】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-12
    • 1970-01-01
    • 2020-02-17
    相关资源
    最近更新 更多