Skip to content

Commit ab4b69f

Browse files
Merge pull request #96 from Iterable/feature/mob-2393-set-endpoints
[MOB-2393] - Allow initialize to be called using api and links endpoint.
2 parents 4386396 + 2e16b79 commit ab4b69f

File tree

3 files changed

+134
-75
lines changed

3 files changed

+134
-75
lines changed

ios/RNIterableAPI/RNIterableAPI.m

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@ @interface RCT_EXTERN_REMAP_MODULE(RNIterableAPI, ReactIterableAPI, NSObject)
1212
config: (nonnull NSDictionary *) config
1313
version: (nonnull NSString *) version)
1414

15+
RCT_EXTERN_METHOD(initialize2WithApiKey: (nonnull NSString *) apiKey
16+
config: (nonnull NSDictionary *) config
17+
apiEndPointOverride: (nonnull NSString *) apiEndPoint
18+
linksEndPointOverride: (nonnull NSString *) linksEndPoint
19+
version: (nonnull NSString *) version)
20+
1521
RCT_EXTERN_METHOD(setEmail: (NSString *) email)
1622

1723
RCT_EXTERN_METHOD(getEmail: (RCTPromiseResolveBlock) resolve

ios/RNIterableAPI/ReactIterableAPI.swift

Lines changed: 55 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -51,31 +51,27 @@ class ReactIterableAPI: RCTEventEmitter {
5151
}
5252

5353
@objc(initializeWithApiKey:config:version:)
54-
func initialize(apiKey: String, config configDict: [AnyHashable: Any], version: String) {
54+
func initialize(apiKey: String,
55+
config configDict: [AnyHashable: Any],
56+
version: String) {
5557
ITBInfo()
5658

57-
let launchOptions = createLaunchOptions()
58-
let iterableConfig = IterableConfig.from(dict: configDict)
59-
if let urlHandlerPresent = configDict["urlHandlerPresent"] as? Bool, urlHandlerPresent == true {
60-
iterableConfig.urlDelegate = self
61-
}
62-
63-
if let customActionHandlerPresent = configDict["customActionHandlerPresent"] as? Bool, customActionHandlerPresent == true {
64-
iterableConfig.customActionDelegate = self
65-
}
66-
67-
if let inAppHandlerPresent = configDict["inAppHandlerPresent"] as? Bool, inAppHandlerPresent == true {
68-
iterableConfig.inAppDelegate = self
69-
}
70-
71-
if let authHandlerPresent = configDict["authHandlerPresent"] as? Bool, authHandlerPresent {
72-
iterableConfig.authDelegate = self
73-
}
74-
75-
DispatchQueue.main.async {
76-
IterableAPI.initialize(apiKey: apiKey, launchOptions: launchOptions, config: iterableConfig)
77-
IterableAPI.setDeviceAttribute(name: "reactNativeSDKVersion", value: version)
78-
}
59+
initialize(withApiKey: apiKey, config: configDict, version: version)
60+
}
61+
62+
@objc(initialize2WithApiKey:config:apiEndPointOverride:linksEndPointOverride:version:)
63+
func initialize2(apiKey: String,
64+
config configDict: [AnyHashable: Any],
65+
version: String,
66+
apiEndPointOverride: String,
67+
linksEndPointOverride: String) {
68+
ITBInfo()
69+
70+
initialize(withApiKey: apiKey,
71+
config: configDict,
72+
version: version,
73+
apiEndPointOverride: apiEndPointOverride,
74+
linksEndPointOverride: linksEndPointOverride)
7975
}
8076

8177
@objc(setEmail:)
@@ -411,6 +407,42 @@ class ReactIterableAPI: RCTEventEmitter {
411407
private var passedAuthToken: String?
412408
private var authHandlerSemaphore = DispatchSemaphore(value: 0)
413409

410+
private func initialize(withApiKey apiKey: String,
411+
config configDict: [AnyHashable: Any],
412+
version: String,
413+
apiEndPointOverride: String? = nil,
414+
linksEndPointOverride: String? = nil) {
415+
416+
ITBInfo()
417+
418+
let launchOptions = createLaunchOptions()
419+
let iterableConfig = IterableConfig.from(dict: configDict)
420+
if let urlHandlerPresent = configDict["urlHandlerPresent"] as? Bool, urlHandlerPresent == true {
421+
iterableConfig.urlDelegate = self
422+
}
423+
424+
if let customActionHandlerPresent = configDict["customActionHandlerPresent"] as? Bool, customActionHandlerPresent == true {
425+
iterableConfig.customActionDelegate = self
426+
}
427+
428+
if let inAppHandlerPresent = configDict["inAppHandlerPresent"] as? Bool, inAppHandlerPresent == true {
429+
iterableConfig.inAppDelegate = self
430+
}
431+
432+
if let authHandlerPresent = configDict["authHandlerPresent"] as? Bool, authHandlerPresent {
433+
iterableConfig.authDelegate = self
434+
}
435+
436+
DispatchQueue.main.async {
437+
IterableAPI.initialize2(apiKey: apiKey,
438+
launchOptions: launchOptions,
439+
config: iterableConfig,
440+
apiEndPointOverride: apiEndPointOverride,
441+
linksEndPointOverride: linksEndPointOverride)
442+
IterableAPI.setDeviceAttribute(name: "reactNativeSDKVersion", value: version)
443+
}
444+
}
445+
414446
private func createLaunchOptions() -> [UIApplication.LaunchOptionsKey: Any]? {
415447
guard let bridge = bridge else {
416448
return nil

ts/Iterable.ts

Lines changed: 73 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -189,62 +189,23 @@ class Iterable {
189189
static initialize(apiKey: string, config: IterableConfig = new IterableConfig()) {
190190
console.log("initialize: " + apiKey);
191191

192-
if (config.urlHandler) {
193-
RNEventEmitter.addListener(
194-
EventName.handleUrlCalled,
195-
(dict) => {
196-
const url = dict["url"]
197-
const context = IterableActionContext.fromDict(dict["context"])
198-
if (config.urlHandler!(url, context) == false) {
199-
Linking.canOpenURL(url)
200-
.then(canOpen => {
201-
if (canOpen) { Linking.openURL(url) }
202-
})
203-
.catch(reason => { console.log("could not open url: " + reason) })
204-
}
205-
}
206-
)
207-
}
208-
209-
if (config.customActionHandler) {
210-
RNEventEmitter.addListener(
211-
EventName.handleCustomActionCalled,
212-
(dict) => {
213-
const action = IterableAction.fromDict(dict["action"])
214-
const context = IterableActionContext.fromDict(dict["context"])
215-
config.customActionHandler!(action, context)
216-
}
217-
)
218-
}
192+
this.setupEventHandlers(config)
193+
const version = this.getVersionFromPackageJson()
219194

220-
if (config.inAppHandler) {
221-
RNEventEmitter.addListener(
222-
EventName.handleInAppCalled,
223-
(messageDict) => {
224-
const message = IterableInAppMessage.fromDict(messageDict)
225-
const result = config.inAppHandler!(message)
226-
RNIterableAPI.setInAppShowResponse(result)
227-
}
228-
)
229-
}
195+
RNIterableAPI.initializeWithApiKey(apiKey, config.toDict(), version)
196+
}
230197

231-
if (config.authHandler) {
232-
RNEventEmitter.addListener(
233-
EventName.handleAuthCalled,
234-
() => {
235-
config.authHandler!()
236-
.then(authToken => {
237-
RNIterableAPI.passAlongAuthToken(authToken)
238-
})
239-
}
240-
)
241-
}
198+
/**
199+
* DO NOT CALL THIS METHOD.
200+
* This method is used internally to connect to staging environment.
201+
*/
202+
static initialize2(apiKey: string, config: IterableConfig = new IterableConfig(), apiEndPoint: string, linksEndPoint: string) {
203+
console.log("initialize2: " + apiKey);
242204

243-
// Set version from package.json
244-
const json = require('../package.json')
245-
const version = json["version"] as string
205+
this.setupEventHandlers(config)
206+
const version = this.getVersionFromPackageJson()
246207

247-
RNIterableAPI.initializeWithApiKey(apiKey, config.toDict(), version)
208+
RNIterableAPI.initialize2WithApiKey(apiKey, config.toDict(), version, apiEndPoint, linksEndPoint)
248209
}
249210

250211
/**
@@ -436,6 +397,66 @@ class Iterable {
436397
console.log("updateSubscriptions")
437398
RNIterableAPI.updateSubscriptions(emailListIds, unsubscribedChannelIds, unsubscribedMessageTypeIds, subscribedMessageTypeIds, campaignId, templateId)
438399
}
400+
401+
// PRIVATE
402+
private static setupEventHandlers(config: IterableConfig) {
403+
if (config.urlHandler) {
404+
RNEventEmitter.addListener(
405+
EventName.handleUrlCalled,
406+
(dict) => {
407+
const url = dict["url"]
408+
const context = IterableActionContext.fromDict(dict["context"])
409+
if (config.urlHandler!(url, context) == false) {
410+
Linking.canOpenURL(url)
411+
.then(canOpen => {
412+
if (canOpen) { Linking.openURL(url) }
413+
})
414+
.catch(reason => { console.log("could not open url: " + reason) })
415+
}
416+
}
417+
)
418+
}
419+
420+
if (config.customActionHandler) {
421+
RNEventEmitter.addListener(
422+
EventName.handleCustomActionCalled,
423+
(dict) => {
424+
const action = IterableAction.fromDict(dict["action"])
425+
const context = IterableActionContext.fromDict(dict["context"])
426+
config.customActionHandler!(action, context)
427+
}
428+
)
429+
}
430+
431+
if (config.inAppHandler) {
432+
RNEventEmitter.addListener(
433+
EventName.handleInAppCalled,
434+
(messageDict) => {
435+
const message = IterableInAppMessage.fromDict(messageDict)
436+
const result = config.inAppHandler!(message)
437+
RNIterableAPI.setInAppShowResponse(result)
438+
}
439+
)
440+
}
441+
442+
if (config.authHandler) {
443+
RNEventEmitter.addListener(
444+
EventName.handleAuthCalled,
445+
() => {
446+
config.authHandler!()
447+
.then(authToken => {
448+
RNIterableAPI.passAlongAuthToken(authToken)
449+
})
450+
}
451+
)
452+
}
453+
}
454+
455+
private static getVersionFromPackageJson(): string {
456+
const json = require('../package.json')
457+
const version = json["version"] as string
458+
return version
459+
}
439460
}
440461

441462
export {

0 commit comments

Comments
 (0)