How to do in-app purchase in ios5?
2012-07-13 11:05
686 查看
I have used the following code for in-app purchase.
- (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; } } }
相关文章推荐
- In Theano, how to do Reverse-MaxPooling in the Convolutional MaxPooling Auto-Encoder
- How do you copy the contents of an array to a std::vector in C++ without looping? (From stack over flow)
- How to run 32-bit app in Ubuntu 14.04 64-bit?
- How to make Windows Form app truly Full Screen (and to hide Taskbar) in C#? 转
- How to publish an App-V-enabled application in Citrix XenApp
- Linq to SQL how to do “where [column] in (list of values)”
- How to do the TM symbol in HTML
- How to Add Splash Screen in Your iOS App
- iphone dev 入门实例7:How to Add Splash Screen in Your iOS App
- How do I add capabilities to my executable in qt for symbian?
- How do I add classes to main menu ul and li in Drupal 8
- How do I convert an enum to a list in C#?
- How can I get a FlowDocument Hyperlink to launch browser and go to URL in a WPF app?
- How to do conditional auto-wiring in Spring?
- how to use ocx control in console app.
- How to run eclipse in clean mode? and what happens if we do so?
- How to check memory leaks in android app?
- How to use user’s location in your app?
- how to enable the Accessibility in the app
- How to test Service Layer in Spring web app