what is the alternative solution for paymentWithProductIdentifier?

前端 未结 4 1155
醉酒成梦
醉酒成梦 2020-12-15 18:34

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

相关标签:
4条回答
  • 2020-12-15 18:52

    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;
    }
    
    0 讨论(0)
  • 2020-12-15 18:54

    Try using this:

    SKProduct *selectedProduct = <#from the products response list#>;
    SKPayment *payment = [SKPayment paymentWithProduct:selectedProduct];
    [[SKPaymentQueue defaultQueue] addPayment:payment];
    
    0 讨论(0)
  • 2020-12-15 19:02

    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.

    0 讨论(0)
  • 2020-12-15 19:04

    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];
      
    0 讨论(0)
提交回复
热议问题