SHARE
TWEET

Untitled

a guest Jul 22nd, 2019 61 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class RemoveAds: UIViewController , SKProductsRequestDelegate, SKPaymentTransactionObserver {
  2.    
  3.       var product_id: String?
  4.  
  5.     override func viewDidLoad() {
  6.         product_id = "ads product id"
  7.         super.viewDidLoad()
  8.        
  9.         SKPaymentQueue.default().add(self)
  10.        
  11.         //Check if product is purchased
  12.         if (UserDefaults.standard.bool(forKey: "purchased")){
  13.            
  14.             // Hide ads
  15.             //adView.hidden = true
  16.            
  17.             // removeAds ();
  18.            
  19.         } else {
  20.             print("Should show ads...")
  21.            
  22.         }
  23.        
  24.     }
  25.  
  26.     override func didReceiveMemoryWarning() {
  27.         super.didReceiveMemoryWarning()
  28.         // Dispose of any resources that can be recreated.
  29.     }
  30.    
  31.    
  32.    
  33.    
  34.     func buyProduct(product: SKProduct){
  35.         print("Sending the Payment Request to Apple");
  36.         let payment = SKPayment(product: product)
  37.         SKPaymentQueue.default().add(payment);
  38.        
  39.     }
  40.    
  41.    
  42.     func productsRequest (_ request: SKProductsRequest, didReceive response: SKProductsResponse) {
  43.        
  44.         let count : Int = response.products.count
  45.         if (count>0) {
  46.             let validProduct: SKProduct = response.products[0] as SKProduct
  47.             if (validProduct.productIdentifier == self.product_id) {
  48.                 print(validProduct.localizedTitle)
  49.                 print(validProduct.localizedDescription)
  50.                 print(validProduct.price)
  51.                 buyProduct(product: validProduct);
  52.             } else {
  53.                 PublicMethod.hideHUD(view: self)
  54.                 print(validProduct.productIdentifier)
  55.                  //self.view.makeToast("Error Fetching product information")
  56.             }
  57.         } else {
  58.             PublicMethod.hideHUD(view: self)
  59.             print("nothing")
  60.              self.view.makeToast("Error Fetching product information")
  61.         }
  62.     }
  63.    
  64.    
  65.     func request(_ request: SKRequest, didFailWithError error: Error) {
  66.         print("Error Fetching product information");
  67.         PublicMethod.hideHUD(view: self)
  68.         self.view.makeToast("Error Fetching product information")
  69.     }
  70.    
  71.     func paymentQueue(_ queue: SKPaymentQueue,
  72.                       updatedTransactions transactions: [SKPaymentTransaction])
  73.        
  74.     {
  75.         print("Received Payment Transaction Response from Apple");
  76.        
  77.         for transaction:AnyObject in transactions {
  78.             if let trans:SKPaymentTransaction = transaction as? SKPaymentTransaction{
  79.                 switch trans.transactionState {
  80.                 case .purchased:
  81.                     PublicMethod.hideHUD(view: self)
  82.                     print("Product Purchased");
  83.                     self.view.makeToast("Product Purchased")
  84.                     SKPaymentQueue.default().finishTransaction(transaction as! SKPaymentTransaction)
  85.                     // Handle the purchase
  86.                     UserDefaults.standard.set(true , forKey: "purchased")
  87.                    
  88.                       self.present(Utilities().showAlertContrller(title: "Purchase Success", message: "You've successfully purchased"), animated: true, completion: nil)
  89.                    
  90.                     // removeAds ();
  91.                     //adView.hidden = true
  92.                     break;
  93.                 case .failed:
  94.                     PublicMethod.hideHUD(view: self)
  95.                     self.view.makeToast("Purchased Failed")
  96.                     print("Purchased Failed");
  97.                     SKPaymentQueue.default().finishTransaction(transaction as! SKPaymentTransaction)
  98.                      self.present(Utilities().showAlertContrller(title: "Purchase failed!", message: trans.error!.localizedDescription), animated: true, completion: nil)
  99.                     break;
  100.                    
  101.                    
  102.                    
  103.                 case .restored:
  104.                     PublicMethod.hideHUD(view: self)
  105.                     self.view.makeToast("Already Purchased")
  106.                     print("Already Purchased");
  107.                     SKPaymentQueue.default().finishTransaction(transaction as! SKPaymentTransaction)
  108.                    
  109.                     // Handle the purchase
  110.                     UserDefaults.standard.set(true , forKey: "purchased")
  111.                    
  112.                      self.present(Utilities().showAlertContrller(title: "Restore Success", message: "You've successfully restored your purchase!"), animated: true, completion: nil)
  113.                    
  114.                     //removeAds ();
  115.                     //adView.hidden = true
  116.                     break;
  117.                 default:
  118.                      PublicMethod.hideHUD(view: self)
  119.                     break;
  120.                 }
  121.             }
  122.         }
  123.        
  124.     }
  125.    
  126.    
  127.     // MARK: - Restore In App Purchase
  128.     @IBAction func restoreAction(_ sender: UIButton) {
  129.        
  130.         if (SKPaymentQueue.canMakePayments()) {
  131.              PublicMethod.showHud(text: "Restoring...", view: self)
  132.             SKPaymentQueue.default().add(self)
  133.             SKPaymentQueue.default().restoreCompletedTransactions()
  134.         } else {
  135.             // show error
  136.             PublicMethod.hideHUD(view: self)
  137.             self.view.makeToast("Can't make payment")
  138.                UIAlertView(title: "Notice", message: "Can't make purchases", delegate: nil, cancelButtonTitle: "OK").show()
  139.         }
  140.        
  141.     }
  142.    
  143.  
  144.    
  145.     // MARK: - Restore In App Purchase
  146.     @IBAction func removeAds(_ sender: UIButton) {
  147.        
  148.  
  149.        
  150.         print("About to fetch the product...")
  151.        
  152.         PublicMethod.showHud(text: "Purchasing...", view: self)
  153.        
  154.         // Can make payments
  155.         if (SKPaymentQueue.canMakePayments())
  156.         {
  157.             let productID:NSSet = NSSet(object: self.product_id!);
  158.             let productsRequest:SKProductsRequest = SKProductsRequest(productIdentifiers: productID as! Set<String>);
  159.             productsRequest.delegate = self;
  160.             productsRequest.start();
  161.             print("Fetching Products");
  162.             self.view.makeToast("Purchasing Products")
  163.         }else{
  164.             PublicMethod.hideHUD(view: self)
  165.             print("Can't make purchases");
  166.             self.view.makeToast("Can't make purchases")
  167.            
  168.              UIAlertView(title: "Notice", message: "Can't make purchases", delegate: nil, cancelButtonTitle: "OK").show()
  169.            
  170.            
  171.            
  172.            
  173.         }
  174.        
  175.        
  176.        
  177.     }
  178.    
  179.    
  180.    
  181.    
  182.     func paymentQueueRestoreCompletedTransactionsFinished(_ queue: SKPaymentQueue) {
  183.          PublicMethod.hideHUD(view: self)
  184.         // TODO: Product is restored and make sure the functionality/availability of purchased product
  185.         // Handle the purchase
  186.        
  187.        
  188.        
  189.         print("received restored transactions: %i", queue.transactions.count);
  190.         if(queue.transactions.count > 0){
  191.            
  192.             let productIdentifiers = NSSet(array: [product_id])
  193.             let productRequest = SKProductsRequest(productIdentifiers: productIdentifiers as! Set<String>)
  194.            
  195.             productRequest.delegate = self
  196.             productRequest.start()
  197.         }else{
  198.             UIAlertView(title: "Notice", message: "You don't buy. Please buy for remove ads", delegate: nil, cancelButtonTitle: "OK").show()
  199.         }
  200.        
  201.        
  202.        
  203. //
  204. //         PublicMethod.hideHUD(view: self)
  205. //
  206. //        UserDefaults.standard.set(true , forKey: "purchased")
  207. //
  208. //        self.present(Utilities().showAlertContrller(title: "Restore Success", message: "You've successfully restored your purchase!"), animated: true, completion: nil)
  209.  
  210.    
  211.        
  212.        
  213.     }
  214.    
  215.    
  216.    
  217.     //If an error occurs, the code will go to this function
  218.     func paymentQueue(_ queue: SKPaymentQueue, restoreCompletedTransactionsFailedWithError error: Error) {
  219.         //Handle Error
  220.          PublicMethod.hideHUD(view: self)
  221.          self.present(Utilities().showAlertContrller(title: "Restore Failed", message: "You've failed to restore your purchase!"), animated: true, completion: nil)
  222.        
  223.     }
  224.  
  225. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top