Skip to content

Commit a0e0bf1

Browse files
committed
calling setSafeAreaInsets in JS after resize
1 parent 83b1986 commit a0e0bf1

File tree

4 files changed

+33
-12
lines changed

4 files changed

+33
-12
lines changed

iOS_SDK/OneSignalSDK/Source/OSInAppMessageView.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ NS_ASSUME_NONNULL_BEGIN
4444

4545
- (instancetype _Nonnull)initWithMessage:(OSInAppMessageInternal *)inAppMessage withScriptMessageHandler:(id<WKScriptMessageHandler>)messageHandler;
4646
- (void)resetWebViewToMaxBoundsAndResizeHeight:(void (^) (NSNumber *newHeight)) completion;
47+
- (void)updateSafeAreaInsets;
4748
- (void)setupWebViewConstraints;
4849
- (void)loadReplacementURL:(NSURL *)url;
4950
- (void)loadedHtmlContent:(NSString *)html withBaseURL:(NSURL *)url;

iOS_SDK/OneSignalSDK/Source/OSInAppMessageView.m

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,28 @@ - (void)resetWebViewToMaxBoundsAndResizeHeight:(void (^) (NSNumber *newHeight))
151151
}];
152152
}
153153

154+
- (void)updateSafeAreaInsets {
155+
if (@available(iOS 11, *)) {
156+
UIWindow *keyWindow = UIApplication.sharedApplication.keyWindow;
157+
CGFloat top = keyWindow.safeAreaInsets.top;
158+
CGFloat bottom = keyWindow.safeAreaInsets.bottom;
159+
CGFloat right = keyWindow.safeAreaInsets.right;
160+
CGFloat left = keyWindow.safeAreaInsets.left;
161+
NSString *safeAreaInsetsObjectString = [NSString stringWithFormat:OS_JS_SAFE_AREA_INSETS_OBJ,top, bottom, right, left];
162+
163+
NSString *setInsetsString = [NSString stringWithFormat:OS_SET_SAFE_AREA_INSETS_METHOD, safeAreaInsetsObjectString];
164+
[self.webView evaluateJavaScript:setInsetsString completionHandler:^(NSDictionary *result, NSError * _Nullable error) {
165+
if (error) {
166+
NSString *errorMessage = [NSString stringWithFormat:@"Javascript Method: %@ Evaluated with Error: %@", OS_SET_SAFE_AREA_INSETS_METHOD, error];
167+
[OneSignal onesignal_Log:ONE_S_LL_ERROR message:errorMessage];
168+
return;
169+
}
170+
NSString *successMessage = [NSString stringWithFormat:@"Javascript Method: %@ Evaluated with Success: %@", OS_SET_SAFE_AREA_INSETS_METHOD, result];
171+
[OneSignal onesignal_Log:ONE_S_LL_VERBOSE message:successMessage];
172+
}];
173+
}
174+
}
175+
154176
- (NSNumber *)extractHeightFromMetaDataPayload:(NSDictionary *)result {
155177
return @([result[@"rect"][@"height"] intValue]);
156178
}

iOS_SDK/OneSignalSDK/Source/OSInAppMessageViewController.m

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -279,13 +279,7 @@ - (NSString *)setContentInsetsInHTML:(NSString *)html {
279279
CGFloat bottom = keyWindow.safeAreaInsets.bottom;
280280
CGFloat right = keyWindow.safeAreaInsets.right;
281281
CGFloat left = keyWindow.safeAreaInsets.left;
282-
NSString *safeAreaInsetsObjectString = [NSString stringWithFormat:@"{\n\
283-
top: %f,\n\
284-
bottom: %f,\n\
285-
right: %f,\n\
286-
left: %f,\n\
287-
}",top, bottom, right, left];
288-
//Script to set the tags for liquid tag substitution
282+
NSString *safeAreaInsetsObjectString = [NSString stringWithFormat:OS_JS_SAFE_AREA_INSETS_OBJ,top, bottom, right, left];
289283
NSString *insetsString = [NSString stringWithFormat:@"\n\n\
290284
<script> \
291285
setSafeAreaInsets(%@);\
@@ -706,10 +700,6 @@ - (BOOL)shouldDismissMessageWithPanGestureOffset:(double)offset withVelocity:(do
706700
}
707701
}
708702

709-
- (void)updateSafeAreaInsets {
710-
711-
}
712-
713703
/*
714704
This delegate function gets called when in-app html is load or action button is tapped
715705
*/
@@ -741,7 +731,7 @@ - (void)jsEventOccurredWithBody:(NSData *)body {
741731
case OSInAppMessageBridgeEventTypePageResize: {
742732
// Unused resize event for IAM during actions like orientation changes and displaying an IAM
743733
// self.message.height = event.resize.height;
744-
[self updateSafeAreaInsets];
734+
[self.messageView updateSafeAreaInsets];
745735
break;
746736
}
747737
case OSInAppMessageBridgeEventTypeActionTaken: {

iOS_SDK/OneSignalSDK/Source/OSInAppMessagingDefines.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,16 @@ typedef NS_ENUM(NSUInteger, OSTriggerOperatorType) {
9292
// Verify that a string is a valid dynamic trigger
9393
#define OS_IS_TRIGGER_PROPERTY(kind) [OS_TRIGGER_PROPERTY_STRINGS containsObject:property]
9494

95+
// Javascript object
96+
#define OS_JS_SAFE_AREA_INSETS_OBJ @"{\n\
97+
top: %f,\n\
98+
bottom: %f,\n\
99+
right: %f,\n\
100+
left: %f,\n\
101+
}"
95102
// JavaScript method names
96103
#define OS_JS_GET_PAGE_META_DATA_METHOD @"getPageMetaData()"
104+
#define OS_SET_SAFE_AREA_INSETS_METHOD @"setSafeAreaInsets(%@)"
97105

98106
#define PREFERRED_VARIANT_ORDER @[@"ios", @"app", @"all"]
99107

0 commit comments

Comments
 (0)