paymentQueue:updatedTransactions什么时候不应该被调用?

时间:2012-03-10 01:16:47

标签: iphone xcode4.2 in-app-purchase storekit

我在应用内购买时遇到了一个奇怪的问题......

在我请求产品信息后,有时会因某种原因自动调用paymentQueue:updatedTransactions。

在我商店的viewDidLoad方法中,我初始化并启动请求:

- (void)viewDidLoad
{
    [super viewDidLoad];

    productsArray = [[NSArray alloc] init];

    [[SKPaymentQueue defaultQueue] addTransactionObserver:self];

    NSSet *productIdentifiers = [NSSet setWithObjects:PRODUCT_ID, nil];
    productsRequest = [[SKProductsRequest alloc] initWithProductIdentifiers:productIdentifiers];
    productsRequest.delegate = self;
    [productsRequest start];
}

然后我得到回复:

- (void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response
{
    productsArray = [[NSArray alloc] initWithArray:response.products];

    NSLog(@"Products Count: %d",[productsArray count]);
    NSLog(@"Invalid Products Count: %d",[response.invalidProductIdentifiers count]);

    if ([productsArray count] > 0)
    {
        NSLog(@"Product title: %@" ,[productsArray objectAtIndex:0]);
        [self hideLoadingView];


    } else {
        UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:@"No products found"
                                                            message:@"There might have been a problem. Please try again soon." delegate:self 
                                                  cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [alertView show];
        [alertView release];

        [self hideLoadingView];
        [purchaseBtn setEnabled:NO];

    }

    [productsRequest release];

    [[NSNotificationCenter defaultCenter] postNotificationName:kInAppPurchaseManagerProductsFetchedNotification object:self userInfo:nil];

}

这是我希望流程结束的地方,并等待用户点击购买按钮...但有时(例如,70%的时间),我会弹出一个用户名/密码警告框为了购买该物品...但是用户没有点击任何东西......(如果用户已经“登录”,那么它会在没有询问的情况下购买该物品。这不是应该如何。

这是有时会自动调用的方法:

- (void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions
{
    NSLog(@"updated transaction");
    for (SKPaymentTransaction *transaction in transactions)
    {
        switch (transaction.transactionState)
        {
            case SKPaymentTransactionStatePurchased:
                NSLog(@"transationStatePurchased");
                [self completeTransaction:transaction];
                break;
            case SKPaymentTransactionStateFailed:
                NSLog(@"transationStateFailed");
                [self failedTransaction:transaction];
                break;
            default:
                break;
        }
    }
}

这是购买按钮的IBAction:

-(IBAction)buyItem:(id)sender {

    [self showLoadingView];

    SKPayment *payment = [SKPayment paymentWithProductIdentifier:[(SKProduct *)[productsArray objectAtIndex:0] productIdentifier]];
    [[SKPaymentQueue defaultQueue] addPayment:payment];

    }        
}

我认为在调用defaultQueue的“addPayment”方法之前,不应显示StoreKit的用户/密码警告框,该方法仅出现在IBAction中。

有什么想法吗? 谢谢!

1 个答案:

答案 0 :(得分:8)

一旦你添加了一个事务观察者,iOS将检查默认的队列,如果有任何未完成的事务(这意味着你没有完成它),所以它会在每次交易时显示警告框,直到你完成它为止如果您没有采取任何行动来添加新的付款。