ViewController.swift 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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 StoreKit
  10. import SwiftyStoreKit
  11. class ViewController: UIViewController {
  12. let AppBundleId = "com.musevisions.iOS.SwiftyStoreKit"
  13. // MARK: actions
  14. @IBAction func getInfo1() {
  15. getInfo("1")
  16. }
  17. @IBAction func getInfo2() {
  18. getInfo("2")
  19. }
  20. @IBAction func purchase1() {
  21. purchase("1")
  22. }
  23. @IBAction func purchase2() {
  24. purchase("2")
  25. }
  26. func getInfo(no: String) {
  27. NetworkActivityIndicatorManager.networkOperationStarted()
  28. SwiftyStoreKit.retrieveProductInfo(AppBundleId + ".purchase" + no) { result in
  29. NetworkActivityIndicatorManager.networkOperationFinished()
  30. self.showAlert(self.alertForProductRetrievalInfo(result))
  31. }
  32. }
  33. func purchase(no: String) {
  34. NetworkActivityIndicatorManager.networkOperationStarted()
  35. SwiftyStoreKit.purchaseProduct(AppBundleId + ".purchase" + no) { result in
  36. NetworkActivityIndicatorManager.networkOperationFinished()
  37. self.showAlert(self.alertForPurchaseResult(result))
  38. }
  39. }
  40. @IBAction func restorePurchases() {
  41. NetworkActivityIndicatorManager.networkOperationStarted()
  42. SwiftyStoreKit.restorePurchases() { result in
  43. NetworkActivityIndicatorManager.networkOperationFinished()
  44. self.showAlert(self.alertForRestorePurchases(result))
  45. }
  46. }
  47. @IBAction func verifyReceipt() {
  48. NetworkActivityIndicatorManager.networkOperationStarted()
  49. SwiftyStoreKit.verifyReceipt() { result in
  50. NetworkActivityIndicatorManager.networkOperationFinished()
  51. self.showAlert(self.alertForVerifyReceipt(result))
  52. switch result {
  53. case .Error(let error):
  54. switch (error) {
  55. case .NoReceiptData :
  56. self.refreshReceipt()
  57. default: break
  58. }
  59. default: break
  60. }
  61. }
  62. }
  63. func refreshReceipt() {
  64. SwiftyStoreKit.receiptRefresh { (result) -> () in
  65. self.showAlert(self.alertForRefreshReceipt(result))
  66. }
  67. }
  68. override func preferredStatusBarStyle() -> UIStatusBarStyle {
  69. return .LightContent
  70. }
  71. }
  72. // MARK: User facing alerts
  73. extension ViewController {
  74. func alertWithTitle(title: String, message: String) -> UIAlertController {
  75. let alert = UIAlertController(title: title, message: message, preferredStyle: .Alert)
  76. alert.addAction(UIAlertAction(title: "OK", style: .Cancel, handler: nil))
  77. return alert
  78. }
  79. func showAlert(alert: UIAlertController) {
  80. guard let _ = self.presentedViewController else {
  81. self.presentViewController(alert, animated: true, completion: nil)
  82. return
  83. }
  84. }
  85. func alertForProductRetrievalInfo(result: SwiftyStoreKit.RetrieveResultType) -> UIAlertController {
  86. switch result {
  87. case .Success(let product):
  88. let priceString = NSNumberFormatter.localizedStringFromNumber(product.price, numberStyle: .CurrencyStyle)
  89. return alertWithTitle(product.localizedTitle, message: "\(product.localizedDescription) - \(priceString)")
  90. case .Error(let error):
  91. return alertWithTitle("Could not retrieve product info", message: String(error))
  92. }
  93. }
  94. func alertForPurchaseResult(result: SwiftyStoreKit.PurchaseResultType) -> UIAlertController {
  95. switch result {
  96. case .Success(let productId):
  97. print("Purchase Success: \(productId)")
  98. return alertWithTitle("Thank You", message: "Purchase completed")
  99. case .NoProductIdentifier:
  100. return alertWithTitle("Purchase failed", message: "Product not found")
  101. case .PaymentNotAllowed:
  102. return alertWithTitle("Payments not enabled", message: "You are not allowed to make payments")
  103. case .Error(let error):
  104. print("Purchase Failed: \(error)")
  105. if case ResponseError.RequestFailed(let internalError) = error where internalError.domain == SKErrorDomain {
  106. return alertWithTitle("Purchase failed", message: "Please check your Internet connection or try again later")
  107. }
  108. if (error as NSError).domain == SKErrorDomain {
  109. return alertWithTitle("Purchase failed", message: "Please check your Internet connection or try again later")
  110. }
  111. return alertWithTitle("Purchase failed", message: "Unknown error. Please contact support")
  112. }
  113. }
  114. func alertForRestorePurchases(result: SwiftyStoreKit.RestoreResultType) -> UIAlertController {
  115. switch result {
  116. case .Success(let productId):
  117. print("Restore Success: \(productId)")
  118. return alertWithTitle("Purchases Restored", message: "All purchases have been restored")
  119. case .NothingToRestore:
  120. print("Nothing to Restore")
  121. return alertWithTitle("Nothing to restore", message: "No previous purchases were found")
  122. case .Error(let error):
  123. print("Restore Failed: \(error)")
  124. return alertWithTitle("Restore failed", message: "Unknown error. Please contact support")
  125. }
  126. }
  127. func alertForVerifyReceipt(result: SwiftyStoreKit.VerifyReceiptResultType) -> UIAlertController{
  128. switch result {
  129. case .Success(let receipt):
  130. print("Verify receipt Success: \(receipt)")
  131. return alertWithTitle("Receipt verified", message: "Receipt verified remotly")
  132. case .Error(let error):
  133. print("Verify receipt Failed: \(error)")
  134. switch (error) {
  135. case .NoReceiptData :
  136. return alertWithTitle("Receipt verification", message: "No receipt data, application will try to get a new one. Try again.")
  137. default:
  138. return alertWithTitle("Receipt verification", message: "Receipt verification failed")
  139. }
  140. }
  141. }
  142. func alertForRefreshReceipt(result: SwiftyStoreKit.RefreshReceiptResultType) -> UIAlertController {
  143. switch result {
  144. case .Success:
  145. print("Receipt refreshed Success")
  146. return self.alertWithTitle("Receipt refreshed", message: "Receipt refreshed with success")
  147. case .Error(let error):
  148. print("Receipt refreshed Failed: \(error)")
  149. return self.alertWithTitle("Receipt refreshed Failed", message: "Receipt refreshed Failed")
  150. }
  151. }
  152. }