ViewController.swift 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. }
  42. // MARK: User facing alerts
  43. extension ViewController {
  44. func alertWithTitle(title: String, message: String) -> NSAlert {
  45. let alert: NSAlert = NSAlert()
  46. alert.messageText = title
  47. alert.informativeText = message
  48. alert.alertStyle = NSAlertStyle.InformationalAlertStyle
  49. return alert
  50. }
  51. func showAlert(alert: NSAlert) {
  52. if let window = NSApplication.sharedApplication().keyWindow {
  53. alert.beginSheetModalForWindow(window) { (response: NSModalResponse) in
  54. }
  55. } else {
  56. alert.runModal()
  57. }
  58. }
  59. func alertForProductRetrievalInfo(result: SwiftyStoreKit.RetrieveResultType) -> NSAlert {
  60. switch result {
  61. case .Success(let product):
  62. let priceString = NSNumberFormatter.localizedStringFromNumber(product.price ?? 0, numberStyle: .CurrencyStyle)
  63. return alertWithTitle(product.localizedTitle ?? "no title", message: "\(product.localizedDescription) - \(priceString)")
  64. case .Error(let error):
  65. return alertWithTitle("Could not retrieve product info", message: String(error))
  66. }
  67. }
  68. func alertForPurchaseResult(result: SwiftyStoreKit.PurchaseResultType) -> NSAlert {
  69. switch result {
  70. case .Success(let productId):
  71. print("Purchase Success: \(productId)")
  72. return alertWithTitle("Thank You", message: "Purchase completed")
  73. case .NoProductIdentifier:
  74. return alertWithTitle("Purchase failed", message: "Product not found")
  75. case .PaymentNotAllowed:
  76. return alertWithTitle("Payments not enabled", message: "You are not allowed to make payments")
  77. case .Error(let error):
  78. print("Purchase Failed: \(error)")
  79. if case ResponseError.RequestFailed(let internalError) = error where internalError.domain == SKErrorDomain {
  80. return alertWithTitle("Purchase failed", message: "Please check your Internet connection or try again later")
  81. }
  82. if (error as NSError).domain == SKErrorDomain {
  83. return alertWithTitle("Purchase failed", message: "Please check your Internet connection or try again later")
  84. }
  85. return alertWithTitle("Purchase failed", message: "Unknown error. Please contact support")
  86. }
  87. }
  88. func alertForRestorePurchases(result: SwiftyStoreKit.RestoreResultType) -> NSAlert {
  89. switch result {
  90. case .Success(let productId):
  91. print("Restore Success: \(productId)")
  92. return alertWithTitle("Purchases Restored", message: "All purchases have been restored")
  93. case .NothingToRestore:
  94. print("Nothing to Restore")
  95. return alertWithTitle("Nothing to restore", message: "No previous purchases were found")
  96. case .Error(let error):
  97. print("Restore Failed: \(error)")
  98. return alertWithTitle("Restore failed", message: "Unknown error. Please contact support")
  99. }
  100. }
  101. }