ViewController.swift 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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, handler: ((NSModalResponse) -> Void)? = nil) {
  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. handler?(response)
  24. }
  25. } else {
  26. let response = alert.runModal()
  27. handler?(response)
  28. }
  29. return
  30. }
  31. // MARK: actions
  32. @IBAction func getInfo1(sender: AnyObject?) {
  33. getInfo("1")
  34. }
  35. @IBAction func getInfo2(sender: AnyObject!) {
  36. getInfo("2")
  37. }
  38. @IBAction func purchase1(sender: AnyObject!) {
  39. purchase("1")
  40. }
  41. @IBAction func purchase2(sender: AnyObject!) {
  42. purchase("2")
  43. }
  44. func getInfo(no: String) {
  45. SwiftyStoreKit.retrieveProductInfo(AppBundleId + ".purchase" + no) { result in
  46. switch result {
  47. case .Success(let product):
  48. let priceString = NSNumberFormatter.localizedStringFromNumber(product.price ?? 0, numberStyle: .CurrencyStyle)
  49. self.showMessage(product.localizedTitle ?? "no title", message: "\(product.localizedDescription) - \(priceString)")
  50. break
  51. case .Error(let error):
  52. self.showMessage("Could not retrieve product info", message: String(error))
  53. break
  54. }
  55. }
  56. }
  57. func purchase(no: String) {
  58. SwiftyStoreKit.purchaseProduct(AppBundleId + ".purchase" + no) { result in
  59. switch result {
  60. case .Success(let productId):
  61. self.showMessage("Thank You", message: "Purchase completed")
  62. print("Purchase Success: \(productId)")
  63. break
  64. case .Error(let error):
  65. if case ResponseError.RequestFailed(let internalError) = error where internalError.domain == SKErrorDomain {
  66. self.showMessage("Purchase failed", message: "Please check your Internet connection or try again later")
  67. }
  68. else if (error as NSError).domain == SKErrorDomain {
  69. self.showMessage("Purchase failed", message: "Please check your Internet connection or try again later")
  70. }
  71. else {
  72. self.showMessage("Purchase failed", message: "Unknown error. Please contact support")
  73. }
  74. print("Purchase Failed: \(error)")
  75. break
  76. }
  77. }
  78. }
  79. @IBAction func restorePurchases(sender: AnyObject?) {
  80. SwiftyStoreKit.restorePurchases() { result in
  81. switch result {
  82. case .Success(let productId):
  83. self.showMessage("Purchases Restored", message: "All purchases have been restored")
  84. print("Restore Success: \(productId)")
  85. break
  86. case .NothingToRestore:
  87. self.showMessage("Nothing to restore", message: "No previous purchases were found")
  88. print("Nothing to Restore")
  89. break
  90. case .Error(let error):
  91. print("Restore Failed: \(error)")
  92. break
  93. }
  94. }
  95. }
  96. @IBAction func verifyReceipt(ender: AnyObject?) {
  97. SwiftyStoreKit.verifyReceipt() { result in
  98. switch result {
  99. case .Success(let receipt):
  100. self.showMessage("Receipt verified", message: "Receipt verified remotly")
  101. print("Verify receipt Success: \(receipt)")
  102. break
  103. case .Error(let error):
  104. print("Verify receipt Failed: \(error)")
  105. self.showMessage("Receipt verification failed", message: "The application will exit to create receipt data. You must have signed the application for app store with your developper id to test") { response in
  106. exit(ReceiptExitCode.NotValid.rawValue)
  107. }
  108. break
  109. }
  110. }
  111. }
  112. }