khenid

Untitled

Mar 13th, 2015
319
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //
  2. //  ViewController.swift
  3. //  Scanner
  4. //
  5. //  Created by Anil on 03/03/15.
  6. //  Copyright (c) 2015 Variya Soft Solutions. All rights reserved.
  7. //
  8.  
  9. import UIKit
  10. import StoreKit
  11.  
  12. class ViewController: UIViewController, SKPaymentTransactionObserver, SKProductsRequestDelegate, ChartboostDelegate {
  13.  
  14.     @IBOutlet weak var buyButton: UIButton!
  15.     var product: SKProduct?
  16.     var productID = "FullAccess"
  17.    
  18.     var isProductRecive = false
  19.  
  20.     override func viewDidLoad() {
  21.         super.viewDidLoad()
  22.        
  23.        
  24.         self.buyButton.hidden = self.hideIAPButton()
  25.         SKPaymentQueue.defaultQueue().addTransactionObserver(self)
  26.         getProductInfo()
  27.     }
  28.     override func viewWillAppear(animated: Bool) {
  29.         if CBEvent == .Cached{
  30.             Chartboost.showInterstitial(CBLocationDefault)
  31.         }else if CBEvent == .None{
  32.             Chartboost.cacheInterstitial(CBLocationDefault)
  33.         }
  34.     }
  35.     @IBAction func buyFullAccess(sender: AnyObject) {
  36.        
  37.         if Reachability.isConnectedToNetwork() {
  38.            
  39.             if isProductRecive{
  40.                 let payment = SKPayment(product: product)
  41.                 SKPaymentQueue.defaultQueue().addPayment(payment)
  42.             }else{
  43.                
  44.                 let alert = UIAlertView()
  45.                 alert.title = "Alert"
  46.                 alert.message = "Retriving product please wait."
  47.                 alert.addButtonWithTitle("Ok")
  48.                 alert.show()
  49.             }
  50.            
  51.         } else {
  52.            
  53.             var alert = UIAlertController(title: "Alert", message: "you need internet connection to Purchase this product", preferredStyle: UIAlertControllerStyle.Alert)
  54.             alert.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default, handler: nil))
  55.             self.presentViewController(alert, animated: true, completion: nil)
  56.         }
  57.     }
  58.    
  59.     func getProductInfo()
  60.     {
  61.         if SKPaymentQueue.canMakePayments() {
  62.             let request = SKProductsRequest(productIdentifiers: NSSet(objects: self.productID))
  63.             request.delegate = self
  64.             request.start()
  65.         } else {
  66.             let alert = UIAlertView()
  67.             alert.title = "Alert"
  68.             alert.message = "Please enable In App Purchase in Settings."
  69.             alert.addButtonWithTitle("Ok")
  70.             alert.show()
  71.         }
  72.     }
  73.    
  74.     func productsRequest(request: SKProductsRequest!, didReceiveResponse response: SKProductsResponse!) {
  75.        
  76.         var products = response.products
  77.        
  78.         if (products.count != 0) {
  79.             product = products[0] as? SKProduct
  80.             self.isProductRecive = true
  81.  
  82.         } else {
  83.            
  84.             let alert = UIAlertView()
  85.             alert.title = "Alert"
  86.             alert.message = "Product not found"
  87.             alert.addButtonWithTitle("Ok")
  88.             alert.show()
  89.         }
  90.        
  91.         products = response.invalidProductIdentifiers
  92.        
  93.         for product in products
  94.         {
  95.             println("Product not found: \(product)")
  96.         }
  97.     }
  98.    
  99.     func paymentQueue(queue: SKPaymentQueue!, updatedTransactions transactions: [AnyObject]!) {
  100.        
  101.         for transaction in transactions as [SKPaymentTransaction] {
  102.            
  103.             switch transaction.transactionState {
  104.                
  105.             case .Restored:
  106.                
  107.                 SKPaymentQueue.defaultQueue().finishTransaction(transaction)
  108.                
  109.             case .Purchased:
  110.                
  111.                 self.unlockFeature()
  112.                 self.buyButton.hidden = true
  113.                 SKPaymentQueue.defaultQueue().finishTransaction(transaction)
  114.                
  115.             case .Failed:
  116.                
  117.                 SKPaymentQueue.defaultQueue().finishTransaction(transaction)
  118.                
  119.             default:
  120.                 break
  121.             }
  122.         }
  123.     }
  124.    
  125.     func request(request: SKRequest!, didFailWithError error: NSError!) {
  126.        
  127.         println("There was an error");
  128.     }
  129.    
  130.     func unlockFeature(){
  131.        
  132.        
  133.         let fileManger = NSFileManager.defaultManager()
  134.         var error : NSError?
  135.         var doumentDirectoryPath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] as NSString
  136.        
  137.         //For QRCode
  138.         var qrGenerator = QRCodeGenerator()
  139.         qrGenerator.stopSegue = false
  140.         let destinationPath = doumentDirectoryPath.stringByAppendingPathComponent("QRGenerator.plist")
  141.         var data : NSMutableDictionary?
  142.         data = NSMutableDictionary(contentsOfFile: destinationPath)
  143.         var QRCount : Int = data?.objectForKey("QRCount") as Int
  144.         QRCount += 15
  145.         data?.setObject(QRCount, forKey: "QRCount")
  146.         data?.writeToFile(destinationPath, atomically: true)
  147.        
  148.         //For BarCode
  149.         let destinationPathforBar = doumentDirectoryPath.stringByAppendingPathComponent("BarGenerator1.plist")
  150.         var data2 : NSMutableDictionary?
  151.         data2 = NSMutableDictionary(contentsOfFile: destinationPathforBar)
  152.         var BarCount : Int = data2?.objectForKey("BarCount") as Int
  153.         BarCount += 15
  154.         data2?.setObject(BarCount, forKey: "BarCount")
  155.         data2?.writeToFile(destinationPathforBar, atomically: true)
  156.  
  157.         // For inApp
  158.         let destinationPathForinApp = doumentDirectoryPath.stringByAppendingPathComponent("inApp.plist")
  159.         var data1 : NSMutableDictionary?
  160.         data1 = NSMutableDictionary(contentsOfFile: destinationPathForinApp)
  161.         var IsIPADone : Bool = data1?.objectForKey("inAppDone") as Bool
  162.         IsIPADone = true
  163.         data1?.setObject(IsIPADone, forKey: "inAppDone")
  164.         data1?.writeToFile(destinationPathForinApp, atomically: true)
  165.     }
  166.    
  167.     func hideIAPButton() -> Bool{
  168.        
  169.         let fileManger = NSFileManager.defaultManager()
  170.         var error : NSError?
  171.         var doumentDirectoryPath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] as NSString
  172.        
  173.         let destinationPathForinApp = doumentDirectoryPath.stringByAppendingPathComponent("inApp.plist")
  174.         var data1 : NSMutableDictionary?
  175.         data1 = NSMutableDictionary(contentsOfFile: destinationPathForinApp)
  176.         var IsIPADone : Bool = data1?.objectForKey("inAppDone") as Bool
  177.        
  178.         return IsIPADone
  179.     }
  180. }
Advertisement
Add Comment
Please, Sign In to add comment