xcode PaymentWithProductIdentifier 的替代解决方案是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10848181/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
what is the alternative solution for paymentWithProductIdentifier?
提问by surendher
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
嗨,我在我的项目中使用 APP 购买。当我运行这个项目时,一切正常,除了我收到一条警告消息说“paymentWithProductIdentifier 已被弃用”,我经历了堆栈溢出中提出的类似问题,但我并不满意。我向您展示了我在下面的项目中使用的编码
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) 此警告的替代方案。2)或者告诉我这个项目是否在应用商店中获得批准,如果我使用这个现有的代码。
回答by Nuzhat Zari
Try using this:
尝试使用这个:
SKProduct *selectedProduct = <#from the products response list#>;
SKPayment *payment = [SKPayment paymentWithProduct:selectedProduct];
[[SKPaymentQueue defaultQueue] addPayment:payment];
回答by hiroshi
You can replace paymentWithProductIdentifier:
with following codes:
您可以替换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];
while productsRequest
is a retain property.
whileproductsRequest
是一个保留属性。
And implement a SKProductsRequestDelegate method:
并实现一个 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;
}
回答by sgl0v
You have 3 options:
您有 3 个选择:
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 ofSKPayment
: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];
使用预处理器定义抑制此警告:
#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];
回答by Jojodmo
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.
我在我的应用程序中使用了这段代码,所以它应该可以工作。