diff --git a/examples/demo/App/Views/Components/LogView.swift b/examples/demo/App/Views/Components/LogView.swift index 96ddb32b38..57d83e5aae 100644 --- a/examples/demo/App/Views/Components/LogView.swift +++ b/examples/demo/App/Views/Components/LogView.swift @@ -42,7 +42,7 @@ struct LogView: View { entryList } .frame(maxWidth: .infinity) - .background(OS.Color.logBackground) + .background(OS.Color.logBackground, ignoresSafeAreaEdges: .horizontal) .accessibilityElement(children: .contain) .accessibilityIdentifier("log_view") } diff --git a/examples/demo/App/Views/ContentView.swift b/examples/demo/App/Views/ContentView.swift index 4c632c9300..610c5f3ab2 100644 --- a/examples/demo/App/Views/ContentView.swift +++ b/examples/demo/App/Views/ContentView.swift @@ -79,7 +79,8 @@ struct ContentView: View { // Android's `scrollIntoView` `scrollableElement` param. .accessibilityIdentifier("main_scroll_view") } - .background(OS.Color.lightBackground.ignoresSafeArea()) + .background(OS.Color.lightBackground.ignoresSafeArea(edges: [.horizontal, .bottom])) + .background(OS.Color.primary.ignoresSafeArea()) .navigationBarTitleDisplayMode(.inline) .toolbarBackground(OS.Color.primary, for: .navigationBar) .toolbarBackground(.visible, for: .navigationBar) diff --git a/iOS_SDK/OneSignalSDK/OneSignal.xcodeproj/project.pbxproj b/iOS_SDK/OneSignalSDK/OneSignal.xcodeproj/project.pbxproj index 930554a0a0..8f2aea9c99 100644 --- a/iOS_SDK/OneSignalSDK/OneSignal.xcodeproj/project.pbxproj +++ b/iOS_SDK/OneSignalSDK/OneSignal.xcodeproj/project.pbxproj @@ -634,6 +634,12 @@ DEBAAE552A42174A00BF2C1C /* OSInAppMessageView.h in Headers */ = {isa = PBXBuildFile; fileRef = DEBAAE512A42174A00BF2C1C /* OSInAppMessageView.h */; }; DEBAAE562A42174A00BF2C1C /* OSInAppMessageViewController.h in Headers */ = {isa = PBXBuildFile; fileRef = DEBAAE522A42174A00BF2C1C /* OSInAppMessageViewController.h */; }; DEBAAE572A42174A00BF2C1C /* OSInAppMessageView.m in Sources */ = {isa = PBXBuildFile; fileRef = DEBAAE532A42174A00BF2C1C /* OSInAppMessageView.m */; }; + 5C1A7E0D2FB0A00100AB0004 /* OSInAppMessageInsets.h in Headers */ = {isa = PBXBuildFile; fileRef = 5C1A7E0D2FB0A00100AB0001 /* OSInAppMessageInsets.h */; }; + 5C1A7E0D2FB0A00100AB0005 /* OSInAppMessageInsets.m in Sources */ = {isa = PBXBuildFile; fileRef = 5C1A7E0D2FB0A00100AB0002 /* OSInAppMessageInsets.m */; }; + 5C1A7E0D2FB0A00100AB0006 /* OSInAppMessageInsetsTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5C1A7E0D2FB0A00100AB0003 /* OSInAppMessageInsetsTests.swift */; }; + 5C1A7E0E2FB0A00100AB0004 /* OSInAppMessageScroll.h in Headers */ = {isa = PBXBuildFile; fileRef = 5C1A7E0E2FB0A00100AB0001 /* OSInAppMessageScroll.h */; }; + 5C1A7E0E2FB0A00100AB0005 /* OSInAppMessageScroll.m in Sources */ = {isa = PBXBuildFile; fileRef = 5C1A7E0E2FB0A00100AB0002 /* OSInAppMessageScroll.m */; }; + 5C1A7E0E2FB0A00100AB0006 /* OSInAppMessageScrollTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5C1A7E0E2FB0A00100AB0003 /* OSInAppMessageScrollTests.swift */; }; DEBAAE602A42175A00BF2C1C /* OSDynamicTriggerController.m in Sources */ = {isa = PBXBuildFile; fileRef = DEBAAE582A42175900BF2C1C /* OSDynamicTriggerController.m */; }; DEBAAE612A42175A00BF2C1C /* OSMessagingController.h in Headers */ = {isa = PBXBuildFile; fileRef = DEBAAE592A42175900BF2C1C /* OSMessagingController.h */; }; DEBAAE622A42175A00BF2C1C /* OSInAppMessageController.h in Headers */ = {isa = PBXBuildFile; fileRef = DEBAAE5A2A42175900BF2C1C /* OSInAppMessageController.h */; }; @@ -1860,6 +1866,12 @@ DEBAAE512A42174A00BF2C1C /* OSInAppMessageView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = OSInAppMessageView.h; sourceTree = ""; }; DEBAAE522A42174A00BF2C1C /* OSInAppMessageViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = OSInAppMessageViewController.h; sourceTree = ""; }; DEBAAE532A42174A00BF2C1C /* OSInAppMessageView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = OSInAppMessageView.m; sourceTree = ""; }; + 5C1A7E0D2FB0A00100AB0001 /* OSInAppMessageInsets.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = OSInAppMessageInsets.h; sourceTree = ""; }; + 5C1A7E0D2FB0A00100AB0002 /* OSInAppMessageInsets.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = OSInAppMessageInsets.m; sourceTree = ""; }; + 5C1A7E0D2FB0A00100AB0003 /* OSInAppMessageInsetsTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = OSInAppMessageInsetsTests.swift; sourceTree = ""; }; + 5C1A7E0E2FB0A00100AB0001 /* OSInAppMessageScroll.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = OSInAppMessageScroll.h; sourceTree = ""; }; + 5C1A7E0E2FB0A00100AB0002 /* OSInAppMessageScroll.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = OSInAppMessageScroll.m; sourceTree = ""; }; + 5C1A7E0E2FB0A00100AB0003 /* OSInAppMessageScrollTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = OSInAppMessageScrollTests.swift; sourceTree = ""; }; DEBAAE582A42175900BF2C1C /* OSDynamicTriggerController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = OSDynamicTriggerController.m; sourceTree = ""; }; DEBAAE592A42175900BF2C1C /* OSMessagingController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = OSMessagingController.h; sourceTree = ""; }; DEBAAE5A2A42175900BF2C1C /* OSInAppMessageController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = OSInAppMessageController.h; sourceTree = ""; }; @@ -2386,6 +2398,8 @@ 3CB35FCA2F0FA20B000E6E0F /* OSMessagingControllerUserStateTests.swift */, 3C7021E72ECF0CF3001768C6 /* OneSignalInAppMessagesTests-Bridging-Header.h */, 67ECA2928D863073B785F93F /* IamFetchIdentityVerificationTests.swift */, + 5C1A7E0D2FB0A00100AB0003 /* OSInAppMessageInsetsTests.swift */, + 5C1A7E0E2FB0A00100AB0003 /* OSInAppMessageScrollTests.swift */, ); path = OneSignalInAppMessagesTests; sourceTree = ""; @@ -3361,6 +3375,10 @@ DEBAAE532A42174A00BF2C1C /* OSInAppMessageView.m */, DEBAAE522A42174A00BF2C1C /* OSInAppMessageViewController.h */, DEBAAE502A42174A00BF2C1C /* OSInAppMessageViewController.m */, + 5C1A7E0D2FB0A00100AB0001 /* OSInAppMessageInsets.h */, + 5C1A7E0D2FB0A00100AB0002 /* OSInAppMessageInsets.m */, + 5C1A7E0E2FB0A00100AB0001 /* OSInAppMessageScroll.h */, + 5C1A7E0E2FB0A00100AB0002 /* OSInAppMessageScroll.m */, ); path = UI; sourceTree = ""; @@ -3692,6 +3710,8 @@ DEBAAE662A42175A00BF2C1C /* OSDynamicTriggerController.h in Headers */, DEBAAE642A42175A00BF2C1C /* OSTriggerController.h in Headers */, DEBAAE562A42174A00BF2C1C /* OSInAppMessageViewController.h in Headers */, + 5C1A7E0D2FB0A00100AB0004 /* OSInAppMessageInsets.h in Headers */, + 5C1A7E0E2FB0A00100AB0004 /* OSInAppMessageScroll.h in Headers */, DEBAAEB82A4381AE00BF2C1C /* OSInAppMessageMigrationController.h in Headers */, DEBAAE7E2A42176800BF2C1C /* OSInAppMessageDisplayStats.h in Headers */, DEBAAE882A42176800BF2C1C /* OSInAppMessageClickEvent.h in Headers */, @@ -4657,6 +4677,8 @@ 3C01519C2C2E29F90079E076 /* IAMRequestTests.m in Sources */, 3CB35FCB2F0FA20B000E6E0F /* OSMessagingControllerUserStateTests.swift in Sources */, 6F894909CB6D09258DCCA1C9 /* IamFetchIdentityVerificationTests.swift in Sources */, + 5C1A7E0D2FB0A00100AB0006 /* OSInAppMessageInsetsTests.swift in Sources */, + 5C1A7E0E2FB0A00100AB0006 /* OSInAppMessageScrollTests.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -5146,6 +5168,8 @@ DEBAAE8C2A42176800BF2C1C /* OSInAppMessagePushPrompt.m in Sources */, DEBAAE8E2A42176800BF2C1C /* OSInAppMessageClickResult.m in Sources */, DEBAAE572A42174A00BF2C1C /* OSInAppMessageView.m in Sources */, + 5C1A7E0D2FB0A00100AB0005 /* OSInAppMessageInsets.m in Sources */, + 5C1A7E0E2FB0A00100AB0005 /* OSInAppMessageScroll.m in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; diff --git a/iOS_SDK/OneSignalSDK/OneSignalInAppMessages/Controller/OSMessagingController.m b/iOS_SDK/OneSignalSDK/OneSignalInAppMessages/Controller/OSMessagingController.m index 704316abb9..52039366a5 100644 --- a/iOS_SDK/OneSignalSDK/OneSignalInAppMessages/Controller/OSMessagingController.m +++ b/iOS_SDK/OneSignalSDK/OneSignalInAppMessages/Controller/OSMessagingController.m @@ -1382,7 +1382,7 @@ - (void)webViewContentFinishedLoading:(OSInAppMessageInternal *)message { if (!self.window) { self.window = [[UIWindow alloc] init]; self.window.windowLevel = UIWindowLevelAlert; - self.window.frame = [[UIScreen mainScreen] bounds]; + self.window.frame = [UIApplication displayBounds]; } self.window.rootViewController = _viewController; diff --git a/iOS_SDK/OneSignalSDK/OneSignalInAppMessages/UI/OSInAppMessageInsets.h b/iOS_SDK/OneSignalSDK/OneSignalInAppMessages/UI/OSInAppMessageInsets.h new file mode 100644 index 0000000000..8a05140632 --- /dev/null +++ b/iOS_SDK/OneSignalSDK/OneSignalInAppMessages/UI/OSInAppMessageInsets.h @@ -0,0 +1,46 @@ +/** + * Modified MIT License + * + * Copyright 2026 OneSignal + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * 1. The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * 2. All copies of substantial portions of the Software may only be used in connection + * with services provided by OneSignal. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +#import + +NS_ASSUME_NONNULL_BEGIN + +@interface OSInAppMessageInsets : NSObject + +// Each side is the larger of the margin and its safe area inset. Region frames are in the scene +// view's coordinates; one sitting inside a side safe area, not flush with the edge, adds clearance. +// Both checks allow 1pt of slack: within 1pt of the screen edge counts as flush, and up to 1pt past +// the safe area edge still counts as inside. +// Full screen messages ignore the safe area and regions. ++ (UIEdgeInsets)horizontalInsetsWithMargin:(CGFloat)margin + safeArea:(UIEdgeInsets)safeArea + width:(CGFloat)width + regionFrames:(NSArray *)regionFrames + isFullscreen:(BOOL)isFullscreen; + +@end + +NS_ASSUME_NONNULL_END diff --git a/iOS_SDK/OneSignalSDK/OneSignalInAppMessages/UI/OSInAppMessageInsets.m b/iOS_SDK/OneSignalSDK/OneSignalInAppMessages/UI/OSInAppMessageInsets.m new file mode 100644 index 0000000000..1088849c96 --- /dev/null +++ b/iOS_SDK/OneSignalSDK/OneSignalInAppMessages/UI/OSInAppMessageInsets.m @@ -0,0 +1,55 @@ +/** + * Modified MIT License + * + * Copyright 2026 OneSignal + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * 1. The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * 2. All copies of substantial portions of the Software may only be used in connection + * with services provided by OneSignal. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +#import "OSInAppMessageInsets.h" + +@implementation OSInAppMessageInsets + ++ (UIEdgeInsets)horizontalInsetsWithMargin:(CGFloat)margin + safeArea:(UIEdgeInsets)safeArea + width:(CGFloat)width + regionFrames:(NSArray *)regionFrames + isFullscreen:(BOOL)isFullscreen { + if (isFullscreen) { + return UIEdgeInsetsMake(0, margin, 0, margin); + } + CGFloat left = MAX(margin, safeArea.left); + CGFloat right = MAX(margin, safeArea.right); + // Region frames already include UIKit's interactive margins; the message margin is added on top. + for (NSValue *value in regionFrames) { + CGRect frame = value.CGRectValue; + CGFloat minX = CGRectGetMinX(frame); + CGFloat maxX = CGRectGetMaxX(frame); + if (minX >= 1 && maxX <= safeArea.left + 1) { + left = MAX(left, maxX + margin); + } else if (maxX <= width - 1 && minX >= width - safeArea.right - 1) { + right = MAX(right, width - minX + margin); + } + } + return UIEdgeInsetsMake(0, left, 0, right); +} + +@end diff --git a/iOS_SDK/OneSignalSDK/OneSignalInAppMessages/UI/OSInAppMessageScroll.h b/iOS_SDK/OneSignalSDK/OneSignalInAppMessages/UI/OSInAppMessageScroll.h new file mode 100644 index 0000000000..667c36675f --- /dev/null +++ b/iOS_SDK/OneSignalSDK/OneSignalInAppMessages/UI/OSInAppMessageScroll.h @@ -0,0 +1,52 @@ +/** + * Modified MIT License + * + * Copyright 2026 OneSignal + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * 1. The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * 2. All copies of substantial portions of the Software may only be used in connection + * with services provided by OneSignal. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +#import + +NS_ASSUME_NONNULL_BEGIN + +@interface OSInAppMessageScroll : NSObject + +// Only content cut off by the screen height cap may scroll. Banners never scroll. Full screen +// messages report no height, so they are always treated as capped. ++ (BOOL)isHeightCappedWithMessageHeight:(CGFloat)messageHeight + availableHeight:(CGFloat)availableHeight + isBanner:(BOOL)isBanner + isFullscreen:(BOOL)isFullscreen; + +// Overflow under 1pt is treated as fitting, so rounding never makes a message scroll. ++ (BOOL)contentOverflowsWithContentHeight:(CGFloat)contentHeight visibleHeight:(CGFloat)visibleHeight; + +// A drag moves the message only when its content fits or is already at the edge it is dragged +// away from; otherwise the content scrolls. translationY is positive when dragging down. ++ (BOOL)shouldDragMessageWithContentOffsetY:(CGFloat)offsetY + contentHeight:(CGFloat)contentHeight + visibleHeight:(CGFloat)visibleHeight + translationY:(CGFloat)translationY; + +@end + +NS_ASSUME_NONNULL_END diff --git a/iOS_SDK/OneSignalSDK/OneSignalInAppMessages/UI/OSInAppMessageScroll.m b/iOS_SDK/OneSignalSDK/OneSignalInAppMessages/UI/OSInAppMessageScroll.m new file mode 100644 index 0000000000..e36d73e126 --- /dev/null +++ b/iOS_SDK/OneSignalSDK/OneSignalInAppMessages/UI/OSInAppMessageScroll.m @@ -0,0 +1,62 @@ +/** + * Modified MIT License + * + * Copyright 2026 OneSignal + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * 1. The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * 2. All copies of substantial portions of the Software may only be used in connection + * with services provided by OneSignal. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +#import "OSInAppMessageScroll.h" + +static const CGFloat kEdgeTolerance = 1.0; + +@implementation OSInAppMessageScroll + ++ (BOOL)isHeightCappedWithMessageHeight:(CGFloat)messageHeight + availableHeight:(CGFloat)availableHeight + isBanner:(BOOL)isBanner + isFullscreen:(BOOL)isFullscreen { + if (isBanner) return false; + if (isFullscreen) return true; + return messageHeight - availableHeight > kEdgeTolerance; +} + ++ (BOOL)contentOverflowsWithContentHeight:(CGFloat)contentHeight visibleHeight:(CGFloat)visibleHeight { + return contentHeight - visibleHeight > kEdgeTolerance; +} + ++ (BOOL)shouldDragMessageWithContentOffsetY:(CGFloat)offsetY + contentHeight:(CGFloat)contentHeight + visibleHeight:(CGFloat)visibleHeight + translationY:(CGFloat)translationY { + if (![self contentOverflowsWithContentHeight:contentHeight visibleHeight:visibleHeight]) { + return true; + } + if (translationY > 0) { + return offsetY <= kEdgeTolerance; + } + if (translationY < 0) { + return offsetY >= contentHeight - visibleHeight - kEdgeTolerance; + } + return false; +} + +@end diff --git a/iOS_SDK/OneSignalSDK/OneSignalInAppMessages/UI/OSInAppMessageView.h b/iOS_SDK/OneSignalSDK/OneSignalInAppMessages/UI/OSInAppMessageView.h index e8d3aa0b71..d8cad54225 100644 --- a/iOS_SDK/OneSignalSDK/OneSignalInAppMessages/UI/OSInAppMessageView.h +++ b/iOS_SDK/OneSignalSDK/OneSignalInAppMessages/UI/OSInAppMessageView.h @@ -41,15 +41,21 @@ NS_ASSUME_NONNULL_BEGIN @interface OSInAppMessageView : UIView @property (weak, nonatomic, nullable) id delegate; +// Left & right space around the message, so content is laid out at the width it is displayed at +@property (nonatomic) UIEdgeInsets horizontalInsets; - (instancetype _Nonnull)initWithMessage:(OSInAppMessageInternal *)inAppMessage withScriptMessageHandler:(id)messageHandler; -- (void)resetWebViewToMaxBoundsAndResizeHeight:(void (^) (NSNumber *newHeight)) completion; -- (void)updateSafeAreaInsets; +- (void)resetWebViewToMaxBoundsAndResizeHeight:(void (^) (NSNumber * _Nullable newHeight)) completion; +- (void)updateSafeAreaInsets:(UIEdgeInsets)insets; - (void)setupWebViewConstraints; - (void)loadReplacementURL:(NSURL *)url; - (void)loadedHtmlContent:(NSString *)html withBaseURL:(NSURL *)url; - (void)removeScriptMessageHandler; - (void)setIsFullscreen:(BOOL)isFullscreen; +- (BOOL)shouldDragMessageForTranslationY:(CGFloat)translationY; +@property (nonatomic, readonly) UIPanGestureRecognizer *contentPanGestureRecognizer; +// Content may scroll only while this is set, so leftover document height never makes a fitting message scroll +@property (nonatomic) BOOL contentScrollAllowed; @end NS_ASSUME_NONNULL_END diff --git a/iOS_SDK/OneSignalSDK/OneSignalInAppMessages/UI/OSInAppMessageView.m b/iOS_SDK/OneSignalSDK/OneSignalInAppMessages/UI/OSInAppMessageView.m index aef2b358bf..3929b5b43b 100644 --- a/iOS_SDK/OneSignalSDK/OneSignalInAppMessages/UI/OSInAppMessageView.m +++ b/iOS_SDK/OneSignalSDK/OneSignalInAppMessages/UI/OSInAppMessageView.m @@ -30,6 +30,10 @@ #import "OSInAppMessageClickResult.h" #import #import "OSMacros.h" +#import "UIApplication+OneSignal.h" +#import "OSInAppMessageScroll.h" + +static void *OSInAppMessageContentSizeContext = &OSInAppMessageContentSizeContext; @interface OSInAppMessageView () @@ -101,7 +105,7 @@ - (void)setupWebviewWithMessageHandler:(id)handler { // WebView should use mainBounds as frame since we need to make sure it spans full possible screen size // to prevent text wrapping while obtaining true height of message from JS - CGRect mainBounds = UIScreen.mainScreen.bounds; + CGRect mainBounds = [UIApplication displayBounds]; mainBounds.size.width -= (2.0 * marginSpacing); // Setup WebView, delegates, and disable scrolling inside of the WebView @@ -121,22 +125,74 @@ - (void)setupWebviewWithMessageHandler:(id)handler { } } } + self.webView.scrollView.scrollEnabled = false; + self.webView.scrollView.bounces = false; + // The HTML draws its own card inside the web view, so a native indicator would not line up with it + self.webView.scrollView.showsVerticalScrollIndicator = false; + [self.webView.scrollView addObserver:self forKeyPath:@"contentSize" options:0 context:OSInAppMessageContentSizeContext]; [self addSubview:self.webView]; [self layoutIfNeeded]; } +- (void)dealloc { + [_webView.scrollView removeObserver:self forKeyPath:@"contentSize" context:OSInAppMessageContentSizeContext]; +} + +- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context { + if (context != OSInAppMessageContentSizeContext) { + [super observeValueForKeyPath:keyPath ofObject:object change:change context:context]; + return; + } + [self updateScrollEnabled]; +} + +- (void)layoutSubviews { + [super layoutSubviews]; + [self updateScrollEnabled]; +} + +- (void)setContentScrollAllowed:(BOOL)contentScrollAllowed { + _contentScrollAllowed = contentScrollAllowed; + [self updateScrollEnabled]; +} + +// Scrolling stays off while the content fits, so messages that fit keep today's touch handling. +- (void)updateScrollEnabled { + UIScrollView *scrollView = self.webView.scrollView; + BOOL overflows = self.contentScrollAllowed && + [OSInAppMessageScroll contentOverflowsWithContentHeight:scrollView.contentSize.height + visibleHeight:scrollView.bounds.size.height]; + if (scrollView.scrollEnabled == overflows) return; + scrollView.scrollEnabled = overflows; + if (!overflows) { + scrollView.contentOffset = CGPointZero; + } +} + +- (BOOL)shouldDragMessageForTranslationY:(CGFloat)translationY { + UIScrollView *scrollView = self.webView.scrollView; + if (!scrollView.scrollEnabled) { + return true; + } + return [OSInAppMessageScroll shouldDragMessageWithContentOffsetY:scrollView.contentOffset.y + contentHeight:scrollView.contentSize.height + visibleHeight:scrollView.bounds.size.height + translationY:translationY]; +} + +- (UIPanGestureRecognizer *)contentPanGestureRecognizer { + return self.webView.scrollView.panGestureRecognizer; +} + - (void)setIsFullscreen:(BOOL)isFullscreen { _isFullscreen = isFullscreen; [self setWebviewFrame]; } - (void)setWebviewFrame { - CGRect mainBounds = UIScreen.mainScreen.bounds; - if (!self.isFullscreen) { - CGFloat marginSpacing = [OneSignalCoreHelper sizeToScale:MESSAGE_MARGIN]; - mainBounds.size.width -= (2.0 * marginSpacing); - } + CGRect mainBounds = [UIApplication displayBounds]; + mainBounds.size.width -= self.horizontalInsets.left + self.horizontalInsets.right; [self.webView setFrame:mainBounds]; } @@ -145,7 +201,7 @@ - (void)setWebviewFrame { WebView will have margins accounted for on width, but height just needs to be phone height or larger The issue is that text wrapping can cause incorrect height issues so width is the real concern here */ -- (void)resetWebViewToMaxBoundsAndResizeHeight:(void (^) (NSNumber *newHeight)) completion { +- (void)resetWebViewToMaxBoundsAndResizeHeight:(void (^) (NSNumber * _Nullable newHeight)) completion { [self.webView removeConstraints:[self.webView constraints]]; @@ -157,6 +213,9 @@ - (void)resetWebViewToMaxBoundsAndResizeHeight:(void (^) (NSNumber *newHeight)) if (error) { NSString *errorMessage = [NSString stringWithFormat:@"Javascript Method: %@ Evaluated with Error: %@", OS_JS_GET_PAGE_META_DATA_METHOD, error]; [OneSignalLog onesignalLog:ONE_S_LL_ERROR message:errorMessage]; + // Still complete so a rotation or fold re-shows the message at its previous height + [self setupWebViewConstraints]; + completion(nil); return; } NSString *successMessage = [NSString stringWithFormat:@"Javascript Method: %@ Evaluated with Success: %@", OS_JS_GET_PAGE_META_DATA_METHOD, result]; @@ -170,13 +229,8 @@ - (void)resetWebViewToMaxBoundsAndResizeHeight:(void (^) (NSNumber *newHeight)) }]; } -- (void)updateSafeAreaInsets { - UIWindow *keyWindow = UIApplication.sharedApplication.keyWindow; - CGFloat top = keyWindow.safeAreaInsets.top; - CGFloat bottom = keyWindow.safeAreaInsets.bottom; - CGFloat right = keyWindow.safeAreaInsets.right; - CGFloat left = keyWindow.safeAreaInsets.left; - NSString *safeAreaInsetsObjectString = [NSString stringWithFormat:OS_JS_SAFE_AREA_INSETS_OBJ,top, bottom, right, left]; +- (void)updateSafeAreaInsets:(UIEdgeInsets)insets { + NSString *safeAreaInsetsObjectString = [NSString stringWithFormat:OS_JS_SAFE_AREA_INSETS_OBJ, insets.top, insets.bottom, insets.right, insets.left]; NSString *setInsetsString = [NSString stringWithFormat:OS_SET_SAFE_AREA_INSETS_METHOD, safeAreaInsetsObjectString]; [self.webView evaluateJavaScript:setInsetsString completionHandler:^(NSDictionary *result, NSError * _Nullable error) { @@ -203,7 +257,6 @@ - (void)setupWebViewConstraints { self.webView.UIDelegate = self; self.webView.navigationDelegate = self; self.webView.scrollView.delegate = self; - self.webView.scrollView.scrollEnabled = false; self.webView.layer.cornerRadius = 10.0f; self.webView.layer.masksToBounds = true; @@ -216,6 +269,7 @@ - (void)setupWebViewConstraints { [self.webView.bottomAnchor constraintEqualToAnchor:self.bottomAnchor].active = true; [self layoutIfNeeded]; + [self updateScrollEnabled]; } /* @@ -238,6 +292,13 @@ - (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigat self.loaded = true; } +- (void)scrollViewDidScroll:(UIScrollView *)scrollView { + // Only vertical overflow turns scrolling on; a slightly wide document must not drift sideways + if (scrollView.contentOffset.x != 0) { + scrollView.contentOffset = CGPointMake(0, scrollView.contentOffset.y); + } +} + - (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView { return nil; } diff --git a/iOS_SDK/OneSignalSDK/OneSignalInAppMessages/UI/OSInAppMessageViewController.m b/iOS_SDK/OneSignalSDK/OneSignalInAppMessages/UI/OSInAppMessageViewController.m index 873e7447d6..ce7a2196b5 100644 --- a/iOS_SDK/OneSignalSDK/OneSignalInAppMessages/UI/OSInAppMessageViewController.m +++ b/iOS_SDK/OneSignalSDK/OneSignalInAppMessages/UI/OSInAppMessageViewController.m @@ -27,18 +27,33 @@ #import "OSInAppMessageViewController.h" #import "OSInAppMessageView.h" +#import "OSInAppMessageInsets.h" +#import "OSInAppMessageScroll.h" #import "OSInAppMessageController.h" #import "OSInAppMessageBridgeEvent.h" #import #import "OSMacros.h" #import "OSSessionManager.h" +#import "UIApplication+OneSignal.h" + +// iOS 27.1 reserved region API, declared here because release binaries are built with an older SDK. +// Remove these and call the UIKit API directly once releases build with the iOS 27.1 SDK. +@protocol OSReservedRegionKindClass +- (id)occlusionRegionKind; +@end +@protocol OSReservedRegionQuerying +- (NSArray *)reservedRegionsOfKind:(id)kind; +@end +@protocol OSReservedRegion +@property (nonatomic, readonly) CGRect frame; +@end #define HIGHEST_CONSTRAINT_PRIORITY 999.0f #define HIGH_CONSTRAINT_PRIORITY 990.0f #define MEDIUM_CONSTRAINT_PRIORITY 950.0f #define LOW_CONSTRAINT_PRIORITY 900.0f -@interface OSInAppMessageViewController () +@interface OSInAppMessageViewController () // The actual message subview @property (nonatomic, nullable) OSInAppMessageView *messageView; @@ -53,9 +68,20 @@ @interface OSInAppMessageViewController () // This constraint enforces an aspect ratio for the given message type (ie. banner) @property (strong, nonatomic, nonnull) NSLayoutConstraint *heightConstraint; +// These pin the message view to the left & right, updated as the safe area changes +@property (strong, nonatomic, nullable) NSLayoutConstraint *leftConstraint; +@property (strong, nonatomic, nullable) NSLayoutConstraint *rightConstraint; + +// Area the message is laid out in vertically, inset by the larger of the margin and the safe area +@property (strong, nonatomic, nullable) UILayoutGuide *verticalGuide; +@property (strong, nonatomic, nullable) NSLayoutConstraint *verticalGuideTopConstraint; +@property (strong, nonatomic, nullable) NSLayoutConstraint *verticalGuideBottomConstraint; + // This recognizer lets the user pan (swipe) the message up and down @property (weak, nonatomic, nullable) UIPanGestureRecognizer *panGestureRecognizer; +@property (nonatomic) BOOL didWarnSceneViewFallback; + // This tap recognizer lets us dismiss the message if the user taps the background @property (weak, nonatomic, nullable) UITapGestureRecognizer *tapGestureRecognizer; @@ -136,6 +162,26 @@ - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; } +- (void)viewDidAppear:(BOOL)animated { + [super viewDidAppear:animated]; + // Resizable scenes (e.g. an unfolded display) don't re-query prefersStatusBarHidden when this window becomes key + if (self.isFullscreen) { + [self setNeedsStatusBarAppearanceUpdate]; + // The insets may have settled before this view had a window, so no change callback follows + if (self.didPageRenderingComplete) { + [self updateContentInsets]; + } + } +} + +// The HTML was given the app's insets, and a page resize doesn't always follow to correct them +- (void)viewSafeAreaInsetsDidChange { + [super viewSafeAreaInsetsDidChange]; + if (self.isFullscreen && self.didPageRenderingComplete) { + [self updateContentInsets]; + } +} + - (void)viewWillDisappear:(BOOL)animated { [super viewWillDisappear:animated]; @@ -288,20 +334,18 @@ - (void)parseContentData:(NSDictionary *)data { if (self.isFullscreen) { self.pendingHTMLContent = [self setContentInsetsInHTML:self.pendingHTMLContent]; } + self.messageView.horizontalInsets = [self horizontalInsets]; [self.messageView setIsFullscreen:self.isFullscreen]; } - (NSString *)setContentInsetsInHTML:(NSString *)html { NSMutableString *newHTML = [[NSMutableString alloc] initWithString:html]; - UIWindow *keyWindow = UIApplication.sharedApplication.keyWindow; - if (!keyWindow) { + UIWindow *window = [self contentInsetsWindow]; + if (!window) { return newHTML; } - CGFloat top = keyWindow.safeAreaInsets.top; - CGFloat bottom = keyWindow.safeAreaInsets.bottom; - CGFloat right = keyWindow.safeAreaInsets.right; - CGFloat left = keyWindow.safeAreaInsets.left; - NSString *safeAreaInsetsObjectString = [NSString stringWithFormat:OS_JS_SAFE_AREA_INSETS_OBJ,top, bottom, right, left]; + UIEdgeInsets insets = window.safeAreaInsets; + NSString *safeAreaInsetsObjectString = [NSString stringWithFormat:OS_JS_SAFE_AREA_INSETS_OBJ, insets.top, insets.bottom, insets.right, insets.left]; NSString *insetsString = [NSString stringWithFormat:@"\n\n\