Question

I have a UiWebView that is pointing to an external site that has a session expiration of 30 minutes of inactivity. In my app, I have a custom login page embedded in the application because I can't use one from the remote site. This login page is:

file://index.html

When the user puts the app into the background, I want to automatically reload my login page if the application stays in the background for more than 20 minutes (I know this isn't ideal, but forced to do this because of the business requirements).

My code to do this is fairly simple:

static NSDate *lastActivity = nil;

- (void)applicationWillResignActive:(UIApplication *)application
{
    lastActivity = [NSDate date];
}

- (void)applicationDidBecomeActive:(UIApplication *)application
{
    NSDate *now = [NSDate date];
    NSTimeInterval time = [now timeIntervalSinceDate:lastActivity];
    if(time > 60 * 20){
       UIWebView *view = self.viewController.webView;
       NSURL *url = [NSURL URLWithString:self.viewController.startPage];
       NSURLRequest *request = [NSURLRequest requestWithURL:url];
       [view loadRequest:request];

    }
}

However when I do this I receive the error:

Failed to load webpage with error: Frame load interrupted

I understand this might be because something is automatically going from one URL scheme to another without user interaction. Is there a way to do this?

Was it helpful?

Solution

I think you will need to handle the file:// protocol in a UIWebView delegate method. eg.

- (BOOL)webView:(UIWebView *)theWebView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
   // Intercept the external http requests and forward to Safari.app
    // Otherwise forward to the PhoneGap WebView
    if ([[url scheme] isEqualToString:@"http"] || [[url scheme] isEqualToString:@"https"]) {
        [[UIApplication sharedApplication] openURL:url];
        return NO;
    }
    else {
        return [ super webView:theWebView shouldStartLoadWithRequest:request navigationType:navigationType ];
    }
}

In your case, tell the delegate method what to do with your url scheme. eg.

if ([url.scheme isEqualToString:@"file"]) {
    NSLog(@"Open start page");
    [[UIApplication sharedApplication] openURL:url];
    return NO;
}

Not sure if this will work for you, but hopefully it will provide a path to the solution.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top