Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // ViewController.swift
- // Scanner
- //
- // Created by Anil on 03/03/15.
- // Copyright (c) 2015 Variya Soft Solutions. All rights reserved.
- //
- import UIKit
- import StoreKit
- class ViewController: UIViewController, SKPaymentTransactionObserver, SKProductsRequestDelegate, ChartboostDelegate {
- @IBOutlet weak var buyButton: UIButton!
- var product: SKProduct?
- var productID = "FullAccess"
- var isProductRecive = false
- override func viewDidLoad() {
- super.viewDidLoad()
- self.buyButton.hidden = self.hideIAPButton()
- SKPaymentQueue.defaultQueue().addTransactionObserver(self)
- getProductInfo()
- }
- override func viewWillAppear(animated: Bool) {
- if CBEvent == .Cached{
- Chartboost.showInterstitial(CBLocationDefault)
- }else if CBEvent == .None{
- Chartboost.cacheInterstitial(CBLocationDefault)
- }
- }
- @IBAction func buyFullAccess(sender: AnyObject) {
- if Reachability.isConnectedToNetwork() {
- if isProductRecive{
- let payment = SKPayment(product: product)
- SKPaymentQueue.defaultQueue().addPayment(payment)
- }else{
- let alert = UIAlertView()
- alert.title = "Alert"
- alert.message = "Retriving product please wait."
- alert.addButtonWithTitle("Ok")
- alert.show()
- }
- } else {
- var alert = UIAlertController(title: "Alert", message: "you need internet connection to Purchase this product", preferredStyle: UIAlertControllerStyle.Alert)
- alert.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default, handler: nil))
- self.presentViewController(alert, animated: true, completion: nil)
- }
- }
- func getProductInfo()
- {
- if SKPaymentQueue.canMakePayments() {
- let request = SKProductsRequest(productIdentifiers: NSSet(objects: self.productID))
- request.delegate = self
- request.start()
- } else {
- let alert = UIAlertView()
- alert.title = "Alert"
- alert.message = "Please enable In App Purchase in Settings."
- alert.addButtonWithTitle("Ok")
- alert.show()
- }
- }
- func productsRequest(request: SKProductsRequest!, didReceiveResponse response: SKProductsResponse!) {
- var products = response.products
- if (products.count != 0) {
- product = products[0] as? SKProduct
- self.isProductRecive = true
- } else {
- let alert = UIAlertView()
- alert.title = "Alert"
- alert.message = "Product not found"
- alert.addButtonWithTitle("Ok")
- alert.show()
- }
- products = response.invalidProductIdentifiers
- for product in products
- {
- println("Product not found: \(product)")
- }
- }
- func paymentQueue(queue: SKPaymentQueue!, updatedTransactions transactions: [AnyObject]!) {
- for transaction in transactions as [SKPaymentTransaction] {
- switch transaction.transactionState {
- case .Restored:
- SKPaymentQueue.defaultQueue().finishTransaction(transaction)
- case .Purchased:
- self.unlockFeature()
- self.buyButton.hidden = true
- SKPaymentQueue.defaultQueue().finishTransaction(transaction)
- case .Failed:
- SKPaymentQueue.defaultQueue().finishTransaction(transaction)
- default:
- break
- }
- }
- }
- func request(request: SKRequest!, didFailWithError error: NSError!) {
- println("There was an error");
- }
- func unlockFeature(){
- let fileManger = NSFileManager.defaultManager()
- var error : NSError?
- var doumentDirectoryPath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] as NSString
- //For QRCode
- var qrGenerator = QRCodeGenerator()
- qrGenerator.stopSegue = false
- let destinationPath = doumentDirectoryPath.stringByAppendingPathComponent("QRGenerator.plist")
- var data : NSMutableDictionary?
- data = NSMutableDictionary(contentsOfFile: destinationPath)
- var QRCount : Int = data?.objectForKey("QRCount") as Int
- QRCount += 15
- data?.setObject(QRCount, forKey: "QRCount")
- data?.writeToFile(destinationPath, atomically: true)
- //For BarCode
- let destinationPathforBar = doumentDirectoryPath.stringByAppendingPathComponent("BarGenerator1.plist")
- var data2 : NSMutableDictionary?
- data2 = NSMutableDictionary(contentsOfFile: destinationPathforBar)
- var BarCount : Int = data2?.objectForKey("BarCount") as Int
- BarCount += 15
- data2?.setObject(BarCount, forKey: "BarCount")
- data2?.writeToFile(destinationPathforBar, atomically: true)
- // For inApp
- let destinationPathForinApp = doumentDirectoryPath.stringByAppendingPathComponent("inApp.plist")
- var data1 : NSMutableDictionary?
- data1 = NSMutableDictionary(contentsOfFile: destinationPathForinApp)
- var IsIPADone : Bool = data1?.objectForKey("inAppDone") as Bool
- IsIPADone = true
- data1?.setObject(IsIPADone, forKey: "inAppDone")
- data1?.writeToFile(destinationPathForinApp, atomically: true)
- }
- func hideIAPButton() -> Bool{
- let fileManger = NSFileManager.defaultManager()
- var error : NSError?
- var doumentDirectoryPath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] as NSString
- let destinationPathForinApp = doumentDirectoryPath.stringByAppendingPathComponent("inApp.plist")
- var data1 : NSMutableDictionary?
- data1 = NSMutableDictionary(contentsOfFile: destinationPathForinApp)
- var IsIPADone : Bool = data1?.objectForKey("inAppDone") as Bool
- return IsIPADone
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment