2012-06-22 15 views
3

我已使用以下代碼進行應用內購買。如何在ios5中進行應用內購買?

- (void)viewDidLoad { 
if ([SKPaymentQueue canMakePayments]) { 
    NSLog(@"Can Buy Product"); 

    SKProductsRequest *productRequest=[[SKProductsRequest alloc]initWithProductIdentifiers:[NSSet setWithObject:@"com.mycompany.myproduct.productpack"]]; 
    productRequest.delegate=self; 
    [productRequest start]; 
} 
else { 

    NSLog(@"Product Can't be purchased"); 
} 
} 

-(IBAction)purchasePack1 { 

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

} 


-(void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response { 

validProduct=nil; 
int count=[response.products count]; 
if (count>0) { 
    NSLog(@"Product Avail"); 

    validProduct=[response.products objectAtIndex:0]; 
} 
else { 
    NSLog(@"No Product avail"); 
    [purchaseBtn setHidden:TRUE]; 
} 
} 


-(void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions { 

for (SKPaymentTransaction *transaction in transactions) { 
    switch (transaction.transactionState) { 
     case SKPaymentTransactionStatePurchasing: 
      NSLog(@"Purchasing"); 
      [activityIndicatorObj setHidden:FALSE]; 
      [activityIndicatorObj startAnimating]; 
      break; 

     case SKPaymentTransactionStatePurchased: 

      UIAlertView *alt=[[UIAlertView alloc]initWithTitle:@"Congrats" message:@"Thanks For Purchasing " delegate:self cancelButtonTitle:@"Dismiss" otherButtonTitles:nil]; 
      [alt show]; 
      [alt release]; 
      NSLog(@"Purchased"); 
      [activityIndicatorObj stopAnimating]; 
      [activityIndicatorObj setHidden:TRUE]; 
      [[SKPaymentQueue defaultQueue] finishTransaction:transaction]; 
      break; 

      case SKPaymentTransactionStateFailed: 

      if (transaction.error.code!=SKErrorPaymentCancelled) { 

       NSLog(@"Cancelled"); 

      } 
      [[SKPaymentQueue defaultQueue] finishTransaction:transaction]; 

      UIAlertView *alt1=[[UIAlertView alloc]initWithTitle:@"Sorry" message:@"Some Error Encountered!" delegate:self cancelButtonTitle:@"Dismiss" otherButtonTitles:nil]; 
       [alt1 show]; 
       [alt1 release]; 
      NSLog(@"Failed"); 
      [activityIndicatorObj stopAnimating]; 
      [activityIndicatorObj setHidden:TRUE]; 
      break; 

      case SKPaymentTransactionStateRestored: 
      NSLog(@"Restored"); 
      [activityIndicatorObj stopAnimating]; 
      [activityIndicatorObj setHidden:TRUE]; 
      [[SKPaymentQueue defaultQueue] finishTransaction:transaction]; 
      break; 



     default: 
      break; 
    } 
} 
} 

當我點擊購買按鈕我收到一個警告消息說「你已經購買了這個產品,還沒有下載的,你想下載?」 當我點擊是,我沒有得到任何應用程序的響應,我相信該產品沒有下載。任何人都可以告訴我這個問題的建議。提前致謝。

+0

當您加載視圖時是否打印「Product avail」文字? –

+2

paymentWithProductIdentifier:自iOS 5起棄用。您應該使用paymentWithProduct:代替。 –

回答

0

我沒有看到- (void)provideContent:(NSString *)productId的任何實現 - 你錯過了那部分?無論如何,當交易狀態到SKPaymentTransactionStatePurchased時,您應該會收到通知。

如果您還沒有它,可能會希望看看Ray Wenderlich購買in-app購買內容,這將有助於您創建應用內管理員課程。我認爲你已經將視覺控制器中的應用程序內置邏輯放在了一個單獨的類中。因此,應用程序內部經理/助手:-)