paymentWithProductIdentifier有什么替代方案?

20

大家好,我正在我的项目中使用应用内购买。当我运行这个项目时,一切都很正常,除了我收到一个警告消息,提示“paymentWithProductIdentifier已被弃用”。我查看了在stackoverflow上提出的类似问题,但我并不满意。下面是我在项目中使用的代码:

SKPayment *payment=[SKPayment paymentWithProductIdentifier:@"com.mycompany.dmaker.maker1"];
[[SKPaymentQueue defaultQueue] addTransactionObserver:self];
[[SKPaymentQueue defaultQueue] addPayment:payment];

有人能告诉我: 1)这个警告的替代方法是什么? 2)如果我使用现有的代码,这个项目是否会在应用商店获得批准?

4个回答

20

尝试使用这个:

SKProduct *selectedProduct = <#from the products response list#>;
SKPayment *payment = [SKPayment paymentWithProduct:selectedProduct];
[[SKPaymentQueue defaultQueue] addPayment:payment];

1
你能告诉我这行代码的示例吗:“<#从产品响应列表中#>”? - surendher
这其实就是用户选择购买的其中一个产品,你可以通过调用-(void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response来获取它。 - Nuzhat Zari
回答Surendher的示例代码。这段代码放在didReceiveResponse委托中:SKProduct * selectedProduct = nil; selectedProduct = [response.products objectAtIndex:0]; - GeneCode

2

您可以使用以下代码替换paymentWithProductIdentifier:

// SKPayment *payment = [SKPayment paymentWithProductIdentifier:productId];
// [[SKPaymentQueue defaultQueue] addPayment:payment];
NSSet *productIdentifiers = [NSSet setWithObject:productId];
self.productsRequest = [[SKProductsRequest alloc] initWithProductIdentifiers:productIdentifiers];
self.productsRequest.delegate = self; // your wrapper for IAP or AppDelegate or anything
[self.productsRequest start];

productsRequest是一个保留属性时。

并实现一个SKProductsRequestDelegate方法:

- (void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response
{
    for (SKProduct *product in response.products) {
        SKPayment *payment = [SKPayment paymentWithProduct:product];
        [[SKPaymentQueue defaultQueue] addPayment:payment];
    }
    self.productsRequest = nil;
}

2

您有三个选项:

  • suppress this warning with preprocessor definition:

    #pragma clang diagnostic push
    #pragma clang diagnostic ignored "-Wdeprecated-declarations"
    SKPayment *payment=[SKPayment paymentWithProductIdentifier:@"com.mycompany.dmaker.maker1"];
    #pragma clang diagnostic pop
    [[SKPaymentQueue defaultQueue] addPayment:payment];
    
  • create SKMutablePayment instead of SKPayment:

    SKMutablePayment *payment = [[SKMutablePayment alloc] init];
    payment.productIdentifier = @"com.mycompany.dmaker.maker1";
    payment.quantity = 1;
    [[SKPaymentQueue defaultQueue] addPayment:payment];
    
  • use paymentWithProduct: convenience initializer:

    SKPayment *payment = [SKPayment paymentWithProduct:<# product that you received in productsRequest:didReceiveResponse: #>];
    [[SKPaymentQueue defaultQueue] addPayment:payment];
    

1
您可以使用以下代码代替,它可能已经包含一些额外的内容,但只是为了确保。
#define kInAppPurchaseId "(your product ID here)"

- (void)makePurchase{
//call this when you would like to begin the purchase
//like when the user taps the "purchase" button
NSLog(@"User requests to make purchase");

if([SKPaymentQueue canMakePayments]){
    NSLog(@"User can make payments");

    SKProductsRequest *productsRequest = [[SKProductsRequest alloc] initWithProductIdentifiers:[NSSet setWithObject:kInAppPurchaseId]];
    productsRequest.delegate = self;
    [productsRequest start];

}
else{
    //the user is not allowed to make payments
    NSLog(@"User cannot make payments due to parental controls");
}
}

- (void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response{
SKProduct *validProduct = nil;
int count = [response.products count];
if(count > 0){
    validProduct = [response.products objectAtIndex:0];
    NSLog(@"Products Available!");
    [self purchase:validProduct];
}
else if(!validProduct){
    NSLog(@"No products available");
}
}

- (IBAction)purchase:(SKProduct *)product{
SKPayment *payment = [SKPayment paymentWithProduct:product];
[[SKPaymentQueue defaultQueue] addTransactionObserver:self];
[[SKPaymentQueue defaultQueue] addPayment:payment];
}

我在我的一个应用程序中使用这段代码,所以它应该可以工作。


您的帖子与问题不符。问题在于,paymentWithProductIdentifier在iOS 5.0中已被弃用。一切都正常工作,但仍然会出现警告消息。 - Alex Cio

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接