Skip to content

Commit 9f42321

Browse files
committed
Fixed SDK VebView hiding when opening a 2nd notification with a URL
* Also uses openURL for app scheme URL instead of trying to show a web view. * This commit fixes issue #123.
1 parent 8becfce commit 9f42321

File tree

4 files changed

+33
-29
lines changed

4 files changed

+33
-29
lines changed

iOS_SDK/OneSignal/OneSignal.m

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -983,12 +983,11 @@ + (void) handleNotificationOpened:(NSDictionary*)messageDict isActive:(BOOL)isAc
983983

984984
+ (void)launchWebURL:(NSString*)openUrl {
985985

986-
if (openUrl) {
987-
if ([OneSignalHelper verifyURL:openUrl]) {
988-
//Create a dleay to allow alertview to dismiss before showing anything or going to safari
989-
NSURL *url = [NSURL URLWithString:openUrl];
990-
[OneSignalHelper performSelector:@selector(displayWebView:) withObject:url afterDelay:0.5];
991-
}
986+
if (openUrl && [OneSignalHelper verifyURL:openUrl]) {
987+
NSURL *url = [NSURL URLWithString:openUrl];
988+
// Give the app resume animation time to finish when tapping on a notificaion from the notificaiton center.
989+
// Isn't a requiremnt but improves visual flow.
990+
[OneSignalHelper performSelector:@selector(displayWebView:) withObject:url afterDelay:0.5];
992991
}
993992

994993
}

iOS_SDK/OneSignal/OneSignalHelper.m

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -824,22 +824,29 @@ + (void)handleJSONNSURLResponse:(NSURLResponse*) response data:(NSData*) data er
824824

825825
+ (void) displayWebView:(NSURL*)url {
826826

827-
//Check if in-app or safari
827+
// Check if in-app or safari
828828
BOOL inAppLaunch = YES;
829-
if(![[NSUserDefaults standardUserDefaults] objectForKey:@"ONESIGNAL_INAPP_LAUNCH_URL"]) {
829+
if( ![[NSUserDefaults standardUserDefaults] objectForKey:@"ONESIGNAL_INAPP_LAUNCH_URL"]) {
830830
[[NSUserDefaults standardUserDefaults] setObject:@YES forKey:@"ONESIGNAL_INAPP_LAUNCH_URL"];
831831
[[NSUserDefaults standardUserDefaults] synchronize];
832832
}
833833

834834
inAppLaunch = [[[NSUserDefaults standardUserDefaults] objectForKey:@"ONESIGNAL_INAPP_LAUNCH_URL"] boolValue];
835+
NSString* urlScheme = [url.scheme lowercaseString];
836+
BOOL isWWWScheme = [urlScheme isEqualToString:@"http"] || [urlScheme isEqualToString:@"https"];
835837

836-
if(inAppLaunch) {
837-
if(!webVC)
838+
if (inAppLaunch && isWWWScheme) {
839+
if (!webVC)
838840
webVC = [[OneSignalWebView alloc] init];
839841
webVC.url = url;
840842
[webVC showInApp];
841843
}
842-
else [[UIApplication sharedApplication] openURL:url];
844+
else {
845+
// Keep dispatch_async. Without this the url can take an extra 2 to 10 secounds to open.
846+
dispatch_async(dispatch_get_main_queue(), ^{
847+
[[UIApplication sharedApplication] openURL:url];
848+
});
849+
}
843850

844851
}
845852

iOS_SDK/OneSignalTracker.m

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,15 @@ + (void) endBackgroundFocusTask {
6060
}
6161

6262
+ (void)onFocus:(BOOL)toBackground {
63-
bool wasBadgeSet = false;
6463

6564
//Prevent the onFocus to be called twice when app being terminated
6665
// (Both WillResignActive and willTerminate
67-
if(lastOnFocusWasToBackground == toBackground) return;
66+
if (lastOnFocusWasToBackground == toBackground)
67+
return;
6868
lastOnFocusWasToBackground = toBackground;
6969

70+
bool wasBadgeSet = false;
71+
7072
NSTimeInterval now = [[NSDate date] timeIntervalSince1970];
7173
NSTimeInterval timeToPingWith = 0.0;
7274

@@ -75,11 +77,6 @@ + (void)onFocus:(BOOL)toBackground {
7577
lastOpenedTime = now;
7678
[OneSignal sendNotificationTypesUpdate];
7779
wasBadgeSet = [OneSignal clearBadgeCount:false];
78-
79-
//Make sure webview dismissed if came back from deep link
80-
OneSignalWebView *webVC = [OneSignalHelper webVC];
81-
if(webVC)
82-
[webVC dismiss:self];
8380
}
8481
else {
8582

iOS_SDK/OneSignalWebView.m

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ @implementation OneSignalWebView
3939
UIViewController *viewControllerForPresentation;
4040

4141
-(void)viewDidLoad {
42-
4342
_webView = [[UIWebView alloc] initWithFrame:self.view.frame];
4443
_webView.delegate = self;
4544
[self.view addSubview:_webView];
@@ -54,6 +53,7 @@ -(void)viewDidLoad {
5453
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:_uiBusy];
5554
}
5655

56+
5757
-(void)viewDidAppear:(BOOL)animated {
5858
[super viewDidAppear:animated];
5959
if (_url)
@@ -65,7 +65,7 @@ -(void)dismiss:(id)sender {
6565
[self.navigationController dismissViewControllerAnimated:true completion:^{
6666
//clear web view
6767
[_webView loadHTMLString:@"" baseURL:nil];
68-
if(viewControllerForPresentation)
68+
if (viewControllerForPresentation)
6969
[viewControllerForPresentation.view removeFromSuperview];
7070
}];
7171
}
@@ -86,31 +86,32 @@ -(void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {
8686

8787

8888
-(void)showInApp {
89-
90-
//if already presented, no need to present again
91-
if(!navController) {
89+
// if already presented, no need to present again
90+
if (!navController) {
9291
navController = [[UINavigationController alloc] initWithRootViewController:self];
9392
navController.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
9493
}
95-
if(!viewControllerForPresentation) {
94+
if (!viewControllerForPresentation) {
9695
viewControllerForPresentation = [[UIViewController alloc] init];
9796
[[viewControllerForPresentation view] setBackgroundColor:[UIColor clearColor]];
9897
[[viewControllerForPresentation view] setOpaque:FALSE];
9998
}
100-
101-
UIWindow* mainWindow = [[UIApplication sharedApplication] keyWindow];
10299

103100
if (navController.isViewLoaded && navController.view.window) {
104101
// navController is visible only refresh webview
105-
if(_url)
102+
if (_url)
106103
[_webView loadRequest:[NSURLRequest requestWithURL:_url]];
107104
return;
108105
}
109106

110-
if(!viewControllerForPresentation.view.superview)
107+
UIWindow* mainWindow = [[UIApplication sharedApplication] keyWindow];
108+
109+
if (!viewControllerForPresentation.view.superview)
111110
[mainWindow addSubview:[viewControllerForPresentation view]];
112111

113-
@try { [viewControllerForPresentation presentViewController:navController animated:YES completion:NULL]; }
112+
@try {
113+
[viewControllerForPresentation presentViewController:navController animated:NO completion:nil];
114+
}
114115
@catch(NSException* exception) { }
115116
}
116117

0 commit comments

Comments
 (0)