iPhone 3G 4.2.1 - 未调用SKProductsRequest委托方法

时间:2011-08-15 21:25:50

标签: iphone ios storekit

我的应用FourFourTwo Stats Zone今晚刚刚在App Store上线:

我已经要求一些人测试In App Purchase并在所有设备上取得成功,除了iPhone 3G(运行4.2.1 - 尚未测试其他iOS版本)。我已经尝试在我拥有的设备上调试它,似乎没有调用任何SKProductsRequest委托方法。这是我的代码:

- (void)requestPurchaseOfCompetition:(Competition*)competition {
    DLog("");

    if ([SKPaymentQueue canMakePayments]) {
        DLog(@"do store");

        NSString* productIdentifier = [NSString stringWithFormat:@"%@%@_%@", kPRODUCT_IDENTIFIER_PREFIX, competition.competitionId, competition.season];

        SKProductsRequest *request= [[SKProductsRequest alloc] initWithProductIdentifiers: [NSSet setWithObject:productIdentifier]];

        [[NSNotificationCenter defaultCenter] postNotificationOnMainThread:[NSNotification notificationWithName:kStoreKitHandlerNotificationRequestProductInfo object:nil userInfo:[NSDictionary dictionaryWithObject:request forKey:@"request"]]];
        request.delegate = self;
        [request start];
        [request release];
    } else {
        DLog(@"no store");

        // Warn the user that purchases are disabled.
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Store", @"Store") message:NSLocalizedString(@"In app purchasing is disabled for this device (in Settings > General > Restrictions).  Please enable this setting to purchase more competitions.", @"In app purchasing is disabled for this device (in Settings > General > Restrictions).  Please enable this setting to purchase more competitions.") delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [alertView show];
        [alertView release];
    }   
}

...

- (void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response {
    DLog(@"response: %@", response);
    DLog(@"invalid product identifiers: %@", response.invalidProductIdentifiers);

    for (SKProduct *product in response.products) {
        DLog(@"product: %@", product);

        [[NSNotificationCenter defaultCenter] postNotificationOnMainThread:[NSNotification notificationWithName:kStoreKitHandlerNotificationGotProductInfo object:nil userInfo:[NSDictionary dictionaryWithObject:product forKey:@"product"]]];

        SKPayment *payment = [SKPayment paymentWithProduct:product];

        SKPaymentQueue *paymentQueue = [SKPaymentQueue defaultQueue];
        [paymentQueue addTransactionObserver:self];
        [paymentQueue addPayment:payment];
    }
}

- (void)request:(SKRequest *)request didFailWithError:(NSError *)error {
    DLog(@"request failed: %@,  %@", request, error);

    [[NSNotificationCenter defaultCenter] postNotificationOnMainThread:[NSNotification notificationWithName:kStoreKitHandlerNotificationRequestProductInfoFailed object:nil userInfo:[NSDictionary dictionaryWithObject:error forKey:@"error"]]];
}

- (void)requestDidFinish:(SKRequest *)request {
    DLog(@"request finished: %@", request);
}

三种委托方法中没有任何日志消息出现。

这适用于3GS,iPhone 4,iPad等,但不适用于运行4.2.1的iPhone 3G。

任何人都可以提供任何见解吗?

1 个答案:

答案 0 :(得分:2)

您应该在启动请求后立即释放SKProductsRequest *request,但应该在两个委托方法中释放它。检查the documentation父级SKRequest类,其中包含:

  

此方法(requestDidFinishrequest:didFailWithError:)   被叫,你的代表没有得到进一步的沟通   请求并可以发布它。

我不知道为什么在较新版本的SDK中这对您有用,但严格查看您的代码,可能会在响应调用委托方法之前发布请求。

我就是这样做的:

- (void)requestPurchaseOfCompetition:(Competition*)competition {
    DLog("");

    if ([SKPaymentQueue canMakePayments]) {
        DLog(@"do store");

        NSString* productIdentifier = [NSString stringWithFormat:@"%@%@_%@", kPRODUCT_IDENTIFIER_PREFIX, competition.competitionId, competition.season];

        SKProductsRequest *request = [[SKProductsRequest alloc] initWithProductIdentifiers: [NSSet setWithObject:productIdentifier]];

        [[NSNotificationCenter defaultCenter] postNotificationOnMainThread:[NSNotification notificationWithName:kStoreKitHandlerNotificationRequestProductInfo object:nil userInfo:[NSDictionary dictionaryWithObject:request forKey:@"request"]]];
        request.delegate = self;
        [request start];

    } else {
        DLog(@"no store");

        // Warn the user that purchases are disabled.
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Store", @"Store") message:NSLocalizedString(@"In app purchasing is disabled for this device (in Settings > General > Restrictions).  Please enable this setting to purchase more competitions.", @"In app purchasing is disabled for this device (in Settings > General > Restrictions).  Please enable this setting to purchase more competitions.") delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [alertView show];
        [alertView release];
    }   
}

...

- (void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response {
    DLog(@"response: %@", response);
    DLog(@"invalid product identifiers: %@", response.invalidProductIdentifiers);

    for (SKProduct *product in response.products) {
        DLog(@"product: %@", product);

        [[NSNotificationCenter defaultCenter] postNotificationOnMainThread:[NSNotification notificationWithName:kStoreKitHandlerNotificationGotProductInfo object:nil userInfo:[NSDictionary dictionaryWithObject:product forKey:@"product"]]];

        SKPayment *payment = [SKPayment paymentWithProduct:product];

        SKPaymentQueue *paymentQueue = [SKPaymentQueue defaultQueue];
        [paymentQueue addTransactionObserver:self];
        [paymentQueue addPayment:payment];
    }
    [request release];
}

- (void)request:(SKRequest *)request didFailWithError:(NSError *)error {
    DLog(@"request failed: %@,  %@", request, error);

    [[NSNotificationCenter defaultCenter] postNotificationOnMainThread:[NSNotification notificationWithName:kStoreKitHandlerNotificationRequestProductInfoFailed object:nil userInfo:[NSDictionary dictionaryWithObject:error forKey:@"error"]]];
    [request release];
}