2

I use remote url for contents in my cordova, I use appcache to make it work offline - now the problem is handling the initial load before the appcache gets initialized.

In Android I let the device fallback to the local index.html - this could be informative eg. letting the user know that they have to be online to finalize the install.

    // On error show default message page...
public void onReceivedError( int errorCode, String description, String failingUrl)
{
    super.loadUrl("file:///android_asset/www/index.html");
    return;
}

Question: "How do I accomplish the same in IOS?"

Dont have to write the code for me - hints to files and api would be appreciated

  • can't you just use a custom alert div or something with javascript? – kangoroo Aug 31 '13 at 13:42
  • @kangoroo I'm loading directly from remote url - so no js to catch the error - I explicitly asked for native iOS since I dont want the js middleware –  Sep 01 '13 at 06:58
  • it's only reasonable if you are using cordova if you ask me, you are already running the engine for it. just saying. you are eating up more resources actually when you pull back native on cordova. – kangoroo Sep 01 '13 at 09:37
  • The above android code is inside the droidGap / webview - but cordova hasn't buildin options for falling back when using a remote url. I dont think the native one line would eat up more resources than js code trying to do the same. –  Sep 02 '13 at 08:31

2 Answers2

1

You can you the UIWebViewDelegate methods to detect that your remote content loading has failed to load. For example :

- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
   // here you can either check for the error type or for the url that has failed to load
   if([webView.request.url.absoluteString isEqualToString:@"your_remote_url")]
   {
      NSURL *url = [NSURL urlWithString:@"your_local_url"];
      NSURLRequest *request = [NSURLRequest requestWithURL:url];
      [webview loadRequest:request];
   }
}
oiledCode
  • 8,589
  • 6
  • 43
  • 59
  • Thanks @elio.d I think this is the correct answer just some additional links: https://developer.apple.com/library/Mac/documentation/Cocoa/Conceptual/ErrorHandlingCocoa/CreateCustomizeNSError/CreateCustomizeNSError.html http://stackoverflow.com/questions/8493082/simple-alert-view-for-connection-check-in-a-web-view http://stackoverflow.com/questions/7028383/didfailloadwitherror-is-called-with-uiwebview-even-though-page-later-loads –  Sep 05 '13 at 08:03
0

Since you simply want to do some error handling, one solution to this would be to do a SubView, and load it from whatever URL you need it from.

You can find a guide on how to do this, here:

http://docs.phonegap.com/en/3.0.0/guide_platforms_ios_webview.md.html#iOS%20WebViews

Of course, this gets called from the native part :)

Hope that helped!

Cutetare
  • 913
  • 8
  • 24