Skip to content

Commit ff9592e

Browse files
authored
Fixes an issue with the in-app webview during device rotation (#317)
* When a user received a push notification with launchUrl specified, and the app displayed the webview, this webview was being manually added as a subview but had no layout constraints. This meant that when the device orientation changed the webview didn't change its frame to match. * App-store launch URL's from push notifications will no longer open an (empty) in-app web view. Instead the app will directly open the App Store link without a blank web view.
1 parent 2e5d176 commit ff9592e

File tree

2 files changed

+28
-6
lines changed

2 files changed

+28
-6
lines changed

iOS_SDK/OneSignalSDK/Source/OneSignalHelper.m

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -838,6 +838,12 @@ + (void) displayWebView:(NSURL*)url {
838838

839839
inAppLaunch = [[[NSUserDefaults standardUserDefaults] objectForKey:@"ONESIGNAL_INAPP_LAUNCH_URL"] boolValue];
840840

841+
// If the URL contains itunes.apple.com, it's an app store link
842+
// that should be opened using sharedApplication openURL
843+
if ([[url absoluteString] containsString:@"itunes.apple.com"]) {
844+
inAppLaunch = NO;
845+
}
846+
841847
if (inAppLaunch && [self isWWWScheme:url]) {
842848
if (!webVC)
843849
webVC = [[OneSignalWebView alloc] init];

iOS_SDK/OneSignalSDK/Source/OneSignalWebView.m

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#import <UIKit/UIKit.h>
2929
#import "OneSignalWebView.h"
3030
#import "OneSignal.h"
31+
#import "OneSignalHelper.h"
3132

3233
@interface OneSignal ()
3334
+ (void) onesignal_Log:(ONE_S_LOG_LEVEL)logLevel message:(NSString*) message;
@@ -40,12 +41,12 @@ @implementation OneSignalWebView
4041

4142
-(void)viewDidLoad {
4243
[super viewDidLoad];
43-
44-
_webView = [[UIWebView alloc] initWithFrame:self.view.frame];
44+
45+
_webView = [UIWebView new];
4546
_webView.delegate = self;
4647
[self.view addSubview:_webView];
4748

48-
[self.view setBackgroundColor:[UIColor blackColor]];
49+
[self pinSubviewToMarginsWithSubview:_webView withSuperview:self.view];
4950

5051
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(dismiss:)];
5152

@@ -68,7 +69,7 @@ -(void)dismiss:(id)sender {
6869
//clear web view
6970
[_webView loadHTMLString:@"" baseURL:nil];
7071
if (viewControllerForPresentation)
71-
[viewControllerForPresentation.view removeFromSuperview];
72+
[viewControllerForPresentation.view removeFromSuperview];
7273
}];
7374
}
7475

@@ -86,6 +87,19 @@ -(void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {
8687
[OneSignal onesignal_Log:ONE_S_LL_ERROR message:error.localizedDescription];
8788
}
8889

90+
-(void)pinSubviewToMarginsWithSubview:(UIView *)subview withSuperview:(UIView *)superview {
91+
subview.translatesAutoresizingMaskIntoConstraints = false;
92+
93+
let attributes = @[@(NSLayoutAttributeTop), @(NSLayoutAttributeBottom), @(NSLayoutAttributeLeading), @(NSLayoutAttributeTrailing)];
94+
95+
for (NSNumber *layoutAttribute in attributes) {
96+
let attribute = (NSLayoutAttribute)[layoutAttribute longValue];
97+
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:subview attribute:attribute relatedBy:NSLayoutRelationEqual toItem:superview attribute:attribute multiplier:1.0 constant:0.0]];
98+
}
99+
100+
[superview layoutIfNeeded];
101+
}
102+
89103

90104
-(void)showInApp {
91105
// if already presented, no need to present again
@@ -110,13 +124,15 @@ -(void)showInApp {
110124

111125
if (!viewControllerForPresentation.view.superview)
112126
[mainWindow addSubview:[viewControllerForPresentation view]];
113-
127+
114128
@try {
115-
[viewControllerForPresentation presentViewController:navController animated:YES completion:nil];
129+
[viewControllerForPresentation presentViewController:navController animated:YES completion:nil];
116130
}
117131
@catch(NSException* exception) { }
118132
}
119133

120134

121135

122136
@end
137+
138+

0 commit comments

Comments
 (0)