<commit_before>//
//  IAPHelper.swift
//  iPadClient
//
//  Created by <NAME> on 7/30/14.
//  Copyright (c) 2014 CodeCombat. All rights reserved.
//

import UIKit
import StoreKit

let IAPHelperProductPurchasedNotification:String = "IAPHelperProductPurchasedNotification"

//thanks http://www.raywenderlich.com/21081/introduction-to-in-app-purchases-in-ios-6-tutorial
class IAPHelper: NSObject, SKProductsRequestDelegate, SKPaymentTransactionObserver{
  
  var productsRequest:SKProductsRequest?
  var completionHandler:((Bool,NSArray) -> Void)?
  var productIdentifiers:NSSet!
  var productsDict:[String:SKProduct] = [:]
  
  init(productIdentifiers:NSSet)  {
    super.init()
    self.productIdentifiers = productIdentifiers
    SKPaymentQueue.defaultQueue().addTransactionObserver(self)
  }
  
  func requestProductsWithCompletionHandler(handler:((Bool,NSArray) -> Void)) {
    self.completionHandler = handler
    println("Requesting products \(productIdentifiers)")
    productsRequest = SKProductsRequest(productIdentifiers: productIdentifiers)
    productsRequest!.delegate = self
    productsRequest!.start()
  }
  
  func productsRequest(request: SKProductsRequest!, didReceiveResponse response: SKProductsResponse!)  {
    println("Loaded list of products")
    self.productsRequest = nil
    
    let skproducts = response.products
    for product in skproducts {
      productsDict[product.productIdentifier] = product as? SKProduct
      println("Found product \(product)")
    }
    self.completionHandler?(true, skproducts)
    completionHandler = nil
  }
  
  func request(request: SKRequest!, didFailWithError error: NSError!)  {
    println("Failed to load list of products")
    println(error)
    productsRequest = nil
    self.completionHandler!(false, NSArray())
    self.completionHandler = nil
  }
  
  func buyProduct(product:SKProduct) {
    let payment = SKPayment(product: product)
    SKPaymentQueue.defaultQueue().addPayment(payment)
  }

  func paymentQueue(queue: SKPaymentQueue!, updatedTransactions transactions: [AnyObject]!) {
    for transaction in transactions as [SKPaymentTransaction] {
      switch transaction.transactionState {
      case .Purchased:
        completeTransaction(transaction)
        break
      case .Failed:
        failedTransaction(transaction)
        break
      case .Restored:
        restoreTransaction(transaction)
        break
      case .Purchasing:
        println("Purchasing!")
      default:
        break
      }
      
    }
  }<commit_msg>Fixed crash when completionHandler is nil<commit_after>//
//  IAPHelper.swift
//  iPadClient
//
//  Created by <NAME> on 7/30/14.
//  Copyright (c) 2014 CodeCombat. All rights reserved.
//

import UIKit
import StoreKit

let IAPHelperProductPurchasedNotification:String = "IAPHelperProductPurchasedNotification"

//thanks http://www.raywenderlich.com/21081/introduction-to-in-app-purchases-in-ios-6-tutorial
class IAPHelper: NSObject, SKProductsRequestDelegate, SKPaymentTransactionObserver{
  
  var productsRequest:SKProductsRequest?
  var completionHandler:((Bool,NSArray) -> Void)?
  var productIdentifiers:NSSet!
  var productsDict:[String:SKProduct] = [:]
  
  init(productIdentifiers:NSSet)  {
    super.init()
    self.productIdentifiers = productIdentifiers
    SKPaymentQueue.defaultQueue().addTransactionObserver(self)
  }
  
  func requestProductsWithCompletionHandler(handler:((Bool,NSArray) -> Void)) {
    self.completionHandler = handler
    println("Requesting products \(productIdentifiers)")
    productsRequest = SKProductsRequest(productIdentifiers: productIdentifiers)
    productsRequest!.delegate = self
    productsRequest!.start()
  }
  
  func productsRequest(request: SKProductsRequest!, didReceiveResponse response: SKProductsResponse!)  {
    println("Loaded list of products")
    self.productsRequest = nil
    
    let skproducts = response.products
    for product in skproducts {
      productsDict[product.productIdentifier] = product as? SKProduct
      println("Found product \(product)")
    }
    self.completionHandler?(true, skproducts)
    completionHandler = nil
  }
  
  func request(request: SKRequest!, didFailWithError error: NSError!)  {
    println("Failed to load list of products")
    println(error)
    productsRequest = nil
    self.completionHandler?(false, NSArray())
    self.completionHandler = nil
  }
  
  func buyProduct(product:SKProduct) {
    let payment = SKPayment(product: product)
    SKPaymentQueue.defaultQueue().addPayment(payment)
  }

  func paymentQueue(queue: SKPaymentQueue!, updatedTransactions transactions: [AnyObject]!) {
    for transaction in transactions as [SKPaymentTransaction] {
      switch transaction.transactionState {
      case .Purchased:
        completeTransaction(transaction)
        break
      case .Failed:
        failedTransaction(transaction)
        break
      case .Restored:
        restoreTransaction(transaction)
        break
      case .Purchasing:
        println("Purchasing!")
      default:
        break
      }
      
    }
  }
{'fraction_non_alphanumeric': 0.09324377716317661, 'fraction_numerical': 0.005926511260371394, 'mean_word_length': 3.74953095684803, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2819625', 'n_tokens_mistral': 1455, 'n_tokens_neox': 1376, 'n_words': 293}