20

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

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

谁能告诉我 1)此警告的替代方法。2)如果我使用这个现有代码,或者告诉我这个项目是否在 appstore 中获得批准。

4

4 回答 4

20

尝试使用这个:

SKProduct *selectedProduct = <#from the products response list#>;
SKPayment *payment = [SKPayment paymentWithProduct:selectedProduct];
[[SKPaymentQueue defaultQueue] addPayment:payment];
于 2012-06-01T10:09:05.410 回答
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];

whileproductsRequest是一个保留属性。

并实现一个 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;
}
于 2013-09-12T05:08:13.737 回答
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];
    
于 2015-07-21T21:50:59.117 回答
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];
}

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

于 2013-10-14T23:10:10.817 回答