paymentWithProductIdentifier的替代解决方案是什么?

时间:2012-06-01 09:58:51

标签: iphone objective-c xcode in-app-purchase

您好我在我的项目中使用APP购买。当我运行这个项目一切正常,除了我收到一条警告消息,说“paymentWithProductIdentifier已被弃用”,我经历了堆栈溢出中提出的类似问题,但我不满意。我向您展示了我在下面的项目中使用的编码

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

任何人都可以告诉我  1)此警告的替代方案。 2)或者告诉我,如果我使用现有代码,该项目是否在appstore中批准。

4 个答案:

答案 0 :(得分:20)

尝试使用:

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

答案 1 :(得分: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 :(得分:2)

您有3个选项:

  • 使用预处理器定义禁止此警告:

    #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];
    
  • 创建SKMutablePayment而不是SKPayment

    SKMutablePayment *payment = [[SKMutablePayment alloc] init];
    payment.productIdentifier = @"com.mycompany.dmaker.maker1";
    payment.quantity = 1;
    [[SKPaymentQueue defaultQueue] addPayment:payment];
    
  • 使用paymentWithProduct:便利初始化程序:

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

答案 3 :(得分: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];
}

我在我的应用程序中使用此代码,因此它应该可以正常工作。