ViewController.swift 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. //
  2. // ViewController.swift
  3. // SwiftStoreOSXDemo
  4. //
  5. // Created by phimage on 22/12/15.
  6. // Copyright © 2015 musevisions. All rights reserved.
  7. //
  8. import Cocoa
  9. import StoreKit
  10. import SwiftyStoreKit
  11. class ViewController: NSViewController {
  12. let AppBundleId = "com.musevisions.OSX.SwiftyStoreKit"
  13. override func viewDidLoad() {
  14. super.viewDidLoad()
  15. }
  16. func showMessage(title: String, message: String) {
  17. let alert: NSAlert = NSAlert()
  18. alert.messageText = title
  19. alert.informativeText = message
  20. alert.alertStyle = NSAlertStyle.InformationalAlertStyle
  21. if let window = NSApplication.sharedApplication().keyWindow {
  22. alert.beginSheetModalForWindow(window) { (response: NSModalResponse) in
  23. }
  24. } else {
  25. alert.runModal()
  26. }
  27. return
  28. }
  29. // MARK: actions
  30. @IBAction func getInfo1(sender: AnyObject?) {
  31. getInfo("1")
  32. }
  33. @IBAction func getInfo2(sender: AnyObject!) {
  34. getInfo("2")
  35. }
  36. @IBAction func purchase1(sender: AnyObject!) {
  37. purchase("1")
  38. }
  39. @IBAction func purchase2(sender: AnyObject!) {
  40. purchase("2")
  41. }
  42. func getInfo(no: String) {
  43. SwiftyStoreKit.retrieveProductInfo(AppBundleId + ".purchase" + no) { result in
  44. switch result {
  45. case .Success(let product):
  46. let priceString = NSNumberFormatter.localizedStringFromNumber(product.price ?? 0, numberStyle: .CurrencyStyle)
  47. self.showMessage(product.localizedTitle ?? "no title", message: "\(product.localizedDescription) - \(priceString)")
  48. break
  49. case .Error(let error):
  50. self.showMessage("Could not retrieve product info", message: String(error))
  51. break
  52. }
  53. }
  54. }
  55. func purchase(no: String) {
  56. SwiftyStoreKit.purchaseProduct(AppBundleId + ".purchase" + no) { result in
  57. switch result {
  58. case .Success(let productId):
  59. self.showMessage("Thank You", message: "Purchase completed")
  60. print("Purchase Success: \(productId)")
  61. break
  62. case .Error(let error):
  63. if case ResponseError.RequestFailed(let internalError) = error where internalError.domain == SKErrorDomain {
  64. self.showMessage("Purchase failed", message: "Please check your Internet connection or try again later")
  65. }
  66. else if (error as NSError).domain == SKErrorDomain {
  67. self.showMessage("Purchase failed", message: "Please check your Internet connection or try again later")
  68. }
  69. else {
  70. self.showMessage("Purchase failed", message: "Unknown error. Please contact support")
  71. }
  72. print("Purchase Failed: \(error)")
  73. break
  74. }
  75. }
  76. }
  77. @IBAction func restorePurchases(sender: AnyObject?) {
  78. SwiftyStoreKit.restorePurchases() { result in
  79. switch result {
  80. case .Success(let productId):
  81. self.showMessage("Purchases Restored", message: "All purchases have been restored")
  82. print("Restore Success: \(productId)")
  83. break
  84. case .NothingToRestore:
  85. self.showMessage("Nothing to restore", message: "No previous purchases were found")
  86. print("Nothing to Restore")
  87. break
  88. case .Error(let error):
  89. print("Restore Failed: \(error)")
  90. break
  91. }
  92. }
  93. }
  94. @IBAction func verifyReceipt(ender: AnyObject?) {
  95. SwiftyStoreKit.verifyReceipt() { result in
  96. switch result {
  97. case .Success(let receipt):
  98. self.showMessage("Receipt verified", message: "Receipt verified remotly")
  99. print("Verify receipt Success: \(receipt)")
  100. break
  101. case .Error(let error):
  102. print("Verify receipt Failed: \(error)")
  103. self.showMessage("Receipt verification failed", message: "The application will exit to create receipt data. You must have signed the application with your developper id to test and be outside of XCode")
  104. exit(ReceiptExitCode.NotValid.rawValue)
  105. break
  106. }
  107. }
  108. }
  109. }