10

I am working on an iPad app which has an embedded HTTP server listening on a high port, and I am loading pages from this HTTP server using a UIWebView.

For the functionality of the app, it is important that this works when no network connection is available.

The UIWebView is loading URLs of the form http://localhost:port/path

When I disable Wi-Fi on my iPad and run the application, the UIWebView’s delegate gets a webView:didFailLoadWithError: message with the following NSError object:

Error Domain=NSURLErrorDomain Code=-1009 UserInfo=0x1b4a50 "no Internet connection"

Clearly the SDK is being clever and detecting that it has no Internet connection, but it should be possible to access 127.0.0.1/localhost/::1 without an Internet connection. I will report the bug to Apple but am asking here for advice about possible workarounds for this SDK bug in the meantime.

Please note: changing the architecture of the application is not in scope for this question.

jbg
  • 141
  • 1
  • 6
  • Why not load the pages with UIWebView's `loadHTMLString:baseURL:` method? – lucius Jul 09 '10 at 02:06
  • 1
    I can’t really go into much detail (hence the last sentence of the question), but there are good reasons why I can’t use that method involving needing to handle external script, style and image resources which the page references and which aren’t easily determined before the initial page load starts. The embedded web server is acting like a kind of proxy server to resources which it obtains via a means other than Wi-Fi/3G network access. – jbg Jul 09 '10 at 02:12

1 Answers1

7

You may want to investigate defining your own scheme and implementing it with NSURLProtocol instead of running an HTTP server. This will allow you to handle the loading of URLs without having to implement the HTTP protocol, and will get around this issue with the SDK where the HTTP protocol fails early if you have no network connection.

Lily Ballard
  • 182,031
  • 33
  • 381
  • 347
  • Thank you. This is exactly what I was looking for, and I was even able to have my `NSURLProtocol` subclass respond to the `http` and `https` protocols, effectively overriding the default platform HTTP behaviour. Fantastic. – jbg Jul 09 '10 at 06:44
  • 1
    Looks like the issue is only with `UIWebView` control and NOT in newer control - `WKWebView`. The later control allows http local host file access without validating internet connection/failure. I noticed this behavior in my app for a downloaded video. My app plays ` – Ashok Jan 15 '15 at 15:10