Pastebin is 300% more awesome when you are logged in. Sign Up, it's FREE!
Guest

inAp Purchase

By: a guest on Oct 14th, 2013  |  syntax: Objective C  |  size: 9.81 KB  |  hits: 467  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. //
  2. //  MyInAppPurchase.mm
  3. //  Vegas Baby
  4. //
  5. //  Created by Gururaj T on 13/09/13.
  6. //  Copyright (c) 2013 Guru. All rights reserved.
  7. //
  8.  
  9. #import "MyInAppPurchase.h"
  10.  
  11.  
  12. #import "MyInAppPurchase.h"
  13. #import "AppDelegate.h"
  14. #import "Reachability.h"
  15.  
  16. static MyInAppPurchase *gInApp = nil;
  17.  
  18.  
  19. @interface MyInAppPurchase()
  20. -(void)StopIndicator;
  21. -(void)ShowActivityIndicator;
  22. -(void)BuyGameCoins;
  23. @end
  24.  
  25. @implementation MyInAppPurchase
  26.  
  27. @synthesize currentID = mCurrentId;
  28. @synthesize currentProductId = mCurrentleaderProductId;
  29.  
  30.  
  31. + (MyInAppPurchase *)sharedInAppPurchase
  32. {
  33.         if (!gInApp) {
  34.         gInApp = [[MyInAppPurchase alloc] init];
  35.         }
  36.         return gInApp;
  37. }
  38.  
  39.  
  40. -(id)init
  41. {
  42.     if(self = [super init])
  43.     {
  44.         mLoadingView = nil;
  45.     }
  46.     return self;
  47. }
  48.  
  49.  
  50. -(void)buyCoins_1
  51. {
  52.     self.currentProductId = IN_APP_KEY_BUY_COIN_1;
  53.     self.currentID = k_Buy_Coins_Pack_1;
  54.    
  55.     [self BuyGameCoins];
  56. }
  57.  
  58. -(void)buyCoins_2
  59. {
  60.     self.currentProductId = IN_APP_KEY_BUY_COIN_2;
  61.     self.currentID = k_Buy_Coins_Pack_2;
  62.    
  63.     [self BuyGameCoins];
  64. }
  65.  
  66. -(void)buyCoins_3
  67. {
  68.     self.currentProductId = IN_APP_KEY_BUY_COIN_3;
  69.     self.currentID = k_Buy_Coins_Pack_3;
  70.    
  71.     [self BuyGameCoins];
  72. }
  73.  
  74.  
  75.  
  76.  
  77. - (BOOL)connected
  78. {
  79.     Reachability *reachability = [Reachability reachabilityForInternetConnection];
  80.     NetworkStatus networkStatus = [reachability currentReachabilityStatus];
  81.     return !(networkStatus == NotReachable);
  82. }
  83.  
  84.  
  85. -(void)BuyGameCoins
  86. {
  87.     if([self connected])
  88.     {
  89.         if ([SKPaymentQueue canMakePayments]) {
  90.             [self ShowActivityIndicator];
  91.            
  92.             SKProductsRequest *request = [[SKProductsRequest alloc] initWithProductIdentifiers:[NSSet setWithObject:self.currentProductId]];
  93.            
  94.             request.delegate = self;
  95.             [request start];
  96.            
  97.            
  98.         } else {
  99.             UIAlertView *tmp = [[UIAlertView alloc]
  100.                                 initWithTitle:@"Prohibited"
  101.                                 message:@"Parental Control is enabled, cannot make a purchase!"
  102.                                 delegate:self
  103.                                 cancelButtonTitle:nil
  104.                                 otherButtonTitles:@"Ok", nil];
  105.             tmp.tag = kInApPurchaseFailedAlertTag;
  106.             [tmp show];
  107.             [tmp release];
  108.         }
  109.        
  110.     }
  111.     else
  112.     {
  113.         UIAlertView* alert= [[[UIAlertView alloc] initWithTitle:@"No Internet Connection!" message: @"Checkout your internet connectivity!"
  114.                                                        delegate: NULL cancelButtonTitle: @"OK" otherButtonTitles: NULL] autorelease];
  115.        
  116.         [alert show];
  117.     }
  118. }
  119.  
  120.  
  121. -(void)RestoreTransaction
  122. {
  123.         [[SKPaymentQueue defaultQueue] addTransactionObserver:self];
  124.     [[SKPaymentQueue defaultQueue] restoreCompletedTransactions];
  125. }
  126.  
  127.  
  128. //Then this delegate Funtion Will be fired
  129. - (void) paymentQueueRestoreCompletedTransactionsFinished:(SKPaymentQueue *)queue
  130. {
  131.     NSMutableArray *purchasedItemIDs = [[NSMutableArray alloc] init];
  132.    
  133.     //NSLog(@"received restored transactions: %i", queue.transactions.count);
  134.     for (SKPaymentTransaction *transaction in queue.transactions)
  135.     {
  136.         NSString *productID = transaction.payment.productIdentifier;
  137.         [purchasedItemIDs addObject:productID];
  138.        
  139.         if([productID isEqualToString:IN_APP_KEY_BUY_COIN_1])
  140.         {
  141.             //NSError *error = nil;
  142.             //[[NSNotificationCenter defaultCenter] postNotificationName:@"inApSuccessful" object:nil];
  143.         }
  144.     }
  145.    
  146. }
  147.  
  148. #pragma mark StoreKit Delegate
  149.  
  150. -(void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions {
  151.     for (SKPaymentTransaction *transaction in transactions) {
  152.         switch (transaction.transactionState) {
  153.             case SKPaymentTransactionStatePurchasing:
  154.                
  155.                 // show wait view here
  156.                 [self ShowActivityIndicator];
  157.                 break;
  158.                
  159.             case SKPaymentTransactionStatePurchased:
  160.             {
  161.                 [[SKPaymentQueue defaultQueue] finishTransaction:transaction];
  162.                 // remove wait view and unlock feature 2
  163.                
  164.                 [self StopIndicator];
  165.                
  166.                 UIAlertView *tmp = [[UIAlertView alloc]
  167.                                     initWithTitle:@"Complete"
  168.                                     message:@"Thanks for purchasing Credits!"
  169.                                     delegate:self
  170.                                     cancelButtonTitle:nil
  171.                                     otherButtonTitles:@"Ok", nil];
  172.                 tmp.tag = kInApPurchaseSucessAlertTag;
  173.                 [tmp show];
  174.                 [tmp release];
  175.                
  176.                 // do other thing to enable the features
  177.             }
  178.                
  179.                 break;
  180.                
  181.             case SKPaymentTransactionStateRestored:
  182.             {
  183.                 [[SKPaymentQueue defaultQueue] finishTransaction:transaction];
  184.                 // remove wait view here
  185.                 [self StopIndicator];
  186.             }
  187.                 break;
  188.                
  189.             case SKPaymentTransactionStateFailed:
  190.             {
  191.                 if (transaction.error.code != SKErrorPaymentCancelled) {
  192.                     //NSLog(@"Error payment cancelled");
  193.                 }
  194.                 [[SKPaymentQueue defaultQueue] finishTransaction:transaction];
  195.                 // remove wait view here
  196.                 //statusLabel.text = @"Purchase Error!";
  197.                 [self StopIndicator];
  198.             }
  199.                 break;
  200.                
  201.             default:
  202.                 break;
  203.         }
  204.     }
  205. }
  206.  
  207.  
  208.  
  209.  
  210.  
  211.  
  212. -(void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response
  213. {
  214.     // remove wait view here
  215.    
  216.     SKProduct *validProduct = nil;
  217.     int count = [response.products count];
  218.    
  219.     if (count>0) {
  220.         validProduct = [response.products objectAtIndex:0];
  221.        
  222.         //  //#define kStoredData @"com.emirbytes.IAPNoobService"
  223.        
  224.         // SKPayment *payment = [SKPayment paymentWithProductIdentifier:@"com.emirbytes.IAPNoob.01"];
  225.         SKPayment *payment = [SKPayment paymentWithProductIdentifier:self.currentProductId];
  226.         [[SKPaymentQueue defaultQueue] addTransactionObserver:self];
  227.         [[SKPaymentQueue defaultQueue] addPayment:payment];
  228.        
  229.        
  230.     } else {
  231.         [self StopIndicator];
  232.        
  233.         UIAlertView *tmp = [[UIAlertView alloc]
  234.                             initWithTitle:@"Not Available"
  235.                             message:@"No products to purchase"
  236.                             delegate:self
  237.                             cancelButtonTitle:nil
  238.                             otherButtonTitles:@"Ok", nil];
  239.        
  240.         tmp.tag = kInApPurchaseFailedAlertTag;
  241.         [tmp show];
  242.         [tmp release];
  243.     }
  244.    
  245.    
  246. }
  247.  
  248. -(void)requestDidFinish:(SKRequest *)request
  249. {
  250.     [request release];
  251. }
  252.  
  253. -(void)request:(SKRequest *)request didFailWithError:(NSError *)error
  254. {
  255.     //NSLog(@"Failed to connect with error: %@", [error localizedDescription]);
  256.     [[NSNotificationCenter defaultCenter] postNotificationName:@"inApFailed" object:nil];
  257. }
  258.  
  259.  
  260.  
  261. #pragma mark AlertView Delegate
  262. -(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
  263.    
  264.     if( alertView.tag == kInApPurchaseSucessAlertTag)
  265.     {
  266.         [[NSNotificationCenter defaultCenter] postNotificationName:@"inApSuccessful" object:nil];
  267.     }
  268.     else if( alertView.tag == kInApPurchaseFailedAlertTag)
  269.     {
  270.         [[NSNotificationCenter defaultCenter] postNotificationName:@"inApFailed" object:nil];
  271.     }
  272. }
  273.  
  274. -(bool)IS_IOS_7
  275. {
  276.     NSString *currOsVersion = [[UIDevice currentDevice] systemVersion];
  277.    
  278.     float sysver = [currOsVersion floatValue] ;
  279.    
  280.     if(sysver >=7.0f)
  281.         return true;
  282.    
  283.     return false;
  284. }
  285.  
  286. -(void)ShowActivityIndicator
  287. {
  288.     if(!mLoadingView) //    UIAlertView     *mLoadingView;
  289.     {
  290.         if([self  IS_IOS_7])
  291.         {
  292.             mLoadingView = [[UIAlertView alloc] initWithTitle:@"" message:@"Please wait..." delegate:self cancelButtonTitle:nil otherButtonTitles:nil];
  293.            
  294.             UIActivityIndicatorView *actInd = [[[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(125, 50, 30, 30)] autorelease];
  295.             actInd.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhiteLarge;
  296.             [mLoadingView addSubview:actInd];
  297.             [actInd startAnimating];
  298.             [mLoadingView show];
  299.         }
  300.         else
  301.         {
  302.             mLoadingView = [[UIAlertView alloc] initWithTitle:@"" message:@"" delegate:self cancelButtonTitle:nil otherButtonTitles:nil];
  303.            
  304.             UIActivityIndicatorView *actInd = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
  305.             actInd.frame = CGRectMake(128.0f, 45.0f, 25.0f, 25.0f);
  306.             [mLoadingView addSubview:actInd];
  307.             [actInd startAnimating];
  308.             [actInd release];
  309.            
  310.            
  311.             UILabel *l = [[UILabel alloc]init];
  312.             l.frame = CGRectMake(100, -25, 210, 100);
  313.             l.text = @"Please wait...";
  314.             l.font = [UIFont fontWithName:@"Helvetica" size:16];
  315.             l.textColor = [UIColor whiteColor];
  316.             l.shadowColor = [UIColor blackColor];
  317.             l.shadowOffset = CGSizeMake(1.0, 1.0);
  318.             l.backgroundColor = [UIColor clearColor];
  319.             [mLoadingView addSubview:l];
  320.             [l release];
  321.         }
  322.     }
  323.    
  324.     [mLoadingView show];
  325. }
  326.  
  327. -(void)StopIndicator
  328. {
  329.     [mLoadingView dismissWithClickedButtonIndex:0 animated:NO];
  330. }
  331.  
  332.  
  333. @end