如何从用户定义方法返回委托方法

时间:2011-09-29 06:53:52

标签: ios iphone in-app-purchase nsarray

我正在实施应用内购买。我在执行商店套件的委托方法时遇到了问题。

以下是我的代码:

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

    NSLog(@"Received products results...");   
    NSLog(@"response.products.......%@",response.products);
    self.products = response.products;
    self.request = nil;    

     proUpgradeProduct = [_products count] == 1 ? [[_products objectAtIndex:0] retain] : nil;

     if (proUpgradeProduct)
     {
     NSLog(@"Product title: %@" , proUpgradeProduct.localizedTitle);
     NSLog(@"Product description: %@" , proUpgradeProduct.localizedDescription);
     NSLog(@"Product price: %@" , proUpgradeProduct.price);
     NSLog(@"Product id: %@" , proUpgradeProduct.productIdentifier);
     }

     NSLog(@"products.....%@",_products);

     for (NSString *invalidProductId in response.invalidProductIdentifiers)
     {
     NSLog(@"Invalid product id: %@" , invalidProductId);
     }


    [[NSNotificationCenter defaultCenter] postNotificationName:kProductsLoadedNotification object:_products];    

    [self buyProductIdentifier:proUpgradeProduct.productIdentifier];

}


- (void)buyProductIdentifier:(NSString *)productIdentifier {
    if ([SKPaymentQueue canMakePayments])
    {
    NSLog(@"Buying %@...", productIdentifier);

    SKPayment *payment = [SKPayment paymentWithProductIdentifier:productIdentifier];
   [[SKPaymentQueue defaultQueue] addPayment:payment];
    }
    else
    {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"StoreKit" message:@"You are not authorised to purchase from AppStore"
                                                       delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
        [alert show];
        [alert release];
    }

}

- (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)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions方法中的(void)buyProductIdentifier:(NSString *)productIdentifier

我该怎么办?

1 个答案:

答案 0 :(得分:0)

  1. 您应该设置SKPaymentTransactionObserver的{​​{1}}:

    defaultQueue

  2. 方法[[SKPaymentQueue defaultQueue] addTransactionObserver:manager];只有在paymentQueue:updatedTransactions:结束使用StoreKit中的交易后才会被调用:当用户取消或购买(或交易已恢复)时产品