You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cordova.apache.org by GitBox <gi...@apache.org> on 2018/05/16 05:41:23 UTC

[GitHub] shazron closed pull request #363: CB-14045 - reinit url after app freezes

shazron closed pull request #363: CB-14045 - reinit url after app freezes
URL: https://github.com/apache/cordova-ios/pull/363
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/CordovaLib/Classes/Public/CDVViewController.m b/CordovaLib/Classes/Public/CDVViewController.m
index d0c05865a..13f1a9038 100644
--- a/CordovaLib/Classes/Public/CDVViewController.m
+++ b/CordovaLib/Classes/Public/CDVViewController.m
@@ -720,12 +720,33 @@ - (void)onAppWillTerminate:(NSNotification*)notification
     }
 }
 
+- (bool)isUrlEmpty:(NSURL *)url
+{
+    if (!url || (url == (id) [NSNull null])) {
+        return true;
+    }
+    NSString *urlAsString = [url absoluteString];
+    return (urlAsString == (id) [NSNull null] || [urlAsString length]==0 || [urlAsString isEqualToString:@"about:blank"]);
+}
+
+- (bool)checkAndReinitViewUrl
+{
+    NSURL* appURL = [self appUrl];
+    if ([self isUrlEmpty: [self.webViewEngine URL]] && ![self isUrlEmpty: appURL]) {
+        NSURLRequest* appReq = [NSURLRequest requestWithURL:appURL cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:20.0];
+        [self.webViewEngine loadRequest:appReq];
+        return true;
+    }
+    return false;
+}
+
 /*
  This method is called to let your application know that it is about to move from the active to inactive state.
  You should use this method to pause ongoing tasks, disable timer, ...
  */
 - (void)onAppWillResignActive:(NSNotification*)notification
 {
+    [self checkAndReinitViewUrl];
     // NSLog(@"%@",@"applicationWillResignActive");
     [self.commandDelegate evalJs:@"cordova.fireDocumentEvent('resign');" scheduledOnRunLoop:NO];
 }
@@ -737,6 +758,7 @@ - (void)onAppWillResignActive:(NSNotification*)notification
  */
 - (void)onAppWillEnterForeground:(NSNotification*)notification
 {
+    [self checkAndReinitViewUrl];
     // NSLog(@"%@",@"applicationWillEnterForeground");
     [self.commandDelegate evalJs:@"cordova.fireDocumentEvent('resume');"];
 
@@ -751,6 +773,7 @@ - (void)onAppWillEnterForeground:(NSNotification*)notification
 // This method is called to let your application know that it moved from the inactive to active state.
 - (void)onAppDidBecomeActive:(NSNotification*)notification
 {
+    [self checkAndReinitViewUrl];
     // NSLog(@"%@",@"applicationDidBecomeActive");
     [self.commandDelegate evalJs:@"cordova.fireDocumentEvent('active');"];
 }
@@ -761,6 +784,7 @@ - (void)onAppDidBecomeActive:(NSNotification*)notification
  */
 - (void)onAppDidEnterBackground:(NSNotification*)notification
 {
+    [self checkAndReinitViewUrl];
     // NSLog(@"%@",@"applicationDidEnterBackground");
     [self.commandDelegate evalJs:@"cordova.fireDocumentEvent('pause', null, true);" scheduledOnRunLoop:NO];
 }
diff --git a/tests/CordovaLibTests/CDVViewControllerTest.m b/tests/CordovaLibTests/CDVViewControllerTest.m
index 9d0f7c06f..9e13a883f 100644
--- a/tests/CordovaLibTests/CDVViewControllerTest.m
+++ b/tests/CordovaLibTests/CDVViewControllerTest.m
@@ -26,6 +26,14 @@ @interface CDVViewControllerTest : XCTestCase
 
 @end
 
+@interface CDVViewController ()
+
+// expose private interface
+- (bool)checkAndReinitViewUrl;
+- (bool)isUrlEmpty:(NSURL*)url;
+
+@end
+
 @implementation CDVViewControllerTest
 
 -(CDVViewController*)viewController{
@@ -89,5 +97,27 @@ -(void)testColorFromColorString{
     XCTAssertNil([viewController colorFromColorString:@"#NOTHEX"]);
 }
 
+-(void)testIsUrlEmpty{
+    CDVViewController* viewController = [self viewController];
+    XCTAssertTrue([viewController isUrlEmpty:(id)[NSNull null]]);
+    XCTAssertTrue([viewController isUrlEmpty:nil]);
+    XCTAssertTrue([viewController isUrlEmpty:[NSURL URLWithString:@""]]);
+    XCTAssertTrue([viewController isUrlEmpty:[NSURL URLWithString:@"about:blank"]]);
+}
+
+-(void)testIfItLoadsAppUrlIfCurrentViewIsBlank{
+    CDVViewController* viewController = [self viewController];
+    
+    NSString* appUrl = @"about:blank";
+    NSString* html = @"<html><body></body></html>";
+    [viewController.webViewEngine loadHTMLString:html baseURL:[NSURL URLWithString:appUrl]];
+    XCTAssertFalse([viewController checkAndReinitViewUrl]);
+
+    appUrl = @"https://cordova.apache.org";
+    viewController.startPage = appUrl;
+    [viewController.webViewEngine loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:appUrl]]];
+    XCTAssertTrue([viewController checkAndReinitViewUrl]);
+}
+
 @end
 


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services

---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@cordova.apache.org
For additional commands, e-mail: commits-help@cordova.apache.org