From a73dcb1a24a101931505e78714bcce7002fd7169 Mon Sep 17 00:00:00 2001 From: emawby Date: Thu, 23 Sep 2021 12:08:17 -0700 Subject: [PATCH 1/8] Manually controlling supportedInterfaceOrientations for IAMs --- .../Source/OSInAppMessageViewController.m | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/iOS_SDK/OneSignalSDK/Source/OSInAppMessageViewController.m b/iOS_SDK/OneSignalSDK/Source/OSInAppMessageViewController.m index f1af98199..3d13ede28 100644 --- a/iOS_SDK/OneSignalSDK/Source/OSInAppMessageViewController.m +++ b/iOS_SDK/OneSignalSDK/Source/OSInAppMessageViewController.m @@ -722,6 +722,40 @@ - (void)jsEventOccurredWithBody:(NSData *)body { } } +/* + Unity overrides orientation behavior and enables all orientations in supportedInterfaceOrientations, regardless of + the values set in the info.plist. It then uses its own internal logic for restricting the Application's views to + the selected orientations. This view controller inherits the behavior of all orientations being allowed so we need + to manually set the supported orientations based on the values in the plist. + If no values are selected for the orientation key in the plist then we will default to super's behavior. +*/ +- (UIInterfaceOrientationMask)supportedInterfaceOrientations { + NSUInteger orientationMask = 0; + NSArray *supportedOrientations = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"UISupportedInterfaceOrientations"]; + if (!supportedOrientations) { + return [super supportedInterfaceOrientations]; + } + + if ([supportedOrientations containsObject:@"UIInterfaceOrientationLandscapeLeft"]) { + orientationMask += UIInterfaceOrientationMaskLandscapeLeft; + } + + if ([supportedOrientations containsObject:@"UIInterfaceOrientationLandscapeRight"]) { + orientationMask += UIInterfaceOrientationMaskLandscapeRight; + } + + if ([supportedOrientations containsObject:@"UIInterfaceOrientationPortrait"]) { + orientationMask += UIInterfaceOrientationMaskPortrait; + } + + if ([supportedOrientations containsObject:@"UIInterfaceOrientationPortraitUpsideDown"]) { + orientationMask += UIInterfaceOrientationMaskPortraitUpsideDown; + } + + return orientationMask; + +} + /* Override method for handling orientation change within a view controller on iOS 8 or higher This specifically handles the resizing and reanimation of a currently showing IAM From 83b1986d2ebfa18f1291310c7b3981186e1880d7 Mon Sep 17 00:00:00 2001 From: emawby Date: Tue, 12 Oct 2021 11:34:02 -0700 Subject: [PATCH 2/8] use isFullscreen boolean to decide on safe area usage --- .../OneSignalDevApp/AppDelegate.m | 4 +- .../OneSignalSDK/Source/OSInAppMessageView.m | 12 +-- .../Source/OSInAppMessageViewController.m | 95 +++++++++++++------ .../Source/OneSignalCommonDefines.h | 4 +- 4 files changed, 78 insertions(+), 37 deletions(-) diff --git a/iOS_SDK/OneSignalDevApp/OneSignalDevApp/AppDelegate.m b/iOS_SDK/OneSignalDevApp/OneSignalDevApp/AppDelegate.m index 7067818d8..5ea16c21a 100644 --- a/iOS_SDK/OneSignalDevApp/OneSignalDevApp/AppDelegate.m +++ b/iOS_SDK/OneSignalDevApp/OneSignalDevApp/AppDelegate.m @@ -85,7 +85,7 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:( [OneSignal addSubscriptionObserver:self]; [OneSignal addEmailSubscriptionObserver:self]; [OneSignal setInAppMessageLifecycleHandler:self]; - [OneSignal pauseInAppMessages:true]; + [OneSignal pauseInAppMessages:false]; [OneSignal setNotificationWillShowInForegroundHandler:notificationReceiverBlock]; [OneSignal setNotificationOpenedHandler:openNotificationHandler]; @@ -95,7 +95,7 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:( return YES; } -#define ONESIGNAL_APP_ID_DEFAULT @"0ba9731b-33bd-43f4-8b59-61172e27447d" +#define ONESIGNAL_APP_ID_DEFAULT @"9ddf83fd-6cf8-403d-98a1-bd5285b23d0e" #define ONESIGNAL_APP_ID_KEY_FOR_TESTING @"ONESIGNAL_APP_ID_KEY_FOR_TESTING" + (NSString*)getOneSignalAppId { diff --git a/iOS_SDK/OneSignalSDK/Source/OSInAppMessageView.m b/iOS_SDK/OneSignalSDK/Source/OSInAppMessageView.m index 2f756c669..24e87982c 100644 --- a/iOS_SDK/OneSignalSDK/Source/OSInAppMessageView.m +++ b/iOS_SDK/OneSignalSDK/Source/OSInAppMessageView.m @@ -90,12 +90,12 @@ - (NSString *)addTagsToHTML:(NSString *)html { - (void)loadedHtmlContent:(NSString *)html withBaseURL:(NSURL *)url { // UI Update must be done on the main thread NSLog(@"11111 [self.webView loadHTMLString:html baseURL:url];"); - dispatch_sync(dispatch_get_main_queue(), ^{ - NSLog(@"222222 [self.webView loadHTMLString:html baseURL:url];"); - NSString *taggedHTML = [self addTagsToHTML:html]; - [OneSignal onesignal_Log:ONE_S_LL_VERBOSE message:[NSString stringWithFormat:@"loadedHtmlContent with Tags: \n%@", taggedHTML]]; - [self.webView loadHTMLString:taggedHTML baseURL:url]; - }); + + NSLog(@"222222 [self.webView loadHTMLString:html baseURL:url];"); + NSString *taggedHTML = [self addTagsToHTML:html]; + [OneSignal onesignal_Log:ONE_S_LL_VERBOSE message:[NSString stringWithFormat:@"loadedHtmlContent with Tags: \n%@", taggedHTML]]; + [self.webView loadHTMLString:taggedHTML baseURL:url]; + } - (void)setupWebviewWithMessageHandler:(id)handler { diff --git a/iOS_SDK/OneSignalSDK/Source/OSInAppMessageViewController.m b/iOS_SDK/OneSignalSDK/Source/OSInAppMessageViewController.m index 3d13ede28..6de8dc7ae 100644 --- a/iOS_SDK/OneSignalSDK/Source/OSInAppMessageViewController.m +++ b/iOS_SDK/OneSignalSDK/Source/OSInAppMessageViewController.m @@ -97,6 +97,8 @@ @interface OSInAppMessageViewController () @property (nonatomic) BOOL useWidthMargin; +@property (nonatomic) BOOL isFullscreen; + @end @implementation OSInAppMessageViewController @@ -225,25 +227,27 @@ - (void)maxDisplayTimeTimerFinished { - (OSResultSuccessBlock)messageContentOnSuccess { return ^(NSDictionary *data) { - if (!data) { - [self encounteredErrorLoadingMessageContent:nil]; - return; - } - - let message = [NSString stringWithFormat:@"In App Messaging htmlContent.html: %@", data[@"html"]]; - [OneSignal onesignal_Log:ONE_S_LL_VERBOSE message:message]; - - if (!self.message.isPreview) - [[OneSignal sessionManager] onInAppMessageReceived:self.message.messageId]; + [OneSignalHelper dispatch_async_on_main_queue:^{ + if (!data) { + [self encounteredErrorLoadingMessageContent:nil]; + return; + } + + let message = [NSString stringWithFormat:@"In App Messaging htmlContent.html: %@", data[@"html"]]; + [OneSignal onesignal_Log:ONE_S_LL_VERBOSE message:message]; + + if (!self.message.isPreview) + [[OneSignal sessionManager] onInAppMessageReceived:self.message.messageId]; - let baseUrl = [NSURL URLWithString:OS_IAM_WEBVIEW_BASE_URL]; - [self parseContentData:data]; - if (self.waitForTags) { - return; - } - [self.delegate messageWillDisplay:self.message]; - [self.messageView loadedHtmlContent:self.pendingHTMLContent withBaseURL:baseUrl]; - self.pendingHTMLContent = nil; + let baseUrl = [NSURL URLWithString:OS_IAM_WEBVIEW_BASE_URL]; + [self parseContentData:data]; + if (self.waitForTags) { + return; + } + [self.delegate messageWillDisplay:self.message]; + [self.messageView loadedHtmlContent:self.pendingHTMLContent withBaseURL:baseUrl]; + self.pendingHTMLContent = nil; + }]; }; } @@ -261,6 +265,34 @@ - (void)parseContentData:(NSDictionary *)data { self.useWidthMargin = ![styles[@"remove_width_margin"] boolValue]; } } + self.isFullscreen = !self.useHeightMargin; + if (self.isFullscreen) { + self.pendingHTMLContent = [self setContentInsetsInHTML:self.pendingHTMLContent]; + } +} + +- (NSString *)setContentInsetsInHTML:(NSString *)html { + NSMutableString *newHTML = [[NSMutableString alloc] initWithString:html]; + if (@available(iOS 11, *)) { + 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:@"{\n\ + top: %f,\n\ + bottom: %f,\n\ + right: %f,\n\ + left: %f,\n\ + }",top, bottom, right, left]; + //Script to set the tags for liquid tag substitution + NSString *insetsString = [NSString stringWithFormat:@"\n\n\ + ",safeAreaInsetsObjectString]; + [newHTML appendString: insetsString]; + } + return newHTML; } - (void)setWaitForTags:(BOOL)waitForTags { @@ -318,13 +350,15 @@ - (void)addConstraintsForMessage { // as a notch or a rounded corner on newer iOS devices like iPhone X // Note that Safe Area layout guides were only introduced in iOS 11 if (@available(iOS 11, *)) { - let safeArea = self.view.safeAreaLayoutGuide; - top = safeArea.topAnchor; - bottom = safeArea.bottomAnchor; - leading = safeArea.leadingAnchor; - trailing = safeArea.trailingAnchor; - center = safeArea.centerXAnchor; - height = safeArea.heightAnchor; + if (!self.isFullscreen) { + let safeArea = self.view.safeAreaLayoutGuide; + top = safeArea.topAnchor; + bottom = safeArea.bottomAnchor; + leading = safeArea.leadingAnchor; + trailing = safeArea.trailingAnchor; + center = safeArea.centerXAnchor; + height = safeArea.heightAnchor; + } } CGRect mainBounds = [OneSignalViewHelper getScreenBounds]; @@ -400,8 +434,10 @@ - (void)addConstraintsForMessage { self.view.window.frame = mainBounds; NSLayoutAnchor *centerYanchor = self.view.centerYAnchor; if (@available(iOS 11, *)) { - let safeArea = self.view.safeAreaLayoutGuide; - centerYanchor = safeArea.centerYAnchor; + if (!self.isFullscreen) { + let safeArea = self.view.safeAreaLayoutGuide; + centerYanchor = safeArea.centerYAnchor; + } } self.initialYConstraint = [self.messageView.centerYAnchor constraintEqualToAnchor:centerYanchor constant:0.0f]; @@ -670,6 +706,10 @@ - (BOOL)shouldDismissMessageWithPanGestureOffset:(double)offset withVelocity:(do } } +- (void)updateSafeAreaInsets { + +} + /* This delegate function gets called when in-app html is load or action button is tapped */ @@ -701,6 +741,7 @@ - (void)jsEventOccurredWithBody:(NSData *)body { case OSInAppMessageBridgeEventTypePageResize: { // Unused resize event for IAM during actions like orientation changes and displaying an IAM // self.message.height = event.resize.height; + [self updateSafeAreaInsets]; break; } case OSInAppMessageBridgeEventTypeActionTaken: { diff --git a/iOS_SDK/OneSignalSDK/Source/OneSignalCommonDefines.h b/iOS_SDK/OneSignalSDK/Source/OneSignalCommonDefines.h index 6ac5d9737..a82942839 100644 --- a/iOS_SDK/OneSignalSDK/Source/OneSignalCommonDefines.h +++ b/iOS_SDK/OneSignalSDK/Source/OneSignalCommonDefines.h @@ -33,8 +33,8 @@ // Networking #define OS_API_VERSION @"1" #define OS_API_ACCEPT_HEADER @"application/vnd.onesignal.v" OS_API_VERSION @"+json" -#define OS_API_SERVER_URL @"https://api.onesignal.com/" -#define OS_IAM_WEBVIEW_BASE_URL @"https://onesignal.com/" +#define OS_API_SERVER_URL @"https://api.staging-01.onesignal.com/" +#define OS_IAM_WEBVIEW_BASE_URL @"https://staging.onesignal.com/" // OneSignalUserDefault keys // String values start with "OSUD_" to maintain a level of uniqueness from other libs and app code From a0e0bf1fdf0bc42b40a0efe2fc72b7ff4b68e98c Mon Sep 17 00:00:00 2001 From: emawby Date: Wed, 20 Oct 2021 14:01:46 -0700 Subject: [PATCH 3/8] calling setSafeAreaInsets in JS after resize --- .../OneSignalSDK/Source/OSInAppMessageView.h | 1 + .../OneSignalSDK/Source/OSInAppMessageView.m | 22 +++++++++++++++++++ .../Source/OSInAppMessageViewController.m | 14 ++---------- .../Source/OSInAppMessagingDefines.h | 8 +++++++ 4 files changed, 33 insertions(+), 12 deletions(-) diff --git a/iOS_SDK/OneSignalSDK/Source/OSInAppMessageView.h b/iOS_SDK/OneSignalSDK/Source/OSInAppMessageView.h index 907ccd944..317bb802f 100644 --- a/iOS_SDK/OneSignalSDK/Source/OSInAppMessageView.h +++ b/iOS_SDK/OneSignalSDK/Source/OSInAppMessageView.h @@ -44,6 +44,7 @@ NS_ASSUME_NONNULL_BEGIN - (instancetype _Nonnull)initWithMessage:(OSInAppMessageInternal *)inAppMessage withScriptMessageHandler:(id)messageHandler; - (void)resetWebViewToMaxBoundsAndResizeHeight:(void (^) (NSNumber *newHeight)) completion; +- (void)updateSafeAreaInsets; - (void)setupWebViewConstraints; - (void)loadReplacementURL:(NSURL *)url; - (void)loadedHtmlContent:(NSString *)html withBaseURL:(NSURL *)url; diff --git a/iOS_SDK/OneSignalSDK/Source/OSInAppMessageView.m b/iOS_SDK/OneSignalSDK/Source/OSInAppMessageView.m index 24e87982c..94c845a4a 100644 --- a/iOS_SDK/OneSignalSDK/Source/OSInAppMessageView.m +++ b/iOS_SDK/OneSignalSDK/Source/OSInAppMessageView.m @@ -151,6 +151,28 @@ - (void)resetWebViewToMaxBoundsAndResizeHeight:(void (^) (NSNumber *newHeight)) }]; } +- (void)updateSafeAreaInsets { + if (@available(iOS 11, *)) { + 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]; + + NSString *setInsetsString = [NSString stringWithFormat:OS_SET_SAFE_AREA_INSETS_METHOD, safeAreaInsetsObjectString]; + [self.webView evaluateJavaScript:setInsetsString completionHandler:^(NSDictionary *result, NSError * _Nullable error) { + if (error) { + NSString *errorMessage = [NSString stringWithFormat:@"Javascript Method: %@ Evaluated with Error: %@", OS_SET_SAFE_AREA_INSETS_METHOD, error]; + [OneSignal onesignal_Log:ONE_S_LL_ERROR message:errorMessage]; + return; + } + NSString *successMessage = [NSString stringWithFormat:@"Javascript Method: %@ Evaluated with Success: %@", OS_SET_SAFE_AREA_INSETS_METHOD, result]; + [OneSignal onesignal_Log:ONE_S_LL_VERBOSE message:successMessage]; + }]; + } +} + - (NSNumber *)extractHeightFromMetaDataPayload:(NSDictionary *)result { return @([result[@"rect"][@"height"] intValue]); } diff --git a/iOS_SDK/OneSignalSDK/Source/OSInAppMessageViewController.m b/iOS_SDK/OneSignalSDK/Source/OSInAppMessageViewController.m index 6de8dc7ae..431df383f 100644 --- a/iOS_SDK/OneSignalSDK/Source/OSInAppMessageViewController.m +++ b/iOS_SDK/OneSignalSDK/Source/OSInAppMessageViewController.m @@ -279,13 +279,7 @@ - (NSString *)setContentInsetsInHTML:(NSString *)html { CGFloat bottom = keyWindow.safeAreaInsets.bottom; CGFloat right = keyWindow.safeAreaInsets.right; CGFloat left = keyWindow.safeAreaInsets.left; - NSString *safeAreaInsetsObjectString = [NSString stringWithFormat:@"{\n\ - top: %f,\n\ - bottom: %f,\n\ - right: %f,\n\ - left: %f,\n\ - }",top, bottom, right, left]; - //Script to set the tags for liquid tag substitution + NSString *safeAreaInsetsObjectString = [NSString stringWithFormat:OS_JS_SAFE_AREA_INSETS_OBJ,top, bottom, right, left]; NSString *insetsString = [NSString stringWithFormat:@"\n\n\