IAPUnityInterface.mm 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. // Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
  2. //
  3. // You are hereby granted a non-exclusive, worldwide, royalty-free license to use,
  4. // copy, modify, and distribute this software in source code or binary form for use
  5. // in connection with the web services and APIs provided by Facebook.
  6. //
  7. // As with any software that integrates with the Facebook platform, your use of
  8. // this software is subject to the Facebook Developer Principles and Policies
  9. // [http://developers.facebook.com/policy/]. This copyright notice shall be
  10. // included in all copies or substantial portions of the software.
  11. //
  12. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  14. // FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  15. // COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  16. // IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  17. // CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  18. #import "IAPUnityInterface.h"
  19. #import "IAPSK1.h"
  20. typedef enum {
  21. SK1=1,
  22. SK2=2
  23. } SKIntegration;
  24. @interface IAPUnityInterface()
  25. @property (nonatomic, assign) SKIntegration skIntegration;
  26. @end
  27. @implementation IAPUnityInterface
  28. #pragma mark - Public APIs
  29. + (IAPUnityInterface *)sharedInstance
  30. {
  31. static dispatch_once_t pred;
  32. static IAPUnityInterface *shared = nil;
  33. dispatch_once(&pred, ^{
  34. shared = [[IAPUnityInterface alloc] init];
  35. });
  36. return shared;
  37. }
  38. - (void)initializeSK1
  39. {
  40. self.skIntegration = SK1;
  41. [[IAPSK1 sharedInstance] initialize];
  42. }
  43. - (void)initializeSK2
  44. {
  45. self.skIntegration = SK2;
  46. if (@available(iOS 15.0, *)) {
  47. [[IAPSK2 shared] initialize];
  48. }
  49. }
  50. - (void)purchaseConsumable
  51. {
  52. NSLog(@"Purchasing Consumable...");
  53. if (self.skIntegration == SK1) {
  54. [[IAPSK1 sharedInstance] purchaseConsumable];
  55. } else if (self.skIntegration == SK2) {
  56. if (@available(iOS 15.0, *)) {
  57. [[IAPSK2 shared] purchaseConsumable];
  58. }
  59. }
  60. }
  61. - (void)purchaseNonConsumable
  62. {
  63. NSLog(@"Purchasing Non-Consumable...");
  64. if (self.skIntegration == SK1) {
  65. [[IAPSK1 sharedInstance] purchaseNonConsumable];
  66. } else if (self.skIntegration == SK2) {
  67. if (@available(iOS 15.0, *)) {
  68. [[IAPSK2 shared] purchaseNonConsumable];
  69. }
  70. }
  71. }
  72. - (void)purchaseSubscription
  73. {
  74. NSLog(@"Purchasing Subscription...");
  75. if (self.skIntegration == SK1) {
  76. [[IAPSK1 sharedInstance] purchaseSubscription];
  77. } else if (self.skIntegration == SK2) {
  78. if (@available(iOS 15.0, *)) {
  79. [[IAPSK2 shared] purchaseSubscription];
  80. }
  81. }
  82. }
  83. @end
  84. #pragma mark - Unity C# interface (extern C)
  85. extern "C" {
  86. void IAPInitializeSK1()
  87. {
  88. [[IAPUnityInterface sharedInstance] initializeSK1];
  89. }
  90. void IAPInitializeSK2()
  91. {
  92. [[IAPUnityInterface sharedInstance] initializeSK2];
  93. }
  94. void IAPPurchaseConsumable()
  95. {
  96. [[IAPUnityInterface sharedInstance] purchaseConsumable];
  97. }
  98. void IAPPurchaseNonConsumable()
  99. {
  100. [[IAPUnityInterface sharedInstance] purchaseNonConsumable];
  101. }
  102. void IAPPurchaseSubscription()
  103. {
  104. [[IAPUnityInterface sharedInstance] purchaseSubscription];
  105. }
  106. }