@altcraft/react-native-sdk 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +20 -0
- package/README.md +46 -0
- package/Sdk.podspec +48 -0
- package/android/build.gradle +78 -0
- package/android/gradle.properties +5 -0
- package/android/src/main/AndroidManifest.xml +2 -0
- package/android/src/main/java/com/altcraft/sdk/SdkModule.kt +1242 -0
- package/android/src/main/java/com/altcraft/sdk/SdkPackage.kt +33 -0
- package/android/src/main/java/com/altcraft/sdk/interfaces/RnFCMProvider.kt +27 -0
- package/android/src/main/java/com/altcraft/sdk/interfaces/RnHMSProvider.kt +32 -0
- package/android/src/main/java/com/altcraft/sdk/interfaces/RnJWTProvider.kt +21 -0
- package/android/src/main/java/com/altcraft/sdk/interfaces/RnRustoreProvider.kt +53 -0
- package/ios/Sdk.h +19 -0
- package/ios/Sdk.mm +1112 -0
- package/ios/SdkModule.swift +888 -0
- package/ios/protocols/RNAPNSProvider.swift +15 -0
- package/ios/protocols/RNFCMProvider.swift +24 -0
- package/ios/protocols/RNHMSProvider.swift +24 -0
- package/ios/protocols/RNJWTProvider.swift +15 -0
- package/lib/module/NativeSdk.js +13 -0
- package/lib/module/NativeSdk.js.map +1 -0
- package/lib/module/index.js +561 -0
- package/lib/module/index.js.map +1 -0
- package/lib/module/package.json +1 -0
- package/lib/typescript/package.json +1 -0
- package/lib/typescript/src/NativeSdk.d.ts +114 -0
- package/lib/typescript/src/NativeSdk.d.ts.map +1 -0
- package/lib/typescript/src/index.d.ts +391 -0
- package/lib/typescript/src/index.d.ts.map +1 -0
- package/package.json +128 -0
- package/react-native.config.js +10 -0
- package/src/NativeSdk.ts +171 -0
- package/src/index.tsx +670 -0
package/ios/Sdk.mm
ADDED
|
@@ -0,0 +1,1112 @@
|
|
|
1
|
+
// ios/Sdk.mm
|
|
2
|
+
#import "Sdk.h"
|
|
3
|
+
|
|
4
|
+
#ifdef RCT_NEW_ARCH_ENABLED
|
|
5
|
+
#import <ReactCommon/RCTTurboModule.h>
|
|
6
|
+
#endif
|
|
7
|
+
|
|
8
|
+
#import <Foundation/Foundation.h>
|
|
9
|
+
#import <objc/message.h>
|
|
10
|
+
#import <objc/runtime.h>
|
|
11
|
+
|
|
12
|
+
#include <optional>
|
|
13
|
+
#include <type_traits>
|
|
14
|
+
|
|
15
|
+
#if __has_include(<React/RCTBridgeModule.h>)
|
|
16
|
+
#import <React/RCTBridgeModule.h>
|
|
17
|
+
#endif
|
|
18
|
+
|
|
19
|
+
#if __has_include(<React/RCTUtils.h>)
|
|
20
|
+
#import <React/RCTUtils.h>
|
|
21
|
+
#endif
|
|
22
|
+
|
|
23
|
+
#if __has_include(<React/RCTCallableJSModules.h>)
|
|
24
|
+
#import <React/RCTCallableJSModules.h>
|
|
25
|
+
#else
|
|
26
|
+
/// Minimal protocol for calling JS from native when headers are not available.
|
|
27
|
+
@protocol RCTCallableJSModules <NSObject>
|
|
28
|
+
- (void)invokeModule:(NSString *)moduleName
|
|
29
|
+
method:(NSString *)methodName
|
|
30
|
+
withArgs:(NSArray *)args;
|
|
31
|
+
@end
|
|
32
|
+
#endif
|
|
33
|
+
|
|
34
|
+
#ifndef RCTPromiseResolveBlock
|
|
35
|
+
/// React Native promise resolver.
|
|
36
|
+
typedef void (^RCTPromiseResolveBlock)(id _Nullable result);
|
|
37
|
+
#endif
|
|
38
|
+
|
|
39
|
+
#ifndef RCTPromiseRejectBlock
|
|
40
|
+
/// React Native promise rejecter.
|
|
41
|
+
typedef void (^RCTPromiseRejectBlock)(NSString * _Nonnull code,
|
|
42
|
+
NSString * _Nullable message,
|
|
43
|
+
NSError * _Nullable error);
|
|
44
|
+
#endif
|
|
45
|
+
|
|
46
|
+
#pragma mark - Runtime helpers
|
|
47
|
+
|
|
48
|
+
/// Builds selector from string.
|
|
49
|
+
static SEL SdkSel(NSString *name) {
|
|
50
|
+
return NSSelectorFromString(name);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/// Returns YES if instance responds to selector.
|
|
54
|
+
static BOOL SdkResponds(id obj, SEL sel) {
|
|
55
|
+
return obj && sel && [obj respondsToSelector:sel];
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/// Returns YES if class responds to selector.
|
|
59
|
+
static BOOL SdkClassResponds(Class cls, SEL sel) {
|
|
60
|
+
return cls && sel && [cls respondsToSelector:sel];
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
#pragma mark - NSNull -> nil
|
|
64
|
+
|
|
65
|
+
/// Converts NSNull / kCFNull to nil (keeps other values as-is).
|
|
66
|
+
static inline id SdkNilIfNSNull(id v) {
|
|
67
|
+
return (v == (id)kCFNull || [v isKindOfClass:[NSNull class]]) ? nil : v;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
#pragma mark - Swift runtime bridge (no Sdk-Swift.h needed)
|
|
71
|
+
|
|
72
|
+
/// Returns SdkModule class if present (resolved by name at runtime).
|
|
73
|
+
static Class SdkAppModuleClass(void) {
|
|
74
|
+
Class cls = NSClassFromString(@"SdkModule");
|
|
75
|
+
|
|
76
|
+
#ifdef DEBUG
|
|
77
|
+
// Debug-only: attempt to discover module name with namespace suffix.
|
|
78
|
+
if (!cls) {
|
|
79
|
+
int count = objc_getClassList(NULL, 0);
|
|
80
|
+
if (count > 0) {
|
|
81
|
+
Class *classes = (Class *)malloc(sizeof(Class) * (NSUInteger)count);
|
|
82
|
+
if (classes) {
|
|
83
|
+
objc_getClassList(classes, count);
|
|
84
|
+
for (int i = 0; i < count; i++) {
|
|
85
|
+
NSString *name = NSStringFromClass(classes[i]);
|
|
86
|
+
if ([name hasSuffix:@".SdkModule"] || [name isEqualToString:@"SdkModule"]) {
|
|
87
|
+
if ([name hasSuffix:@".SdkModule"]) {
|
|
88
|
+
cls = classes[i];
|
|
89
|
+
break;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
free(classes);
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
#endif // DEBUG
|
|
98
|
+
|
|
99
|
+
return cls;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/// Returns shared Swift module instance via +[SdkModule shared].
|
|
103
|
+
static id SdkAppModuleSharedInstance(void) {
|
|
104
|
+
Class cls = SdkAppModuleClass();
|
|
105
|
+
if (!cls) return nil;
|
|
106
|
+
|
|
107
|
+
SEL sel = SdkSel(@"shared");
|
|
108
|
+
if (!SdkClassResponds(cls, sel)) return nil;
|
|
109
|
+
|
|
110
|
+
IMP imp = [cls methodForSelector:sel];
|
|
111
|
+
if (!imp) return nil;
|
|
112
|
+
|
|
113
|
+
id (*func)(id, SEL) = (id (*)(id, SEL))imp;
|
|
114
|
+
return func((id)cls, sel);
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/// Ensures push providers are installed/configured inside Swift module.
|
|
118
|
+
static void EnsureProvidersInstalled(id instance) {
|
|
119
|
+
if (!instance) return;
|
|
120
|
+
|
|
121
|
+
SEL sel = SdkSel(@"ensureProvidersInstalled");
|
|
122
|
+
if (!SdkResponds(instance, sel)) return;
|
|
123
|
+
|
|
124
|
+
IMP imp = [instance methodForSelector:sel];
|
|
125
|
+
if (!imp) return;
|
|
126
|
+
|
|
127
|
+
void (*func)(id, SEL) = (void (*)(id, SEL))imp;
|
|
128
|
+
func(instance, sel);
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/// Sets token on Swift module using a selector name.
|
|
132
|
+
static void CallSetToken(id instance, NSString *selectorName, NSString * _Nullable token) {
|
|
133
|
+
if (!instance) return;
|
|
134
|
+
|
|
135
|
+
SEL sel = SdkSel(selectorName);
|
|
136
|
+
if (!SdkResponds(instance, sel)) return;
|
|
137
|
+
|
|
138
|
+
IMP imp = [instance methodForSelector:sel];
|
|
139
|
+
if (!imp) return;
|
|
140
|
+
|
|
141
|
+
void (*func)(id, SEL, id) = (void (*)(id, SEL, id))imp;
|
|
142
|
+
func(instance, sel, token);
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/// Sets App Group name on Swift module.
|
|
146
|
+
static void CallSetAppGroup(id instance, NSString * _Nullable groupName) {
|
|
147
|
+
if (!instance) return;
|
|
148
|
+
|
|
149
|
+
SEL sel = SdkSel(@"setAppGroupWithName:");
|
|
150
|
+
if (!SdkResponds(instance, sel)) return;
|
|
151
|
+
|
|
152
|
+
IMP imp = [instance methodForSelector:sel];
|
|
153
|
+
if (!imp) return;
|
|
154
|
+
|
|
155
|
+
void (*func)(id, SEL, id) = (void (*)(id, SEL, id))imp;
|
|
156
|
+
func(instance, sel, groupName);
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/// Deletes device token for a provider on Swift module.
|
|
160
|
+
static void CallDeleteDeviceToken(id instance, NSString * _Nullable provider, void (^completion)(BOOL ok)) {
|
|
161
|
+
if (!instance) { completion(NO); return; }
|
|
162
|
+
|
|
163
|
+
SEL sel = SdkSel(@"deleteDeviceTokenWithProvider:completion:");
|
|
164
|
+
if (!SdkResponds(instance, sel)) { completion(NO); return; }
|
|
165
|
+
|
|
166
|
+
IMP imp = [instance methodForSelector:sel];
|
|
167
|
+
if (!imp) { completion(NO); return; }
|
|
168
|
+
|
|
169
|
+
void (*func)(id, SEL, id, id) = (void (*)(id, SEL, id, id))imp;
|
|
170
|
+
func(instance, sel, provider, completion);
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/// Retrieves current push token from Swift module.
|
|
174
|
+
static void CallGetPushToken(id instance, void (^completion)(id _Nullable tokenObjC)) {
|
|
175
|
+
if (!instance) { completion(nil); return; }
|
|
176
|
+
|
|
177
|
+
SEL sel = SdkSel(@"getPushTokenWithCompletion:");
|
|
178
|
+
if (!SdkResponds(instance, sel)) { completion(nil); return; }
|
|
179
|
+
|
|
180
|
+
IMP imp = [instance methodForSelector:sel];
|
|
181
|
+
if (!imp) { completion(nil); return; }
|
|
182
|
+
|
|
183
|
+
void (*func)(id, SEL, id) = (void (*)(id, SEL, id))imp;
|
|
184
|
+
func(instance, sel, completion);
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
/// Forces token update on Swift module.
|
|
188
|
+
static void CallForcedTokenUpdate(id instance, void (^completion)(void)) {
|
|
189
|
+
if (!instance) { completion(); return; }
|
|
190
|
+
|
|
191
|
+
SEL sel = SdkSel(@"forcedTokenUpdateWithCompletion:");
|
|
192
|
+
if (!SdkResponds(instance, sel)) { completion(); return; }
|
|
193
|
+
|
|
194
|
+
IMP imp = [instance methodForSelector:sel];
|
|
195
|
+
if (!imp) { completion(); return; }
|
|
196
|
+
|
|
197
|
+
void (*func)(id, SEL, id) = (void (*)(id, SEL, id))imp;
|
|
198
|
+
func(instance, sel, completion);
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
/// Changes push provider priority list on Swift module.
|
|
202
|
+
static void CallChangeProviderPriorityList(id instance, NSArray * _Nullable list, void (^completion)(BOOL ok)) {
|
|
203
|
+
if (!instance) { completion(NO); return; }
|
|
204
|
+
|
|
205
|
+
SEL sel = SdkSel(@"changePushProviderPriorityListWithList:completion:");
|
|
206
|
+
if (!SdkResponds(instance, sel)) { completion(NO); return; }
|
|
207
|
+
|
|
208
|
+
IMP imp = [instance methodForSelector:sel];
|
|
209
|
+
if (!imp) { completion(NO); return; }
|
|
210
|
+
|
|
211
|
+
void (*func)(id, SEL, id, id) = (void (*)(id, SEL, id, id))imp;
|
|
212
|
+
func(instance, sel, list, completion);
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
/// Sets push token with provider on Swift module.
|
|
216
|
+
static void CallSetPushToken(id instance, NSString *provider, NSString * _Nullable token) {
|
|
217
|
+
if (!instance) return;
|
|
218
|
+
|
|
219
|
+
SEL sel = SdkSel(@"setPushTokenWithProvider:pushToken:");
|
|
220
|
+
if (!SdkResponds(instance, sel)) return;
|
|
221
|
+
|
|
222
|
+
IMP imp = [instance methodForSelector:sel];
|
|
223
|
+
if (!imp) return;
|
|
224
|
+
|
|
225
|
+
void (*func)(id, SEL, id, id) = (void (*)(id, SEL, id, id))imp;
|
|
226
|
+
func(instance, sel, provider, token);
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
#pragma mark - Push subscription calls
|
|
230
|
+
|
|
231
|
+
/// Invokes subscription-related methods on Swift module.
|
|
232
|
+
static void CallPushSubscription(id instance,
|
|
233
|
+
NSString *selectorName,
|
|
234
|
+
NSNumber *sync,
|
|
235
|
+
NSDictionary *profileFields,
|
|
236
|
+
NSDictionary *customFields,
|
|
237
|
+
NSArray *cats,
|
|
238
|
+
NSNumber *replace,
|
|
239
|
+
NSNumber *skipTriggers) {
|
|
240
|
+
if (!instance) return;
|
|
241
|
+
|
|
242
|
+
SEL sel = SdkSel(selectorName);
|
|
243
|
+
if (!SdkResponds(instance, sel)) return;
|
|
244
|
+
|
|
245
|
+
IMP imp = [instance methodForSelector:sel];
|
|
246
|
+
if (!imp) return;
|
|
247
|
+
|
|
248
|
+
void (*func)(id, SEL, id, id, id, id, id, id) =
|
|
249
|
+
(void (*)(id, SEL, id, id, id, id, id, id))imp;
|
|
250
|
+
|
|
251
|
+
func(instance, sel, sync, profileFields, customFields, cats, replace, skipTriggers);
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
#pragma mark - Mobile Event calls
|
|
255
|
+
|
|
256
|
+
/// Sends mobile event to Swift module.
|
|
257
|
+
static void CallMobileEvent(id instance,
|
|
258
|
+
NSString *sid,
|
|
259
|
+
NSString *eventName,
|
|
260
|
+
NSString * _Nullable sendMessageId,
|
|
261
|
+
NSDictionary * _Nullable payload,
|
|
262
|
+
NSDictionary * _Nullable matching,
|
|
263
|
+
NSString * _Nullable matchingType,
|
|
264
|
+
NSDictionary * _Nullable profileFields) {
|
|
265
|
+
if (!instance) return;
|
|
266
|
+
|
|
267
|
+
SEL sel = SdkSel(@"mobileEvent:eventName:sendMessageId:payload:matching:matchingType:profileFields:");
|
|
268
|
+
if (!SdkResponds(instance, sel)) return;
|
|
269
|
+
|
|
270
|
+
IMP imp = [instance methodForSelector:sel];
|
|
271
|
+
if (!imp) return;
|
|
272
|
+
|
|
273
|
+
void (*func)(id, SEL, id, id, id, id, id, id, id) =
|
|
274
|
+
(void (*)(id, SEL, id, id, id, id, id, id, id))imp;
|
|
275
|
+
|
|
276
|
+
func(instance, sel, sid, eventName, sendMessageId, payload, matching, matchingType, profileFields);
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
#pragma mark - Promise helpers
|
|
280
|
+
|
|
281
|
+
/// Calls a Swift method with signature (resolver, rejecter).
|
|
282
|
+
static void CallPromise0Args(id instance,
|
|
283
|
+
NSString *selectorName,
|
|
284
|
+
RCTPromiseResolveBlock resolve,
|
|
285
|
+
RCTPromiseRejectBlock reject) {
|
|
286
|
+
if (!instance) {
|
|
287
|
+
NSError *err = [NSError errorWithDomain:@"Sdk" code:1 userInfo:@{NSLocalizedDescriptionKey:@"SdkAppModule is nil"}];
|
|
288
|
+
reject(@"SWIFT_MODULE_NOT_FOUND", @"SdkAppModule instance is nil", err);
|
|
289
|
+
return;
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
SEL sel = SdkSel(selectorName);
|
|
293
|
+
if (!SdkResponds(instance, sel)) {
|
|
294
|
+
NSError *err = [NSError errorWithDomain:@"Sdk" code:22 userInfo:@{NSLocalizedDescriptionKey:@"Swift selector not found"}];
|
|
295
|
+
reject(@"METHOD_NOT_FOUND", @"method not found", err);
|
|
296
|
+
return;
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
// React Native expects promise calls on main thread.
|
|
300
|
+
if (![NSThread isMainThread]) {
|
|
301
|
+
dispatch_async(dispatch_get_main_queue(), ^{
|
|
302
|
+
CallPromise0Args(instance, selectorName, resolve, reject);
|
|
303
|
+
});
|
|
304
|
+
return;
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
IMP imp = [instance methodForSelector:sel];
|
|
308
|
+
if (!imp) {
|
|
309
|
+
// Fallback for cases where methodForSelector returns NULL in mixed ObjC/Swift builds.
|
|
310
|
+
NSMethodSignature *sig = [instance methodSignatureForSelector:sel];
|
|
311
|
+
if (!sig) {
|
|
312
|
+
NSError *err = [NSError errorWithDomain:@"Sdk" code:20 userInfo:@{NSLocalizedDescriptionKey:@"No signature"}];
|
|
313
|
+
reject(@"METHOD_SIGNATURE_NOT_FOUND", @"method signature not found", err);
|
|
314
|
+
return;
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
NSInvocation *inv = [NSInvocation invocationWithMethodSignature:sig];
|
|
318
|
+
[inv setTarget:instance];
|
|
319
|
+
[inv setSelector:sel];
|
|
320
|
+
RCTPromiseResolveBlock res = resolve;
|
|
321
|
+
RCTPromiseRejectBlock rej = reject;
|
|
322
|
+
[inv setArgument:&res atIndex:2];
|
|
323
|
+
[inv setArgument:&rej atIndex:3];
|
|
324
|
+
[inv invoke];
|
|
325
|
+
return;
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
void (*func)(id, SEL, RCTPromiseResolveBlock, RCTPromiseRejectBlock) =
|
|
329
|
+
(void (*)(id, SEL, RCTPromiseResolveBlock, RCTPromiseRejectBlock))imp;
|
|
330
|
+
|
|
331
|
+
func(instance, sel, resolve, reject);
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
/// Calls a Swift method with signature (String?, resolver, rejecter).
|
|
335
|
+
static void CallPromise1StringArg(id instance,
|
|
336
|
+
NSString *selectorName,
|
|
337
|
+
NSString * _Nullable arg,
|
|
338
|
+
RCTPromiseResolveBlock resolve,
|
|
339
|
+
RCTPromiseRejectBlock reject) {
|
|
340
|
+
if (!instance) {
|
|
341
|
+
NSError *err = [NSError errorWithDomain:@"Sdk" code:1 userInfo:@{NSLocalizedDescriptionKey:@"SdkAppModule is nil"}];
|
|
342
|
+
reject(@"SWIFT_MODULE_NOT_FOUND", @"SdkAppModule instance is nil", err);
|
|
343
|
+
return;
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
SEL sel = SdkSel(selectorName);
|
|
347
|
+
if (!SdkResponds(instance, sel)) {
|
|
348
|
+
NSError *err = [NSError errorWithDomain:@"Sdk" code:23 userInfo:@{NSLocalizedDescriptionKey:@"Swift selector not found"}];
|
|
349
|
+
reject(@"METHOD_NOT_FOUND", @"method not found", err);
|
|
350
|
+
return;
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
// React Native expects promise calls on main thread.
|
|
354
|
+
if (![NSThread isMainThread]) {
|
|
355
|
+
NSString *a = arg;
|
|
356
|
+
dispatch_async(dispatch_get_main_queue(), ^{
|
|
357
|
+
CallPromise1StringArg(instance, selectorName, a, resolve, reject);
|
|
358
|
+
});
|
|
359
|
+
return;
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
IMP imp = [instance methodForSelector:sel];
|
|
363
|
+
if (!imp) {
|
|
364
|
+
NSMethodSignature *sig = [instance methodSignatureForSelector:sel];
|
|
365
|
+
if (!sig) {
|
|
366
|
+
NSError *err = [NSError errorWithDomain:@"Sdk" code:21 userInfo:@{NSLocalizedDescriptionKey:@"No signature"}];
|
|
367
|
+
reject(@"METHOD_SIGNATURE_NOT_FOUND", @"method signature not found", err);
|
|
368
|
+
return;
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
NSInvocation *inv = [NSInvocation invocationWithMethodSignature:sig];
|
|
372
|
+
[inv setTarget:instance];
|
|
373
|
+
[inv setSelector:sel];
|
|
374
|
+
NSString *a = arg;
|
|
375
|
+
RCTPromiseResolveBlock res = resolve;
|
|
376
|
+
RCTPromiseRejectBlock rej = reject;
|
|
377
|
+
[inv setArgument:&a atIndex:2];
|
|
378
|
+
[inv setArgument:&res atIndex:3];
|
|
379
|
+
[inv setArgument:&rej atIndex:4];
|
|
380
|
+
[inv invoke];
|
|
381
|
+
return;
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
void (*func)(id, SEL, NSString *, RCTPromiseResolveBlock, RCTPromiseRejectBlock) =
|
|
385
|
+
(void (*)(id, SEL, NSString *, RCTPromiseResolveBlock, RCTPromiseRejectBlock))imp;
|
|
386
|
+
|
|
387
|
+
func(instance, sel, arg, resolve, reject);
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
#pragma mark - Initialize / Clear
|
|
391
|
+
|
|
392
|
+
/// Calls Swift initializer selector on the main thread.
|
|
393
|
+
static void CallInitializeBySelector(id instance,
|
|
394
|
+
SEL sel,
|
|
395
|
+
NSDictionary *config,
|
|
396
|
+
RCTPromiseResolveBlock resolve,
|
|
397
|
+
RCTPromiseRejectBlock reject) {
|
|
398
|
+
if (![NSThread isMainThread]) {
|
|
399
|
+
NSDictionary *cfg = config ?: @{};
|
|
400
|
+
dispatch_async(dispatch_get_main_queue(), ^{
|
|
401
|
+
CallInitializeBySelector(instance, sel, cfg, resolve, reject);
|
|
402
|
+
});
|
|
403
|
+
return;
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
IMP imp = [instance methodForSelector:sel];
|
|
407
|
+
if (!imp) {
|
|
408
|
+
NSMethodSignature *sig = [instance methodSignatureForSelector:sel];
|
|
409
|
+
if (!sig) {
|
|
410
|
+
NSError *err = [NSError errorWithDomain:@"Sdk" code:7 userInfo:@{NSLocalizedDescriptionKey:@"No signature"}];
|
|
411
|
+
reject(@"INIT_SIGNATURE_NOT_FOUND", @"initialize signature not found", err);
|
|
412
|
+
return;
|
|
413
|
+
}
|
|
414
|
+
NSInvocation *inv = [NSInvocation invocationWithMethodSignature:sig];
|
|
415
|
+
[inv setTarget:instance];
|
|
416
|
+
[inv setSelector:sel];
|
|
417
|
+
NSDictionary *cfg = config ?: @{};
|
|
418
|
+
RCTPromiseResolveBlock res = resolve;
|
|
419
|
+
RCTPromiseRejectBlock rej = reject;
|
|
420
|
+
[inv setArgument:&cfg atIndex:2];
|
|
421
|
+
[inv setArgument:&res atIndex:3];
|
|
422
|
+
[inv setArgument:&rej atIndex:4];
|
|
423
|
+
[inv invoke];
|
|
424
|
+
return;
|
|
425
|
+
}
|
|
426
|
+
|
|
427
|
+
void (*func)(id, SEL, NSDictionary *, RCTPromiseResolveBlock, RCTPromiseRejectBlock) =
|
|
428
|
+
(void (*)(id, SEL, NSDictionary *, RCTPromiseResolveBlock, RCTPromiseRejectBlock))imp;
|
|
429
|
+
|
|
430
|
+
NSDictionary *safeCfg = config ?: @{};
|
|
431
|
+
func(instance, sel, safeCfg, resolve, reject);
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
/// Initializes Swift module with config.
|
|
435
|
+
static void CallInitialize(id instance,
|
|
436
|
+
NSDictionary *config,
|
|
437
|
+
RCTPromiseResolveBlock resolve,
|
|
438
|
+
RCTPromiseRejectBlock reject) {
|
|
439
|
+
if (!instance) {
|
|
440
|
+
NSError *err = [NSError errorWithDomain:@"Sdk" code:1 userInfo:@{NSLocalizedDescriptionKey:@"SdkAppModule is nil"}];
|
|
441
|
+
reject(@"SWIFT_MODULE_NOT_FOUND", @"SdkAppModule instance is nil", err);
|
|
442
|
+
return;
|
|
443
|
+
}
|
|
444
|
+
|
|
445
|
+
SEL sel = SdkSel(@"initializeWithConfig:resolver:rejecter:");
|
|
446
|
+
if (!SdkResponds(instance, sel)) {
|
|
447
|
+
NSError *err = [NSError errorWithDomain:@"Sdk" code:6 userInfo:@{NSLocalizedDescriptionKey:@"initialize selector not found"}];
|
|
448
|
+
reject(@"INIT_METHOD_NOT_FOUND", @"initialize method not found", err);
|
|
449
|
+
return;
|
|
450
|
+
}
|
|
451
|
+
|
|
452
|
+
CallInitializeBySelector(instance, sel, config, resolve, reject);
|
|
453
|
+
}
|
|
454
|
+
|
|
455
|
+
/// Clears SDK state in Swift module.
|
|
456
|
+
static void CallClear(id instance,
|
|
457
|
+
RCTPromiseResolveBlock resolve,
|
|
458
|
+
RCTPromiseRejectBlock reject) {
|
|
459
|
+
CallPromise0Args(instance, @"clearWithResolver:rejecter:", resolve, reject);
|
|
460
|
+
}
|
|
461
|
+
|
|
462
|
+
#pragma mark - Events: Swift -> NotificationCenter -> ObjC++ -> JS
|
|
463
|
+
|
|
464
|
+
/// Notification name used by Swift layer to publish events.
|
|
465
|
+
static NSString * const SdkEventsNotificationName = @"AltcraftSdkEventNotification";
|
|
466
|
+
|
|
467
|
+
static id _Nullable gSdkEventsObserver = nil;
|
|
468
|
+
static id<RCTCallableJSModules> _Nullable gCallableJSModules = nil;
|
|
469
|
+
|
|
470
|
+
/// Returns callableJSModules from a module instance if present.
|
|
471
|
+
static id<RCTCallableJSModules> _Nullable SdkGetCallableFromModule(id moduleInstance) {
|
|
472
|
+
if (!moduleInstance) return nil;
|
|
473
|
+
|
|
474
|
+
SEL sel = SdkSel(@"callableJSModules");
|
|
475
|
+
if (![moduleInstance respondsToSelector:sel]) return nil;
|
|
476
|
+
|
|
477
|
+
id (*msgSend)(id, SEL) = (id (*)(id, SEL))objc_msgSend;
|
|
478
|
+
id v = msgSend(moduleInstance, sel);
|
|
479
|
+
return (id<RCTCallableJSModules>)v;
|
|
480
|
+
}
|
|
481
|
+
|
|
482
|
+
/// Returns bridge from a module instance if present (Old Arch).
|
|
483
|
+
static id _Nullable SdkGetBridgeAny(id moduleInstance) {
|
|
484
|
+
if (!moduleInstance) return nil;
|
|
485
|
+
|
|
486
|
+
SEL sel = SdkSel(@"bridge");
|
|
487
|
+
if (![moduleInstance respondsToSelector:sel]) return nil;
|
|
488
|
+
|
|
489
|
+
id (*msgSend)(id, SEL) = (id (*)(id, SEL))objc_msgSend;
|
|
490
|
+
return msgSend(moduleInstance, sel);
|
|
491
|
+
}
|
|
492
|
+
|
|
493
|
+
/// Checks if bridge supports enqueueing JS calls.
|
|
494
|
+
static BOOL SdkBridgeCanEnqueue(id bridgeObj) {
|
|
495
|
+
if (!bridgeObj) return NO;
|
|
496
|
+
|
|
497
|
+
SEL sel = SdkSel(@"enqueueJSCall:method:args:completion:");
|
|
498
|
+
return [bridgeObj respondsToSelector:sel];
|
|
499
|
+
}
|
|
500
|
+
|
|
501
|
+
/// Emits event using bridge enqueue (Old Arch).
|
|
502
|
+
static void SdkBridgeEnqueueEmit(id bridgeObj, NSDictionary *payload) {
|
|
503
|
+
SEL sel = SdkSel(@"enqueueJSCall:method:args:completion:");
|
|
504
|
+
if (![bridgeObj respondsToSelector:sel]) return;
|
|
505
|
+
|
|
506
|
+
NSArray *args = @[@"AltcraftSdkEvent", payload ?: @{}];
|
|
507
|
+
|
|
508
|
+
void (*func)(id, SEL, id, id, id, id) = (void (*)(id, SEL, id, id, id, id))objc_msgSend;
|
|
509
|
+
func(bridgeObj, sel, @"RCTDeviceEventEmitter", @"emit", args, (id)nil);
|
|
510
|
+
}
|
|
511
|
+
|
|
512
|
+
/// Emits event to JS via callableJSModules (preferred) or bridge enqueue.
|
|
513
|
+
static void SdkEmitEventToJS(id moduleInstance, NSDictionary *payload) {
|
|
514
|
+
NSDictionary *safePayload = (payload && [payload isKindOfClass:[NSDictionary class]]) ? payload : @{};
|
|
515
|
+
|
|
516
|
+
// 1) New Arch: callableJSModules.
|
|
517
|
+
id<RCTCallableJSModules> callable = gCallableJSModules;
|
|
518
|
+
if (!callable) {
|
|
519
|
+
callable = SdkGetCallableFromModule(moduleInstance);
|
|
520
|
+
if (callable) gCallableJSModules = callable;
|
|
521
|
+
}
|
|
522
|
+
if (callable) {
|
|
523
|
+
[callable invokeModule:@"RCTDeviceEventEmitter"
|
|
524
|
+
method:@"emit"
|
|
525
|
+
withArgs:@[@"AltcraftSdkEvent", safePayload]];
|
|
526
|
+
return;
|
|
527
|
+
}
|
|
528
|
+
|
|
529
|
+
// 2) Old Arch: bridge enqueueJSCall.
|
|
530
|
+
id bridgeObj = SdkGetBridgeAny(moduleInstance);
|
|
531
|
+
if (SdkBridgeCanEnqueue(bridgeObj)) {
|
|
532
|
+
SdkBridgeEnqueueEmit(bridgeObj, safePayload);
|
|
533
|
+
}
|
|
534
|
+
}
|
|
535
|
+
|
|
536
|
+
/// Installs NotificationCenter observer to forward events to JS.
|
|
537
|
+
static void SdkInstallEventsObserverIfNeeded(id moduleInstance) {
|
|
538
|
+
// Refresh callable cache if possible.
|
|
539
|
+
id<RCTCallableJSModules> callable = SdkGetCallableFromModule(moduleInstance);
|
|
540
|
+
if (callable) gCallableJSModules = callable;
|
|
541
|
+
|
|
542
|
+
if (gSdkEventsObserver != nil) return;
|
|
543
|
+
|
|
544
|
+
gSdkEventsObserver =
|
|
545
|
+
[[NSNotificationCenter defaultCenter] addObserverForName:SdkEventsNotificationName
|
|
546
|
+
object:nil
|
|
547
|
+
queue:[NSOperationQueue mainQueue]
|
|
548
|
+
usingBlock:^(NSNotification *note) {
|
|
549
|
+
NSDictionary *payload = note.userInfo;
|
|
550
|
+
|
|
551
|
+
// Debug visibility for event flow (safe to keep).
|
|
552
|
+
NSLog(@"[AltcraftSdk] event %@", payload);
|
|
553
|
+
|
|
554
|
+
SdkEmitEventToJS(moduleInstance, payload);
|
|
555
|
+
}];
|
|
556
|
+
}
|
|
557
|
+
|
|
558
|
+
/// Removes NotificationCenter observer.
|
|
559
|
+
static void SdkRemoveEventsObserverIfNeeded(void) {
|
|
560
|
+
if (!gSdkEventsObserver) return;
|
|
561
|
+
|
|
562
|
+
[[NSNotificationCenter defaultCenter] removeObserver:gSdkEventsObserver];
|
|
563
|
+
gSdkEventsObserver = nil;
|
|
564
|
+
gCallableJSModules = nil;
|
|
565
|
+
}
|
|
566
|
+
|
|
567
|
+
#pragma mark - Typed config -> NSDictionary (New Arch)
|
|
568
|
+
|
|
569
|
+
#ifdef RCT_NEW_ARCH_ENABLED
|
|
570
|
+
|
|
571
|
+
template <typename T>
|
|
572
|
+
struct SdkIsStdOptional : std::false_type {};
|
|
573
|
+
|
|
574
|
+
template <typename T>
|
|
575
|
+
struct SdkIsStdOptional<std::optional<T>> : std::true_type {};
|
|
576
|
+
|
|
577
|
+
/// Converts NSString* / optional NSString* to NSString* or nil.
|
|
578
|
+
template <typename T>
|
|
579
|
+
static NSString *SdkGetStringOrNil(const T &v) {
|
|
580
|
+
using D = std::decay_t<T>;
|
|
581
|
+
if constexpr (std::is_same_v<D, NSString *>) {
|
|
582
|
+
return v;
|
|
583
|
+
} else if constexpr (SdkIsStdOptional<D>::value) {
|
|
584
|
+
return v.has_value() ? v.value() : nil;
|
|
585
|
+
} else {
|
|
586
|
+
return nil;
|
|
587
|
+
}
|
|
588
|
+
}
|
|
589
|
+
|
|
590
|
+
/// Converts bool / optional bool to NSNumber* or nil.
|
|
591
|
+
template <typename T>
|
|
592
|
+
static NSNumber *SdkGetBoolNumberOrNil(const T &v) {
|
|
593
|
+
using D = std::decay_t<T>;
|
|
594
|
+
if constexpr (std::is_same_v<D, bool>) {
|
|
595
|
+
return @(v);
|
|
596
|
+
} else if constexpr (SdkIsStdOptional<D>::value) {
|
|
597
|
+
return v.has_value() ? @(v.value()) : nil;
|
|
598
|
+
} else {
|
|
599
|
+
return nil;
|
|
600
|
+
}
|
|
601
|
+
}
|
|
602
|
+
|
|
603
|
+
#import <limits.h>
|
|
604
|
+
|
|
605
|
+
/// Converts LazyVector<NSString*> to NSArray (capped to INT_MAX).
|
|
606
|
+
static NSArray *SdkLazyVectorToNSArray(const facebook::react::LazyVector<NSString *> &vec) {
|
|
607
|
+
const size_t n = vec.size();
|
|
608
|
+
const size_t capped = (n > (size_t)INT_MAX) ? (size_t)INT_MAX : n;
|
|
609
|
+
|
|
610
|
+
NSMutableArray *arr = [NSMutableArray arrayWithCapacity:(NSUInteger)capped];
|
|
611
|
+
for (int i = 0; i < (int)capped; i++) {
|
|
612
|
+
NSString *s = vec[i];
|
|
613
|
+
[arr addObject:(s ? s : (id)kCFNull)];
|
|
614
|
+
}
|
|
615
|
+
|
|
616
|
+
return arr;
|
|
617
|
+
}
|
|
618
|
+
|
|
619
|
+
/// Converts LazyVector / optional LazyVector to NSArray* or nil.
|
|
620
|
+
template <typename T>
|
|
621
|
+
static NSArray *SdkGetStringArrayOrNil(const T &v) {
|
|
622
|
+
using D = std::decay_t<T>;
|
|
623
|
+
if constexpr (std::is_same_v<D, facebook::react::LazyVector<NSString *>>) {
|
|
624
|
+
return SdkLazyVectorToNSArray(v);
|
|
625
|
+
} else if constexpr (SdkIsStdOptional<D>::value) {
|
|
626
|
+
return v.has_value() ? SdkLazyVectorToNSArray(v.value()) : nil;
|
|
627
|
+
} else {
|
|
628
|
+
return nil;
|
|
629
|
+
}
|
|
630
|
+
}
|
|
631
|
+
|
|
632
|
+
/// Converts appInfo struct to NSDictionary* or nil.
|
|
633
|
+
template <typename T>
|
|
634
|
+
static NSDictionary *SdkGetAppInfoDictOrNil(const T &v) {
|
|
635
|
+
using D = std::decay_t<T>;
|
|
636
|
+
|
|
637
|
+
auto makeDict = [](NSString *appID, NSString *appIID, NSString *appVer) -> NSDictionary * {
|
|
638
|
+
if ((!appID || appID.length == 0) && (!appIID || appIID.length == 0) && (!appVer || appVer.length == 0)) {
|
|
639
|
+
return nil;
|
|
640
|
+
}
|
|
641
|
+
return @{
|
|
642
|
+
@"appID": appID ?: (id)kCFNull,
|
|
643
|
+
@"appIID": appIID ?: (id)kCFNull,
|
|
644
|
+
@"appVer": appVer ?: (id)kCFNull,
|
|
645
|
+
};
|
|
646
|
+
};
|
|
647
|
+
|
|
648
|
+
if constexpr (SdkIsStdOptional<D>::value) {
|
|
649
|
+
if (!v.has_value()) return nil;
|
|
650
|
+
const auto &info = v.value();
|
|
651
|
+
return makeDict(info.appID(), info.appIID(), info.appVer());
|
|
652
|
+
} else {
|
|
653
|
+
return makeDict(v.appID(), v.appIID(), v.appVer());
|
|
654
|
+
}
|
|
655
|
+
}
|
|
656
|
+
|
|
657
|
+
/// Converts typed RN config to NSDictionary for Swift module.
|
|
658
|
+
static NSDictionary *SdkConvertAltcraftConfigToNSDictionary(JS::NativeSdk::AltcraftConfig &cfg) {
|
|
659
|
+
NSString *apiUrl = cfg.apiUrl();
|
|
660
|
+
NSString *rToken = SdkGetStringOrNil(cfg.rToken());
|
|
661
|
+
NSNumber *enableLogging = SdkGetBoolNumberOrNil(cfg.enableLogging());
|
|
662
|
+
NSArray *providerPriorityList = SdkGetStringArrayOrNil(cfg.providerPriorityList());
|
|
663
|
+
NSDictionary *appInfo = SdkGetAppInfoDictOrNil(cfg.appInfo());
|
|
664
|
+
|
|
665
|
+
NSMutableDictionary *dict = [NSMutableDictionary new];
|
|
666
|
+
dict[@"apiUrl"] = apiUrl ?: @"";
|
|
667
|
+
dict[@"rToken"] = rToken ?: (id)kCFNull;
|
|
668
|
+
dict[@"appInfo"] = appInfo ?: (id)kCFNull;
|
|
669
|
+
dict[@"providerPriorityList"] = providerPriorityList ?: (id)kCFNull;
|
|
670
|
+
dict[@"enableLogging"] = enableLogging ?: (id)kCFNull;
|
|
671
|
+
return dict;
|
|
672
|
+
}
|
|
673
|
+
|
|
674
|
+
#endif // RCT_NEW_ARCH_ENABLED
|
|
675
|
+
|
|
676
|
+
#pragma mark - Module implementation
|
|
677
|
+
|
|
678
|
+
@implementation Sdk
|
|
679
|
+
|
|
680
|
+
#ifdef RCT_NEW_ARCH_ENABLED
|
|
681
|
+
/// Returns TurboModule instance for New Architecture.
|
|
682
|
+
- (std::shared_ptr<facebook::react::TurboModule>)getTurboModule:
|
|
683
|
+
(const facebook::react::ObjCTurboModule::InitParams &)params
|
|
684
|
+
{
|
|
685
|
+
return std::make_shared<facebook::react::NativeSdkSpecJSI>(params);
|
|
686
|
+
}
|
|
687
|
+
#endif // RCT_NEW_ARCH_ENABLED
|
|
688
|
+
|
|
689
|
+
/// React Native module name.
|
|
690
|
+
+ (NSString *)moduleName { return @"Sdk"; }
|
|
691
|
+
|
|
692
|
+
#ifdef RCT_NEW_ARCH_ENABLED
|
|
693
|
+
/// Initializes SDK using typed config (New Architecture).
|
|
694
|
+
- (void)initialize:(JS::NativeSdk::AltcraftConfig &)config
|
|
695
|
+
resolve:(RCTPromiseResolveBlock)resolve
|
|
696
|
+
reject:(RCTPromiseRejectBlock)reject
|
|
697
|
+
{
|
|
698
|
+
id module = SdkAppModuleSharedInstance();
|
|
699
|
+
EnsureProvidersInstalled(module);
|
|
700
|
+
|
|
701
|
+
NSDictionary *swiftConfig = SdkConvertAltcraftConfigToNSDictionary(config);
|
|
702
|
+
CallInitialize(module, swiftConfig, resolve, reject);
|
|
703
|
+
}
|
|
704
|
+
#endif // RCT_NEW_ARCH_ENABLED
|
|
705
|
+
|
|
706
|
+
/// Required by RN event emitter API (no-op).
|
|
707
|
+
- (void)addListener:(NSString *)eventName { (void)eventName; }
|
|
708
|
+
|
|
709
|
+
/// Required by RN event emitter API (no-op).
|
|
710
|
+
- (void)removeListeners:(double)count { (void)count; }
|
|
711
|
+
|
|
712
|
+
#pragma mark - Events API
|
|
713
|
+
|
|
714
|
+
/// Subscribes to SDK events and forwards them to JS.
|
|
715
|
+
- (void)subscribeToEvents
|
|
716
|
+
{
|
|
717
|
+
// Install observer and keep emitting to JS.
|
|
718
|
+
SdkInstallEventsObserverIfNeeded(self);
|
|
719
|
+
|
|
720
|
+
// Ask Swift to subscribe and start posting notifications.
|
|
721
|
+
id module = SdkAppModuleSharedInstance();
|
|
722
|
+
EnsureProvidersInstalled(module);
|
|
723
|
+
|
|
724
|
+
SEL sel = SdkSel(@"subscribeToEvents");
|
|
725
|
+
if (!SdkResponds(module, sel)) return;
|
|
726
|
+
|
|
727
|
+
IMP imp = [module methodForSelector:sel];
|
|
728
|
+
if (!imp) return;
|
|
729
|
+
|
|
730
|
+
void (*func)(id, SEL) = (void (*)(id, SEL))imp;
|
|
731
|
+
func(module, sel);
|
|
732
|
+
}
|
|
733
|
+
|
|
734
|
+
/// Unsubscribes from SDK events and stops forwarding to JS.
|
|
735
|
+
- (void)unsubscribeFromEvent
|
|
736
|
+
{
|
|
737
|
+
id module = SdkAppModuleSharedInstance();
|
|
738
|
+
EnsureProvidersInstalled(module);
|
|
739
|
+
|
|
740
|
+
SEL sel = SdkSel(@"unsubscribeFromEvent");
|
|
741
|
+
if (SdkResponds(module, sel)) {
|
|
742
|
+
IMP imp = [module methodForSelector:sel];
|
|
743
|
+
if (imp) {
|
|
744
|
+
void (*func)(id, SEL) = (void (*)(id, SEL))imp;
|
|
745
|
+
func(module, sel);
|
|
746
|
+
}
|
|
747
|
+
}
|
|
748
|
+
|
|
749
|
+
SdkRemoveEventsObserverIfNeeded();
|
|
750
|
+
}
|
|
751
|
+
|
|
752
|
+
#pragma mark - Common (JWT / AppGroup)
|
|
753
|
+
|
|
754
|
+
/// Sets JWT used by SDK requests.
|
|
755
|
+
- (void)setJwt:(NSString * _Nullable)token {
|
|
756
|
+
id module = SdkAppModuleSharedInstance();
|
|
757
|
+
CallSetToken(module, @"setJWT:", token);
|
|
758
|
+
}
|
|
759
|
+
|
|
760
|
+
/// Sets App Group name for shared storage.
|
|
761
|
+
- (void)setAppGroup:(NSString * _Nullable)groupName
|
|
762
|
+
resolve:(RCTPromiseResolveBlock)resolve
|
|
763
|
+
reject:(RCTPromiseRejectBlock)reject
|
|
764
|
+
{
|
|
765
|
+
id module = SdkAppModuleSharedInstance();
|
|
766
|
+
if (!module) {
|
|
767
|
+
NSError *err = [NSError errorWithDomain:@"Sdk" code:1 userInfo:@{NSLocalizedDescriptionKey:@"SdkAppModule not found"}];
|
|
768
|
+
reject(@"SWIFT_MODULE_NOT_FOUND", @"SdkAppModule class not found", err);
|
|
769
|
+
return;
|
|
770
|
+
}
|
|
771
|
+
|
|
772
|
+
CallSetAppGroup(module, groupName);
|
|
773
|
+
resolve((id)kCFNull);
|
|
774
|
+
}
|
|
775
|
+
|
|
776
|
+
#pragma mark - Platform-specific tokens
|
|
777
|
+
|
|
778
|
+
/// Android-only API stub (no-op on iOS).
|
|
779
|
+
- (void)setAndroidFcmToken:(NSString * _Nullable)token { (void)token; }
|
|
780
|
+
|
|
781
|
+
/// Android-only API stub (no-op on iOS).
|
|
782
|
+
- (void)setAndroidHmsToken:(NSString * _Nullable)token { (void)token; }
|
|
783
|
+
|
|
784
|
+
/// Sets iOS FCM token in Swift module.
|
|
785
|
+
- (void)setIosFcmToken:(NSString * _Nullable)token {
|
|
786
|
+
id module = SdkAppModuleSharedInstance();
|
|
787
|
+
EnsureProvidersInstalled(module);
|
|
788
|
+
CallSetToken(module, @"setFCM:", token);
|
|
789
|
+
}
|
|
790
|
+
|
|
791
|
+
/// Sets iOS HMS token in Swift module (if used).
|
|
792
|
+
- (void)setIosHmsToken:(NSString * _Nullable)token {
|
|
793
|
+
id module = SdkAppModuleSharedInstance();
|
|
794
|
+
EnsureProvidersInstalled(module);
|
|
795
|
+
CallSetToken(module, @"setHMS:", token);
|
|
796
|
+
}
|
|
797
|
+
|
|
798
|
+
/// Sets APNs token in Swift module.
|
|
799
|
+
- (void)setApnsToken:(NSString * _Nullable)token {
|
|
800
|
+
id module = SdkAppModuleSharedInstance();
|
|
801
|
+
EnsureProvidersInstalled(module);
|
|
802
|
+
CallSetToken(module, @"setAPNS:", token);
|
|
803
|
+
}
|
|
804
|
+
|
|
805
|
+
/// RuStore-only API stub (no-op on iOS).
|
|
806
|
+
- (void)setRustoreToken:(NSString * _Nullable)token { (void)token; }
|
|
807
|
+
|
|
808
|
+
#pragma mark - Subscription (void)
|
|
809
|
+
|
|
810
|
+
/// Subscribes device for push notifications.
|
|
811
|
+
- (void)pushSubscribe:(NSNumber *)sync
|
|
812
|
+
profileFields:(NSDictionary *)profileFields
|
|
813
|
+
customFields:(NSDictionary *)customFields
|
|
814
|
+
cats:(NSArray *)cats
|
|
815
|
+
replace:(NSNumber *)replace
|
|
816
|
+
skipTriggers:(NSNumber *)skipTriggers
|
|
817
|
+
{
|
|
818
|
+
id module = SdkAppModuleSharedInstance();
|
|
819
|
+
EnsureProvidersInstalled(module);
|
|
820
|
+
|
|
821
|
+
CallPushSubscription(module,
|
|
822
|
+
@"pushSubscribe:profileFields:customFields:cats:replace:skipTriggers:",
|
|
823
|
+
(NSNumber *)SdkNilIfNSNull(sync),
|
|
824
|
+
(NSDictionary *)SdkNilIfNSNull(profileFields),
|
|
825
|
+
(NSDictionary *)SdkNilIfNSNull(customFields),
|
|
826
|
+
(NSArray *)SdkNilIfNSNull(cats),
|
|
827
|
+
(NSNumber *)SdkNilIfNSNull(replace),
|
|
828
|
+
(NSNumber *)SdkNilIfNSNull(skipTriggers));
|
|
829
|
+
}
|
|
830
|
+
|
|
831
|
+
/// Suspends push subscription.
|
|
832
|
+
- (void)pushSuspend:(NSNumber *)sync
|
|
833
|
+
profileFields:(NSDictionary *)profileFields
|
|
834
|
+
customFields:(NSDictionary *)customFields
|
|
835
|
+
cats:(NSArray *)cats
|
|
836
|
+
replace:(NSNumber *)replace
|
|
837
|
+
skipTriggers:(NSNumber *)skipTriggers
|
|
838
|
+
{
|
|
839
|
+
id module = SdkAppModuleSharedInstance();
|
|
840
|
+
EnsureProvidersInstalled(module);
|
|
841
|
+
|
|
842
|
+
CallPushSubscription(module,
|
|
843
|
+
@"pushSuspend:profileFields:customFields:cats:replace:skipTriggers:",
|
|
844
|
+
(NSNumber *)SdkNilIfNSNull(sync),
|
|
845
|
+
(NSDictionary *)SdkNilIfNSNull(profileFields),
|
|
846
|
+
(NSDictionary *)SdkNilIfNSNull(customFields),
|
|
847
|
+
(NSArray *)SdkNilIfNSNull(cats),
|
|
848
|
+
(NSNumber *)SdkNilIfNSNull(replace),
|
|
849
|
+
(NSNumber *)SdkNilIfNSNull(skipTriggers));
|
|
850
|
+
}
|
|
851
|
+
|
|
852
|
+
/// Unsubscribes device from push notifications.
|
|
853
|
+
- (void)pushUnSubscribe:(NSNumber *)sync
|
|
854
|
+
profileFields:(NSDictionary *)profileFields
|
|
855
|
+
customFields:(NSDictionary *)customFields
|
|
856
|
+
cats:(NSArray *)cats
|
|
857
|
+
replace:(NSNumber *)replace
|
|
858
|
+
skipTriggers:(NSNumber *)skipTriggers
|
|
859
|
+
{
|
|
860
|
+
id module = SdkAppModuleSharedInstance();
|
|
861
|
+
EnsureProvidersInstalled(module);
|
|
862
|
+
|
|
863
|
+
CallPushSubscription(module,
|
|
864
|
+
@"pushUnSubscribe:profileFields:customFields:cats:replace:skipTriggers:",
|
|
865
|
+
(NSNumber *)SdkNilIfNSNull(sync),
|
|
866
|
+
(NSDictionary *)SdkNilIfNSNull(profileFields),
|
|
867
|
+
(NSDictionary *)SdkNilIfNSNull(customFields),
|
|
868
|
+
(NSArray *)SdkNilIfNSNull(cats),
|
|
869
|
+
(NSNumber *)SdkNilIfNSNull(replace),
|
|
870
|
+
(NSNumber *)SdkNilIfNSNull(skipTriggers));
|
|
871
|
+
}
|
|
872
|
+
|
|
873
|
+
#pragma mark - MobileEvent (void)
|
|
874
|
+
|
|
875
|
+
/// Sends a custom mobile event to backend.
|
|
876
|
+
- (void)mobileEvent:(NSString *)sid
|
|
877
|
+
eventName:(NSString *)eventName
|
|
878
|
+
sendMessageId:(NSString * _Nullable)sendMessageId
|
|
879
|
+
payload:(NSDictionary *)payload
|
|
880
|
+
matching:(NSDictionary *)matching
|
|
881
|
+
matchingType:(NSString * _Nullable)matchingType
|
|
882
|
+
profileFields:(NSDictionary *)profileFields
|
|
883
|
+
{
|
|
884
|
+
id module = SdkAppModuleSharedInstance();
|
|
885
|
+
EnsureProvidersInstalled(module);
|
|
886
|
+
|
|
887
|
+
CallMobileEvent(module,
|
|
888
|
+
(NSString *)SdkNilIfNSNull(sid),
|
|
889
|
+
(NSString *)SdkNilIfNSNull(eventName),
|
|
890
|
+
(NSString *)SdkNilIfNSNull(sendMessageId),
|
|
891
|
+
(NSDictionary *)SdkNilIfNSNull(payload),
|
|
892
|
+
(NSDictionary *)SdkNilIfNSNull(matching),
|
|
893
|
+
(NSString *)SdkNilIfNSNull(matchingType),
|
|
894
|
+
(NSDictionary *)SdkNilIfNSNull(profileFields));
|
|
895
|
+
}
|
|
896
|
+
|
|
897
|
+
#pragma mark - Promises
|
|
898
|
+
|
|
899
|
+
/// Unsuspends current push subscription.
|
|
900
|
+
- (void)unSuspendPushSubscription:(RCTPromiseResolveBlock)resolve
|
|
901
|
+
reject:(RCTPromiseRejectBlock)reject
|
|
902
|
+
{
|
|
903
|
+
id module = SdkAppModuleSharedInstance();
|
|
904
|
+
EnsureProvidersInstalled(module);
|
|
905
|
+
CallPromise0Args(module, @"unSuspendPushSubscriptionWithResolver:rejecter:", resolve, reject);
|
|
906
|
+
}
|
|
907
|
+
|
|
908
|
+
/// Returns status of latest subscription attempt.
|
|
909
|
+
- (void)getStatusOfLatestSubscription:(RCTPromiseResolveBlock)resolve
|
|
910
|
+
reject:(RCTPromiseRejectBlock)reject
|
|
911
|
+
{
|
|
912
|
+
id module = SdkAppModuleSharedInstance();
|
|
913
|
+
EnsureProvidersInstalled(module);
|
|
914
|
+
CallPromise0Args(module, @"getStatusOfLatestSubscriptionWithResolver:rejecter:", resolve, reject);
|
|
915
|
+
}
|
|
916
|
+
|
|
917
|
+
/// Returns current subscription status.
|
|
918
|
+
- (void)getStatusForCurrentSubscription:(RCTPromiseResolveBlock)resolve
|
|
919
|
+
reject:(RCTPromiseRejectBlock)reject
|
|
920
|
+
{
|
|
921
|
+
id module = SdkAppModuleSharedInstance();
|
|
922
|
+
EnsureProvidersInstalled(module);
|
|
923
|
+
CallPromise0Args(module, @"getStatusForCurrentSubscriptionWithResolver:rejecter:", resolve, reject);
|
|
924
|
+
}
|
|
925
|
+
|
|
926
|
+
/// Returns status of latest subscription for a specific provider.
|
|
927
|
+
- (void)getStatusOfLatestSubscriptionForProvider:(NSString * _Nullable)provider
|
|
928
|
+
resolve:(RCTPromiseResolveBlock)resolve
|
|
929
|
+
reject:(RCTPromiseRejectBlock)reject
|
|
930
|
+
{
|
|
931
|
+
id module = SdkAppModuleSharedInstance();
|
|
932
|
+
EnsureProvidersInstalled(module);
|
|
933
|
+
NSString *p = (NSString *)SdkNilIfNSNull(provider);
|
|
934
|
+
CallPromise1StringArg(module,
|
|
935
|
+
@"getStatusOfLatestSubscriptionForProviderWithProvider:resolver:rejecter:",
|
|
936
|
+
p,
|
|
937
|
+
resolve,
|
|
938
|
+
reject);
|
|
939
|
+
}
|
|
940
|
+
|
|
941
|
+
/// Clears SDK state (tokens, cached values, etc).
|
|
942
|
+
- (void)clear:(RCTPromiseResolveBlock)resolve
|
|
943
|
+
reject:(RCTPromiseRejectBlock)reject
|
|
944
|
+
{
|
|
945
|
+
id module = SdkAppModuleSharedInstance();
|
|
946
|
+
EnsureProvidersInstalled(module);
|
|
947
|
+
CallClear(module, resolve, reject);
|
|
948
|
+
}
|
|
949
|
+
|
|
950
|
+
/// Returns current push token info {provider, token}.
|
|
951
|
+
- (void)getPushToken:(RCTPromiseResolveBlock)resolve
|
|
952
|
+
reject:(RCTPromiseRejectBlock)reject
|
|
953
|
+
{
|
|
954
|
+
id module = SdkAppModuleSharedInstance();
|
|
955
|
+
if (!module) {
|
|
956
|
+
NSError *err = [NSError errorWithDomain:@"Sdk" code:1 userInfo:@{NSLocalizedDescriptionKey:@"SdkAppModule not found"}];
|
|
957
|
+
reject(@"SWIFT_MODULE_NOT_FOUND", @"SdkAppModule class not found", err);
|
|
958
|
+
return;
|
|
959
|
+
}
|
|
960
|
+
|
|
961
|
+
EnsureProvidersInstalled(module);
|
|
962
|
+
|
|
963
|
+
CallGetPushToken(module, ^(id _Nullable tokenObjC) {
|
|
964
|
+
if (!tokenObjC) { resolve((id)kCFNull); return; }
|
|
965
|
+
|
|
966
|
+
NSString *provider = nil;
|
|
967
|
+
NSString *token = nil;
|
|
968
|
+
|
|
969
|
+
@try {
|
|
970
|
+
provider = [tokenObjC valueForKey:@"provider"];
|
|
971
|
+
token = [tokenObjC valueForKey:@"token"];
|
|
972
|
+
} @catch (__unused NSException *e) {}
|
|
973
|
+
|
|
974
|
+
if (!provider && !token) { resolve((id)kCFNull); return; }
|
|
975
|
+
|
|
976
|
+
resolve(@{
|
|
977
|
+
@"provider": provider ?: (id)kCFNull,
|
|
978
|
+
@"token": token ?: (id)kCFNull
|
|
979
|
+
});
|
|
980
|
+
});
|
|
981
|
+
}
|
|
982
|
+
|
|
983
|
+
/// Deletes device token for provider.
|
|
984
|
+
- (void)deleteDeviceToken:(NSString * _Nullable)provider
|
|
985
|
+
resolve:(RCTPromiseResolveBlock)resolve
|
|
986
|
+
reject:(RCTPromiseRejectBlock)reject
|
|
987
|
+
{
|
|
988
|
+
id module = SdkAppModuleSharedInstance();
|
|
989
|
+
if (!module) {
|
|
990
|
+
NSError *err = [NSError errorWithDomain:@"Sdk" code:1 userInfo:@{NSLocalizedDescriptionKey:@"SdkAppModule not found"}];
|
|
991
|
+
reject(@"SWIFT_MODULE_NOT_FOUND", @"SdkAppModule class not found", err);
|
|
992
|
+
return;
|
|
993
|
+
}
|
|
994
|
+
|
|
995
|
+
EnsureProvidersInstalled(module);
|
|
996
|
+
|
|
997
|
+
CallDeleteDeviceToken(module, provider, ^(BOOL ok) {
|
|
998
|
+
if (ok) resolve((id)kCFNull);
|
|
999
|
+
else {
|
|
1000
|
+
NSError *err = [NSError errorWithDomain:@"Sdk" code:2 userInfo:@{NSLocalizedDescriptionKey:@"Unknown provider"}];
|
|
1001
|
+
reject(@"INVALID_PROVIDER", @"Unknown provider for deleteDeviceToken", err);
|
|
1002
|
+
}
|
|
1003
|
+
});
|
|
1004
|
+
}
|
|
1005
|
+
|
|
1006
|
+
/// Forces push token update.
|
|
1007
|
+
- (void)forcedTokenUpdate:(RCTPromiseResolveBlock)resolve
|
|
1008
|
+
reject:(RCTPromiseRejectBlock)reject
|
|
1009
|
+
{
|
|
1010
|
+
id module = SdkAppModuleSharedInstance();
|
|
1011
|
+
if (!module) {
|
|
1012
|
+
NSError *err = [NSError errorWithDomain:@"Sdk" code:1 userInfo:@{NSLocalizedDescriptionKey:@"SdkAppModule not found"}];
|
|
1013
|
+
reject(@"SWIFT_MODULE_NOT_FOUND", @"SdkAppModule class not found", err);
|
|
1014
|
+
return;
|
|
1015
|
+
}
|
|
1016
|
+
|
|
1017
|
+
EnsureProvidersInstalled(module);
|
|
1018
|
+
|
|
1019
|
+
CallForcedTokenUpdate(module, ^{
|
|
1020
|
+
resolve((id)kCFNull);
|
|
1021
|
+
});
|
|
1022
|
+
}
|
|
1023
|
+
|
|
1024
|
+
/// Updates provider priority list used by SDK.
|
|
1025
|
+
- (void)changePushProviderPriorityList:(NSArray * _Nullable)priorityList
|
|
1026
|
+
resolve:(RCTPromiseResolveBlock)resolve
|
|
1027
|
+
reject:(RCTPromiseRejectBlock)reject
|
|
1028
|
+
{
|
|
1029
|
+
id module = SdkAppModuleSharedInstance();
|
|
1030
|
+
if (!module) {
|
|
1031
|
+
NSError *err = [NSError errorWithDomain:@"Sdk" code:1 userInfo:@{NSLocalizedDescriptionKey:@"SdkAppModule not found"}];
|
|
1032
|
+
reject(@"SWIFT_MODULE_NOT_FOUND", @"SdkAppModule class not found", err);
|
|
1033
|
+
return;
|
|
1034
|
+
}
|
|
1035
|
+
|
|
1036
|
+
EnsureProvidersInstalled(module);
|
|
1037
|
+
|
|
1038
|
+
NSArray *list = priorityList ?: @[];
|
|
1039
|
+
CallChangeProviderPriorityList(module, list, ^(BOOL ok) {
|
|
1040
|
+
if (ok) resolve((id)kCFNull);
|
|
1041
|
+
else {
|
|
1042
|
+
NSError *err = [NSError errorWithDomain:@"Sdk" code:3 userInfo:@{NSLocalizedDescriptionKey:@"Change priority failed"}];
|
|
1043
|
+
reject(@"CHANGE_PRIORITY_FAILED", @"Failed to change provider priority list", err);
|
|
1044
|
+
}
|
|
1045
|
+
});
|
|
1046
|
+
}
|
|
1047
|
+
|
|
1048
|
+
/// Sets push token for explicit provider (advanced usage).
|
|
1049
|
+
- (void)setPushToken:(NSString *)provider
|
|
1050
|
+
token:(NSString * _Nullable)token
|
|
1051
|
+
resolve:(RCTPromiseResolveBlock)resolve
|
|
1052
|
+
reject:(RCTPromiseRejectBlock)reject
|
|
1053
|
+
{
|
|
1054
|
+
id module = SdkAppModuleSharedInstance();
|
|
1055
|
+
if (!module) {
|
|
1056
|
+
NSError *err = [NSError errorWithDomain:@"Sdk" code:1 userInfo:@{NSLocalizedDescriptionKey:@"SdkAppModule not found"}];
|
|
1057
|
+
reject(@"SWIFT_MODULE_NOT_FOUND", @"SdkAppModule class not found", err);
|
|
1058
|
+
return;
|
|
1059
|
+
}
|
|
1060
|
+
|
|
1061
|
+
EnsureProvidersInstalled(module);
|
|
1062
|
+
|
|
1063
|
+
if (!provider || provider.length == 0) {
|
|
1064
|
+
NSError *err = [NSError errorWithDomain:@"Sdk" code:4 userInfo:@{NSLocalizedDescriptionKey:@"Provider is empty"}];
|
|
1065
|
+
reject(@"INVALID_PROVIDER", @"Provider is empty", err);
|
|
1066
|
+
return;
|
|
1067
|
+
}
|
|
1068
|
+
|
|
1069
|
+
CallSetPushToken(module, provider, token);
|
|
1070
|
+
resolve((id)kCFNull);
|
|
1071
|
+
}
|
|
1072
|
+
|
|
1073
|
+
#pragma mark - iOS-only stubs (not implemented)
|
|
1074
|
+
|
|
1075
|
+
/// iOS stub: delivery tracking is handled by server/other platforms.
|
|
1076
|
+
- (void)deliveryEvent:(NSDictionary * _Nullable)message
|
|
1077
|
+
messageUID:(NSString * _Nullable)messageUID
|
|
1078
|
+
{
|
|
1079
|
+
(void)message;
|
|
1080
|
+
(void)messageUID;
|
|
1081
|
+
// Intentionally not implemented on iOS.
|
|
1082
|
+
}
|
|
1083
|
+
|
|
1084
|
+
/// iOS stub: open tracking is handled by OS / deep-link flow.
|
|
1085
|
+
- (void)openEvent:(NSDictionary * _Nullable)message
|
|
1086
|
+
messageUID:(NSString * _Nullable)messageUID
|
|
1087
|
+
{
|
|
1088
|
+
(void)message;
|
|
1089
|
+
(void)messageUID;
|
|
1090
|
+
// Intentionally not implemented on iOS.
|
|
1091
|
+
}
|
|
1092
|
+
|
|
1093
|
+
/// iOS stub: retry control is managed in Swift layer / system scheduling.
|
|
1094
|
+
- (void)reinitializeRetryControlInThisSession
|
|
1095
|
+
{
|
|
1096
|
+
// Intentionally not implemented on iOS.
|
|
1097
|
+
}
|
|
1098
|
+
|
|
1099
|
+
/// iOS stub: permission prompt should be initiated by the host app UI.
|
|
1100
|
+
- (void)requestNotificationPermission
|
|
1101
|
+
{
|
|
1102
|
+
// Intentionally not implemented on iOS.
|
|
1103
|
+
}
|
|
1104
|
+
|
|
1105
|
+
/// iOS stub: push handling is done via AppDelegate / UNUserNotificationCenter callbacks.
|
|
1106
|
+
- (void)takePush:(NSDictionary * _Nullable)message
|
|
1107
|
+
{
|
|
1108
|
+
(void)message;
|
|
1109
|
+
// Intentionally not implemented on iOS.
|
|
1110
|
+
}
|
|
1111
|
+
|
|
1112
|
+
@end
|