ViewController.swift 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. // MARK: actions
  14. @IBAction func getInfo1(sender: AnyObject?) {
  15. getInfo("1")
  16. }
  17. @IBAction func getInfo2(sender: AnyObject!) {
  18. getInfo("2")
  19. }
  20. @IBAction func purchase1(sender: AnyObject!) {
  21. purchase("1")
  22. }
  23. @IBAction func purchase2(sender: AnyObject!) {
  24. purchase("2")
  25. }
  26. func getInfo(no: String) {
  27. SwiftyStoreKit.retrieveProductInfo(AppBundleId + ".purchase" + no) { result in
  28. self.showAlert(self.alertForProductRetrievalInfo(result))
  29. }
  30. }
  31. func purchase(no: String) {
  32. SwiftyStoreKit.purchaseProduct(AppBundleId + ".purchase" + no) { result in
  33. self.showAlert(self.alertForPurchaseResult(result))
  34. }
  35. }
  36. @IBAction func restorePurchases(sender: AnyObject?) {
  37. SwiftyStoreKit.restorePurchases() { result in
  38. self.showAlert(self.alertForRestorePurchases(result))
  39. }
  40. }
  41. @IBAction func verifyReceipt(ender: AnyObject?) {
  42. SwiftyStoreKit.verifyReceipt() { result in
  43. self.showAlert(self.alertForVerifyReceipt(result)) { response in
  44. exit(ReceiptExitCode.NotValid.rawValue)
  45. }
  46. }
  47. }
  48. }
  49. // MARK: User facing alerts
  50. extension ViewController {
  51. func alertWithTitle(title: String, message: String) -> NSAlert {
  52. let alert: NSAlert = NSAlert()
  53. alert.messageText = title
  54. alert.informativeText = message
  55. alert.alertStyle = NSAlertStyle.InformationalAlertStyle
  56. return alert
  57. }
  58. func showAlert(alert: NSAlert, handler: ((NSModalResponse) -> Void)? = nil) {
  59. if let window = NSApplication.sharedApplication().keyWindow {
  60. alert.beginSheetModalForWindow(window) { (response: NSModalResponse) in
  61. handler?(response)
  62. }
  63. } else {
  64. let response = alert.runModal()
  65. handler?(response)
  66. }
  67. }
  68. func alertForProductRetrievalInfo(result: SwiftyStoreKit.RetrieveResultType) -> NSAlert {
  69. switch result {
  70. case .Success(let product):
  71. let priceString = NSNumberFormatter.localizedStringFromNumber(product.price ?? 0, numberStyle: .CurrencyStyle)
  72. return alertWithTitle(product.localizedTitle ?? "no title", message: "\(product.localizedDescription) - \(priceString)")
  73. case .Error(let error):
  74. return alertWithTitle("Could not retrieve product info", message: String(error))
  75. }
  76. }
  77. func alertForPurchaseResult(result: SwiftyStoreKit.PurchaseResultType) -> NSAlert {
  78. switch result {
  79. case .Success(let productId):
  80. print("Purchase Success: \(productId)")
  81. return alertWithTitle("Thank You", message: "Purchase completed")
  82. case .NoProductIdentifier:
  83. return alertWithTitle("Purchase failed", message: "Product not found")
  84. case .PaymentNotAllowed:
  85. return alertWithTitle("Payments not enabled", message: "You are not allowed to make payments")
  86. case .Error(let error):
  87. print("Purchase Failed: \(error)")
  88. if case ResponseError.RequestFailed(let internalError) = error where internalError.domain == SKErrorDomain {
  89. return alertWithTitle("Purchase failed", message: "Please check your Internet connection or try again later")
  90. }
  91. if (error as NSError).domain == SKErrorDomain {
  92. return alertWithTitle("Purchase failed", message: "Please check your Internet connection or try again later")
  93. }
  94. return alertWithTitle("Purchase failed", message: "Unknown error. Please contact support")
  95. }
  96. }
  97. func alertForRestorePurchases(result: SwiftyStoreKit.RestoreResultType) -> NSAlert {
  98. switch result {
  99. case .Success(let productId):
  100. print("Restore Success: \(productId)")
  101. return alertWithTitle("Purchases Restored", message: "All purchases have been restored")
  102. case .NothingToRestore:
  103. print("Nothing to Restore")
  104. return alertWithTitle("Nothing to restore", message: "No previous purchases were found")
  105. case .Error(let error):
  106. print("Restore Failed: \(error)")
  107. return alertWithTitle("Restore failed", message: "Unknown error. Please contact support")
  108. }
  109. }
  110. func alertForVerifyReceipt(result: SwiftyStoreKit.VerifyReceiptResultType) -> NSAlert {
  111. switch result {
  112. case .Success(let receipt):
  113. print("Verify receipt Success: \(receipt)")
  114. return self.alertWithTitle("Receipt verified", message: "Receipt verified remotly")
  115. case .Error(let error):
  116. print("Verify receipt Failed: \(error)")
  117. return self.alertWithTitle("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")
  118. }
  119. }
  120. }