【问题标题】:what is the alternative solution for paymentWithProductIdentifier?paymentWithProductIdentifier 的替代解决方案是什么?
【发布时间】:2012-06-01 09:58:50
【问题描述】:

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

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

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

【问题讨论】:

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


    【解决方案1】:

    试试这个:

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

    【讨论】:

    • 你能告诉我这行“”的示例代码
    • 这只是用户选择购买的产品之一,您在调用后得到 - (void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response。
    • 回答surendher获取示例代码。这进入了 didReceiveResponse 委托: SKProduct * selectedProduct = nil; selectedProduct = [response.products objectAtIndex:0];
    【解决方案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;
    }
    

    【讨论】:

      【解决方案3】:

      您有 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];
        

      【讨论】:

        【解决方案4】:

        您可以改用以下代码,它确实有一些您可能已经拥有的额外代码,但只是为了确保

        #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 中已被弃用。一切正常,但仍然有警告消息。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-08-11
        • 2018-04-02
        • 2019-06-23
        • 1970-01-01
        • 2011-04-13
        • 2016-10-24
        • 1970-01-01
        相关资源
        最近更新 更多