Skip to content

Commit bb318f2

Browse files
Merge pull request #11 from apphud/develop
feat: implemented delegate methods
2 parents 62c160e + 0951b06 commit bb318f2

File tree

12 files changed

+338
-207
lines changed

12 files changed

+338
-207
lines changed

example/ios/Podfile.lock

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -237,8 +237,8 @@ PODS:
237237
- React-cxxreact (= 0.63.4)
238238
- React-jsi (= 0.63.4)
239239
- React-jsinspector (0.63.4)
240-
- react-native-apphud-sdk (1.0.4):
241-
- ApphudSDK
240+
- react-native-apphud-sdk (1.0.5):
241+
- ApphudSDK (= 1.2)
242242
- React-Core
243243
- react-native-safe-area-context (3.1.9):
244244
- React-Core
@@ -492,7 +492,7 @@ SPEC CHECKSUMS:
492492
React-jsi: a0418934cf48f25b485631deb27c64dc40fb4c31
493493
React-jsiexecutor: 93bd528844ad21dc07aab1c67cb10abae6df6949
494494
React-jsinspector: 58aef7155bc9a9683f5b60b35eccea8722a4f53a
495-
react-native-apphud-sdk: 4139f22ed5f50daadb40f9f102291249a809893a
495+
react-native-apphud-sdk: 89b0dcd5264ed11aea17d0ccec4c36af348322cc
496496
react-native-safe-area-context: b6e0e284002381d2ff29fa4fff42b4d8282e3c94
497497
React-RCTActionSheet: 89a0ca9f4a06c1f93c26067af074ccdce0f40336
498498
React-RCTAnimation: 1bde3ecc0c104c55df246eda516e0deb03c4e49b

ios/ApphudSdk.swift

Lines changed: 6 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,7 @@ class ApphudSdk: NSObject {
3939
let products:[SKProduct]? = Apphud.products();
4040
resolve(
4141
products?.map{ (product) -> NSDictionary in
42-
return [
43-
"id": product.productIdentifier,
44-
"price": product.price,
45-
"regionCode": product.priceLocale.regionCode as Any,
46-
"currencyCode": product.priceLocale.currencyCode as Any,
47-
];
42+
return DataTransformer.skProduct(product: product);
4843
}
4944
);
5045
}
@@ -61,26 +56,8 @@ class ApphudSdk: NSObject {
6156
Apphud.purchase(productIdentifier) { (result:ApphudPurchaseResult) in
6257
let transaction:SKPaymentTransaction? = result.transaction;
6358
var response = [
64-
"subscription": [
65-
"productId": result.subscription?.productId as Any,
66-
"expiresDate": result.subscription?.expiresDate.timeIntervalSince1970 as Any,
67-
"startedAt": result.subscription?.startedAt.timeIntervalSince1970 as Any,
68-
"canceledAt": result.subscription?.canceledAt?.timeIntervalSince1970 as Any,
69-
"isInRetryBilling": result.subscription?.isInRetryBilling as Any,
70-
"isAutorenewEnabled": result.subscription?.isAutorenewEnabled as Any,
71-
"isIntroductoryActivated": result.subscription?.isIntroductoryActivated as Any,
72-
"isActive": result.subscription?.isActive() as Any,
73-
"status": result.subscription?.status.rawValue as Any,
74-
"isLocal": result.subscription?.isLocal as Any,
75-
"isSandbox": result.subscription?.isSandbox as Any
76-
],
77-
"nonRenewingPurchase": [
78-
"productId": result.nonRenewingPurchase?.productId as Any,
79-
"purchasedAt": result.nonRenewingPurchase?.purchasedAt.timeIntervalSince1970 as Any,
80-
"canceledAt": result.nonRenewingPurchase?.canceledAt?.timeIntervalSince1970 as Any,
81-
"isLocal": result.nonRenewingPurchase?.isLocal as Any,
82-
"isSandbox": result.nonRenewingPurchase?.isSandbox as Any
83-
],
59+
"subscription": DataTransformer.apphudSubscription(subscription: result.subscription),
60+
"nonRenewingPurchase": DataTransformer.nonRenewingPurchase(nonRenewingPurchase: result.nonRenewingPurchase),
8461
"error": result.error.debugDescription
8562
] as [String : Any];
8663
if (transaction != nil) {
@@ -102,19 +79,7 @@ class ApphudSdk: NSObject {
10279
@objc(subscription:withRejecter:)
10380
func subscription(resolve: RCTPromiseResolveBlock, reject:RCTPromiseRejectBlock) -> Void {
10481
let subscription = Apphud.subscription();
105-
resolve([
106-
"productId": subscription?.productId as Any,
107-
"expiresDate": subscription?.expiresDate.timeIntervalSince1970 as Any,
108-
"startedAt": subscription?.startedAt.timeIntervalSince1970 as Any,
109-
"canceledAt": subscription?.canceledAt?.timeIntervalSince1970 as Any,
110-
"isInRetryBilling": subscription?.isInRetryBilling as Any,
111-
"isAutorenewEnabled": subscription?.isAutorenewEnabled as Any,
112-
"isIntroductoryActivated": subscription?.isIntroductoryActivated as Any,
113-
"isActive": subscription?.isActive() as Any,
114-
"status": subscription?.status.rawValue as Any,
115-
"isLocal": subscription?.isLocal as Any,
116-
"isSandbox": subscription?.isSandbox as Any
117-
]);
82+
resolve(DataTransformer.apphudSubscription(subscription: subscription));
11883
}
11984

12085
@objc(isNonRenewingPurchaseActive:withResolver:withRejecter:)
@@ -129,13 +94,7 @@ class ApphudSdk: NSObject {
12994
let purchases = Apphud.nonRenewingPurchases();
13095
resolve(
13196
purchases?.map({ (purchase) -> NSDictionary in
132-
return [
133-
"productId": purchase.productId,
134-
"canceledAt": purchase.canceledAt?.timeIntervalSince1970 as Any,
135-
"purchasedAt": purchase.purchasedAt.timeIntervalSince1970 as Any,
136-
"isLocal": purchase.isLocal as Any,
137-
"isSandbox": purchase.isSandbox as Any
138-
]
97+
return DataTransformer.nonRenewingPurchase(nonRenewingPurchase: purchase);
13998
})
14099
);
141100
}
@@ -145,19 +104,7 @@ class ApphudSdk: NSObject {
145104
Apphud.restorePurchases { (subscriptions, purchases, error) in
146105
resolve([
147106
"subscriptions": subscriptions?.map{ (subscription) -> NSDictionary in
148-
return [
149-
"productId": subscription.productId as Any,
150-
"expiresDate": subscription.expiresDate.timeIntervalSince1970 as Any,
151-
"startedAt": subscription.startedAt.timeIntervalSince1970 as Any,
152-
"canceledAt": subscription.canceledAt?.timeIntervalSince1970 as Any,
153-
"isInRetryBilling": subscription.isInRetryBilling as Any,
154-
"isAutorenewEnabled": subscription.isAutorenewEnabled as Any,
155-
"isIntroductoryActivated": subscription.isIntroductoryActivated as Any,
156-
"isActive": subscription.isActive() as Any,
157-
"status": subscription.status.rawValue as Any,
158-
"isLocal": subscription.isLocal as Any,
159-
"isSandbox": subscription.isSandbox as Any
160-
]
107+
return DataTransformer.apphudSubscription(subscription: subscription);
161108
} as Any,
162109
"purchases": purchases?.map{ (purchase) -> NSDictionary in
163110
return [

ios/ApphudSdk.xcodeproj/project.pbxproj

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@
77
objects = {
88

99
/* Begin PBXBuildFile section */
10-
11-
5E555C0D2413F4C50049A1A2 /* ApphudSdk.m in Sources */ = {isa = PBXBuildFile; fileRef = B3E7B5891CC2AC0600A0062D /* ApphudSdk.m */; };
12-
F4FF95D7245B92E800C19C63 /* ApphudSdk.swift in Sources */ = {isa = PBXBuildFile; fileRef = F4FF95D6245B92E800C19C63 /* ApphudSdk.swift */; };
13-
10+
A878EB9B2625BFBA00A20068 /* ApphudSdkEvents.swift in Sources */ = {isa = PBXBuildFile; fileRef = A878EB972625BFBA00A20068 /* ApphudSdkEvents.swift */; };
11+
A878EB9C2625BFBA00A20068 /* DataTransformer.swift in Sources */ = {isa = PBXBuildFile; fileRef = A878EB992625BFBA00A20068 /* DataTransformer.swift */; };
12+
A878EB9D2625BFBA00A20068 /* ApphudSdkEvents.m in Sources */ = {isa = PBXBuildFile; fileRef = A878EB9A2625BFBA00A20068 /* ApphudSdkEvents.m */; };
13+
F4FF95D7245B92E800C19C63 /* ApphudSdk.swift in Sources */ = {isa = PBXBuildFile; fileRef = F4FF95D6245B92E800C19C63 /* ApphudSdk.swift */; };
1414
/* End PBXBuildFile section */
1515

1616
/* Begin PBXCopyFilesBuildPhase section */
@@ -27,11 +27,13 @@
2727

2828
/* Begin PBXFileReference section */
2929
134814201AA4EA6300B7C361 /* libApphudSdk.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = libApphudSdk.a; sourceTree = BUILT_PRODUCTS_DIR; };
30-
30+
A878EB972625BFBA00A20068 /* ApphudSdkEvents.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ApphudSdkEvents.swift; sourceTree = "<group>"; };
31+
A878EB982625BFBA00A20068 /* ApphudSdkEvents-Bridging-Header.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "ApphudSdkEvents-Bridging-Header.h"; sourceTree = "<group>"; };
32+
A878EB992625BFBA00A20068 /* DataTransformer.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DataTransformer.swift; sourceTree = "<group>"; };
33+
A878EB9A2625BFBA00A20068 /* ApphudSdkEvents.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ApphudSdkEvents.m; sourceTree = "<group>"; };
3134
B3E7B5891CC2AC0600A0062D /* ApphudSdk.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ApphudSdk.m; sourceTree = "<group>"; };
3235
F4FF95D5245B92E700C19C63 /* ApphudSdk-Bridging-Header.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "ApphudSdk-Bridging-Header.h"; sourceTree = "<group>"; };
3336
F4FF95D6245B92E800C19C63 /* ApphudSdk.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ApphudSdk.swift; sourceTree = "<group>"; };
34-
3537
/* End PBXFileReference section */
3638

3739
/* Begin PBXFrameworksBuildPhase section */
@@ -56,11 +58,13 @@
5658
58B511D21A9E6C8500147676 = {
5759
isa = PBXGroup;
5860
children = (
59-
6061
F4FF95D6245B92E800C19C63 /* ApphudSdk.swift */,
6162
B3E7B5891CC2AC0600A0062D /* ApphudSdk.m */,
63+
A878EB982625BFBA00A20068 /* ApphudSdkEvents-Bridging-Header.h */,
64+
A878EB9A2625BFBA00A20068 /* ApphudSdkEvents.m */,
65+
A878EB972625BFBA00A20068 /* ApphudSdkEvents.swift */,
66+
A878EB992625BFBA00A20068 /* DataTransformer.swift */,
6267
F4FF95D5245B92E700C19C63 /* ApphudSdk-Bridging-Header.h */,
63-
6468
134814211AA4EA7D00B7C361 /* Products */,
6569
);
6670
sourceTree = "<group>";
@@ -122,10 +126,10 @@
122126
isa = PBXSourcesBuildPhase;
123127
buildActionMask = 2147483647;
124128
files = (
125-
126129
F4FF95D7245B92E800C19C63 /* ApphudSdk.swift in Sources */,
127-
B3E7B58A1CC2AC0600A0062D /* ApphudSdk.m in Sources */,
128-
130+
A878EB9D2625BFBA00A20068 /* ApphudSdkEvents.m in Sources */,
131+
A878EB9B2625BFBA00A20068 /* ApphudSdkEvents.swift in Sources */,
132+
A878EB9C2625BFBA00A20068 /* DataTransformer.swift in Sources */,
129133
);
130134
runOnlyForDeploymentPostprocessing = 0;
131135
};
@@ -238,11 +242,9 @@
238242
OTHER_LDFLAGS = "-ObjC";
239243
PRODUCT_NAME = ApphudSdk;
240244
SKIP_INSTALL = YES;
241-
242245
SWIFT_OBJC_BRIDGING_HEADER = "ApphudSdk-Bridging-Header.h";
243246
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
244247
SWIFT_VERSION = 5.0;
245-
246248
};
247249
name = Debug;
248250
};
@@ -259,10 +261,8 @@
259261
OTHER_LDFLAGS = "-ObjC";
260262
PRODUCT_NAME = ApphudSdk;
261263
SKIP_INSTALL = YES;
262-
263264
SWIFT_OBJC_BRIDGING_HEADER = "ApphudSdk-Bridging-Header.h";
264265
SWIFT_VERSION = 5.0;
265-
266266
};
267267
name = Release;
268268
};
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#import <React/RCTBridgeModule.h>
2+
#import <React/RCTEventEmitter.h>
3+
#import <React/RCTUtils.h>

ios/ApphudSdkEvents.m

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#import <React/RCTBridgeModule.h>
2+
#import <React/RCTEventEmitter.h>
3+
4+
@interface RCT_EXTERN_MODULE(ApphudSdkEvents, RCTEventEmitter)
5+
6+
RCT_EXTERN_METHOD(
7+
setApphudProductIdentifiers:(NSArray)ids
8+
withResolve:(RCTPromiseResolveBlock)resolve
9+
withReject:(RCTPromiseRejectBlock)reject
10+
)
11+
12+
@end

ios/ApphudSdkEvents.swift

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
import Foundation
2+
import React
3+
import ApphudSDK
4+
import StoreKit
5+
6+
@objc(ApphudSdkEvents)
7+
class ApphudSdkEvents: RCTEventEmitter {
8+
9+
var productIdentifiers:[String] = [];
10+
11+
override init() {
12+
super.init();
13+
Apphud.setDelegate(self);
14+
Apphud.setUIDelegate(self);
15+
}
16+
17+
@objc(setApphudProductIdentifiers:withResolve:withReject:)
18+
public func setApphudProductIdentifiers(ids: NSArray, resolve: RCTPromiseResolveBlock, reject: RCTPromiseRejectBlock) -> Void {
19+
self.productIdentifiers = ids as! [String];
20+
resolve(self.productIdentifiers);
21+
}
22+
23+
override func supportedEvents() -> [String]! {
24+
return [
25+
"apphudDidFetchStoreKitProducts",
26+
"apphudDidChangeUserID",
27+
"apphudSubscriptionsUpdated",
28+
"apphudNonRenewingPurchasesUpdated",
29+
"apphudProductIdentifiers",
30+
"apphudDidPurchase",
31+
"apphudWillPurchase",
32+
"apphudDidFailPurchase",
33+
"apphudDidSelectSurveyAnswer"
34+
]
35+
}
36+
}
37+
38+
extension ApphudSdkEvents: ApphudDelegate {
39+
40+
func apphudDidFetchStoreKitProducts(_ products: [SKProduct]) {
41+
let result:[NSDictionary] = products.map{ (product) -> NSDictionary in
42+
return DataTransformer.skProduct(product: product);
43+
}
44+
self.sendEvent(withName: "apphudDidFetchStoreKitProducts", body: result);
45+
}
46+
47+
func apphudDidChangeUserID(_ userID: String) {
48+
self.sendEvent(withName: "apphudDidChangeUserID", body: userID);
49+
}
50+
51+
func apphudSubscriptionsUpdated(_ subscriptions: [ApphudSubscription]) {
52+
let result:[NSDictionary] = subscriptions.map{ (subscription) -> NSDictionary in
53+
return DataTransformer.apphudSubscription(subscription: subscription);
54+
}
55+
self.sendEvent(withName: "apphudSubscriptionsUpdated", body: result);
56+
}
57+
58+
func apphudNonRenewingPurchasesUpdated(_ purchases: [ApphudNonRenewingPurchase]) {
59+
let result:[NSDictionary] = purchases.map{ (purchase) -> NSDictionary in
60+
return DataTransformer.nonRenewingPurchase(nonRenewingPurchase: purchase);
61+
}
62+
self.sendEvent(withName: "apphudNonRenewingPurchasesUpdated", body: result);
63+
}
64+
65+
func apphudProductIdentifiers() -> [String] {
66+
return self.productIdentifiers;
67+
}
68+
}
69+
70+
extension ApphudSdkEvents: ApphudUIDelegate {
71+
72+
func apphudDidPurchase(product: SKProduct, offerID: String?, screenName: String) {
73+
self.sendEvent(withName: "apphudDidPurchase", body: [
74+
"product": DataTransformer.skProduct(product: product),
75+
"offerId": offerID as Any,
76+
"screenName": screenName
77+
]);
78+
}
79+
80+
func apphudWillPurchase(product: SKProduct, offerID: String?, screenName: String) {
81+
self.sendEvent(withName: "apphudWillPurchase", body: [
82+
"product": DataTransformer.skProduct(product: product),
83+
"offerId": offerID as Any,
84+
"screenName": screenName
85+
]);
86+
}
87+
88+
func apphudDidFailPurchase(product: SKProduct, offerID: String?, errorCode: SKError.Code, screenName: String) {
89+
self.sendEvent(withName: "apphudWillPurchase", body: [
90+
"product": DataTransformer.skProduct(product: product),
91+
"offerId": offerID as Any,
92+
"screenName": screenName,
93+
"errorCode": errorCode.rawValue
94+
]);
95+
}
96+
97+
func apphudDidSelectSurveyAnswer(question: String, answer: String, screenName: String) {
98+
self.sendEvent(withName: "apphudDidSelectSurveyAnswer", body: [
99+
"question": question,
100+
"answer": answer,
101+
"screenName": screenName
102+
])
103+
}
104+
}

ios/DataTransformer.swift

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
//
2+
// SKProductDictionary.swift
3+
// ApphudSDK
4+
//
5+
// Created by Alexandr Makarov on 4/8/21.
6+
//
7+
8+
import Foundation
9+
import StoreKit
10+
import ApphudSDK
11+
12+
public class DataTransformer {
13+
public static func skProduct(product: SKProduct) -> NSDictionary {
14+
return [
15+
"id": product.productIdentifier,
16+
"price": product.price,
17+
"regionCode": product.priceLocale.regionCode as Any,
18+
"currencyCode": product.priceLocale.currencyCode as Any,
19+
];
20+
}
21+
22+
public static func apphudSubscription(subscription: ApphudSubscription?) -> NSDictionary {
23+
var result:NSDictionary = NSDictionary();
24+
if (subscription != nil) {
25+
result = [
26+
"productId": subscription!.productId as Any,
27+
"expiresDate": subscription!.expiresDate.timeIntervalSince1970 as Any,
28+
"startedAt": subscription!.startedAt.timeIntervalSince1970 as Any,
29+
"canceledAt": subscription?.canceledAt?.timeIntervalSince1970 as Any,
30+
"isInRetryBilling": subscription!.isInRetryBilling as Any,
31+
"isAutorenewEnabled": subscription!.isAutorenewEnabled as Any,
32+
"isIntroductoryActivated": subscription!.isIntroductoryActivated as Any,
33+
"isActive": subscription!.isActive() as Any,
34+
"status": subscription!.status.rawValue as Any,
35+
"isLocal": subscription!.isLocal as Any,
36+
"isSandbox": subscription!.isSandbox as Any
37+
]
38+
}
39+
return result;
40+
}
41+
42+
public static func nonRenewingPurchase(nonRenewingPurchase: ApphudNonRenewingPurchase?) -> NSDictionary {
43+
var result:NSDictionary = NSDictionary();
44+
if (nonRenewingPurchase != nil) {
45+
result = [
46+
"productId": nonRenewingPurchase!.productId as Any,
47+
"purchasedAt": nonRenewingPurchase!.purchasedAt.timeIntervalSince1970 as Any,
48+
"canceledAt": nonRenewingPurchase?.canceledAt?.timeIntervalSince1970 as Any,
49+
"isLocal": nonRenewingPurchase!.isLocal as Any,
50+
"isSandbox": nonRenewingPurchase!.isSandbox as Any
51+
]
52+
}
53+
return result;
54+
}
55+
}
56+

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@apphud/react-native-apphud-sdk",
3-
"version": "1.0.5",
3+
"version": "1.0.6",
44
"description": "Apphud SDK for react native",
55
"main": "lib/commonjs/index",
66
"module": "lib/module/index",

react-native-apphud-sdk.podspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,5 @@ Pod::Spec.new do |s|
1818

1919

2020
s.dependency "React-Core"
21-
s.dependency "ApphudSDK"
21+
s.dependency "ApphudSDK", "1.2"
2222
end

0 commit comments

Comments
 (0)