2012-06-01 15 views
19

Ciao sto usando in acquisto APP nel mio progetto. Quando eseguo questo progetto, tutto funziona correttamente, tranne che ricevo un messaggio di avviso che dice "paymentWithProductIdentifier è deprecato", ho seguito le domande simili che sono state poste nello stack overflow ma non mi sono soddisfatto. Ti ho mostrato la mia codifica che ho usato nel progetto sottoqual è la soluzione alternativa per paymentWithProductIdentifier?

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

Qualcuno può dirmi 1) l'alternativa per questo avvertimento. 2) o dimmi se questo progetto approva in appstore se utilizzo questo codice esistente.

risposta

19

Provare a utilizzare questo:

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

Potete dirmi il codice di esempio per questa linea "<#from la lista dei prodotti risposta #>" – surendher

+0

Questo non è altro che uno dei prodotti che utente seleziona per l'acquisto, che si ottiene dopo aver chiamato - (void) productsRequest: (SKProductsRequest *) request didReceiveResponse: (risposta SKProductsResponse *). –

+0

Per rispondere al surendher per il codice di esempio. Ciò vale in delegatoReceiveResponse: SKProduct * selectedProduct = nil; selectedProduct = [response.products objectAtIndex: 0]; – GeneCode

2

È possibile sostituire paymentWithProductIdentifier: con i seguenti codici:

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

mentre productsRequest è una conservano proprietà.

e implementare un metodo 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; 
} 
1

è possibile utilizzare il seguente codice, invece, che deve avere un piccolo extra che si può già avere, ma solo per assicurarsi che

#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 usa questo codice in una delle mie applicazioni, quindi dovrebbe funzionare.

+0

Il tuo post non soddisfa la domanda. Il problema è che 'paymentWithProductIdentifier' è deprecato in iOS 5.0. Tutto funziona bene ma c'è ancora il messaggio di avviso. –

+1

@ 亚历山大 Spiacente, ho aggiornato la mia risposta con il codice corretto – Jojodmo

1

avete 3 opzioni:

  • sopprimere questo avvertimento con la definizione del preprocessore:

    #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]; 
    
  • creare SKMutablePayment invece di SKPayment:

    SKMutablePayment *payment = [[SKMutablePayment alloc] init]; 
    payment.productIdentifier = @"com.mycompany.dmaker.maker1"; 
    payment.quantity = 1; 
    [[SKPaymentQueue defaultQueue] addPayment:payment]; 
    
  • uso paymentWithProduct: convenienza initializer:

    SKPayment *payment = [SKPayment paymentWithProduct:<# product that you received in productsRequest:didReceiveResponse: #>]; 
    [[SKPaymentQueue defaultQueue] addPayment:payment]; 
    
Problemi correlati