ViewController.swift 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. //
  2. // ViewController.swift
  3. // SwiftyStoreKit
  4. //
  5. // Created by Andrea Bizzotto on 03/09/2015.
  6. // Copyright © 2015 musevisions. All rights reserved.
  7. //
  8. import UIKit
  9. import SwiftyStoreKit
  10. class ViewController: UIViewController {
  11. let AppBundleId = "com.musevisions.iOS.SwiftyStoreKit"
  12. override func viewDidLoad() {
  13. super.viewDidLoad()
  14. }
  15. func showMessage(title: String, message: String) {
  16. guard let _ = self.presentedViewController else {
  17. let alert = UIAlertController(title: title, message: message, preferredStyle: .Alert)
  18. alert.addAction(UIAlertAction(title: "OK", style: .Cancel, handler: nil))
  19. self.presentViewController(alert, animated: true, completion: nil)
  20. return
  21. }
  22. }
  23. @IBAction func purchase1() {
  24. purchase("1")
  25. }
  26. @IBAction func purchase2() {
  27. purchase("2")
  28. }
  29. @IBAction func purchase3() {
  30. purchase("3")
  31. }
  32. func purchase(no: String) {
  33. SwiftyStoreKit.sharedInstance.purchaseProduct(AppBundleId + ".purchase" + no) { result in
  34. switch result {
  35. case .Success(let productId):
  36. self.showMessage("Thank You", message: "Purchase completed")
  37. print("Purchase Success: \(productId)")
  38. break
  39. case .Error(let error):
  40. if case ResponseError.RequestFailed(let internalError) = error where internalError.code == 0 {
  41. self.showMessage("Purchase failed", message: "Please check your Internet connection or try again later")
  42. }
  43. else {
  44. self.showMessage("Purchase failed", message: "Unknown error. Please contact support")
  45. }
  46. print("Purchase Failed: \(error)")
  47. break
  48. }
  49. }
  50. }
  51. @IBAction func restorePurchases() {
  52. SwiftyStoreKit.sharedInstance.restorePurchases() { result in
  53. switch result {
  54. case .Success(let productId):
  55. self.showMessage("Purchases Restored", message: "All purchases have been restored")
  56. print("Restore Success: \(productId)")
  57. break
  58. case .NothingToRestore:
  59. self.showMessage("Nothing to restore", message: "No previous purchases were found")
  60. print("Nothing to Restore")
  61. break
  62. case .Error(let error):
  63. print("Restore Failed: \(error)")
  64. break
  65. }
  66. }
  67. }
  68. }