Skip to content

Commit 6323479

Browse files
authored
Merge pull request #394 from Telzio/feature/ios-setup-in-appdelegate
Support IOS setup in AppDelegate.m
2 parents aabd136 + e5607f5 commit 6323479

File tree

3 files changed

+56
-7
lines changed

3 files changed

+56
-7
lines changed

README.md

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,34 @@ const options = {
6161
RNCallKeep.setup(options).then(accepted => {});
6262
```
6363

64+
iOS only.
65+
66+
Alternative on iOS you can perform setup in `AppDelegate.m`. Doing this allows capturing events prior to the react native event bridge being up. Please be aware that calling setup in `AppDelegate.m` will ignore any subsequent calls to `RNCallKeep.setup();`.
67+
68+
```objective-c
69+
@implementation AppDelegate
70+
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
71+
{
72+
self.bridge = [[RCTBridge alloc] initWithDelegate:self launchOptions:launchOptions];
73+
74+
[RNCallKeep setup:@{
75+
@"appName": @"Awesome App",
76+
@"maximumCallGroups": @3,
77+
@"maximumCallsPerCallGroup": @1,
78+
@"supportsVideo": @NO,
79+
}];
80+
81+
RCTRootView *rootView = [[RCTRootView alloc] initWithBridge:self.bridge
82+
moduleName:@"App"
83+
initialProperties:nil];
84+
85+
// ======== OTHER CODE REDACTED ==========
86+
87+
return YES;
88+
}
89+
90+
```
91+
6492
- `options`: Object
6593
- `ios`: object
6694
- `appName`: string (required)
@@ -93,7 +121,7 @@ RNCallKeep.setup(options).then(accepted => {});
93121
- `additionalPermissions`: [PermissionsAndroid] (optional)
94122
Any additional permissions you'd like your app to have at first launch. Can be used to simplify permission flows and avoid
95123
multiple popups to the user at different times.
96-
124+
97125
`setup` calls internally `registerPhoneAccount` and `registerEvents`.
98126
99127
## Constants
@@ -641,6 +669,8 @@ Called as soon as JS context initializes if there were some actions performed by
641669

642670
Since iOS 13, you must display incoming call on receiving PushKit push notification. But if app was killed, it takes some time to create JS context. If user answers the call (or ends it) before JS context has been initialized, user actions will be passed as events array of this event. Similar situation can happen if user would like to start a call from Recents or similar iOS app, assuming that your app was in killed state.
643671

672+
In order for this event to reliably fire, it's necessary to perform setup in `AppDelegate.m`
673+
644674
**NOTE: You still need to subscribe / handle the rest events as usuall. This is just a helper whcih cache and propagate early fired events if and only if for "the native events which DID fire BEFORE js bridge is initialed", it does NOT mean this will have events each time when the app reopened.**
645675

646676
```js

ios/RNCallKeep/RNCallKeep.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,6 @@ continueUserActivity:(NSUserActivity *)userActivity
4545

4646
+ (BOOL)isCallActive:(NSString *)uuidString;
4747

48+
+ (void)setup:(NSDictionary *)options;
49+
4850
@end

ios/RNCallKeep/RNCallKeep.m

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#import <React/RCTConvert.h>
1313
#import <React/RCTEventDispatcher.h>
1414
#import <React/RCTUtils.h>
15+
#import <React/RCTLog.h>
1516

1617
#import <AVFoundation/AVAudioSession.h>
1718

@@ -43,6 +44,7 @@ @implementation RNCallKeep
4344
NSMutableArray *_delayedEvents;
4445
}
4546

47+
static bool isSetupNatively;
4648
static CXProvider* sharedProvider;
4749

4850
// should initialise in AppDelegate.m
@@ -55,7 +57,7 @@ - (instancetype)init
5557
#endif
5658
if (self = [super init]) {
5759
_isStartCallActionEventListenerAdded = NO;
58-
_delayedEvents = [NSMutableArray array];
60+
if (_delayedEvents == nil) _delayedEvents = [NSMutableArray array];
5961
}
6062
return self;
6163
}
@@ -118,10 +120,11 @@ - (void)sendEventWithNameWrapper:(NSString *)name body:(id)body {
118120
if (_hasListeners) {
119121
[self sendEventWithName:name body:body];
120122
} else {
121-
NSDictionary *dictionary = @{
122-
@"name": name,
123-
@"data": body
124-
};
123+
NSDictionary *dictionary = [NSDictionary dictionaryWithObjectsAndKeys:
124+
name, @"name",
125+
body, @"data",
126+
nil
127+
];
125128
[_delayedEvents addObject:dictionary];
126129
}
127130
}
@@ -133,8 +136,22 @@ + (void)initCallKitProvider {
133136
}
134137
}
135138

139+
+ (void)setup:(NSDictionary *)options {
140+
RNCallKeep *callKeep = [RNCallKeep allocWithZone: nil];
141+
[callKeep setup:options];
142+
isSetupNatively = YES;
143+
}
144+
136145
RCT_EXPORT_METHOD(setup:(NSDictionary *)options)
137146
{
147+
if (isSetupNatively) {
148+
#ifdef DEBUG
149+
NSLog(@"[RNCallKeep][setup] already setup");
150+
RCTLog(@"[RNCallKeep][setup] already setup in native code");
151+
#endif
152+
return;
153+
}
154+
138155
#ifdef DEBUG
139156
NSLog(@"[RNCallKeep][setup] options = %@", options);
140157
#endif
@@ -183,7 +200,7 @@ + (void)initCallKitProvider {
183200
supportsHolding:(BOOL)supportsHolding
184201
supportsDTMF:(BOOL)supportsDTMF
185202
supportsGrouping:(BOOL)supportsGrouping
186-
supportsUngrouping:(BOOL)supportsUngrouping)
203+
supportsUngrouping:(BOOL)supportsUngrouping)
187204
{
188205
[RNCallKeep reportNewIncomingCall: uuidString
189206
handle: handle

0 commit comments

Comments
 (0)