what is the alternative solution for paymentWithProductIdentifier?

▼魔方 西西 提交于 2019-12-18 03:05:01

问题


Hi i am using in APP purchase in my project . When i run this project everything works fine, except i am getting a warning message saying that "paymentWithProductIdentifier is deprecated", I gone through the similar questions that are asked in stack overflow but i didn't satisfied. I shown you my coding that i used in the project below

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

Can anyone tell me 1)the alternative for this warning. 2)or tell me whether this project approve in appstore if i use this existing code.


回答1:


Try using this:

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



回答2:


You can replace paymentWithProductIdentifier: with following codes:

// 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];

while productsRequest is a retain property.

And implement a SKProductsRequestDelegate method:

- (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;
}



回答3:


You have 3 options:

  • 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];
    



回答4:


You could use the following code instead, it does have a little extra that you may already have, but just to make sure

#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];
}

I use this code in one off my applications, so it should work.



来源:https://stackoverflow.com/questions/10848181/what-is-the-alternative-solution-for-paymentwithproductidentifier

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!