Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Allow UIWebView to load http://localhost:port/path URIs without an Internet connection

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.

like image 658
jbg Avatar asked Jul 09 '10 01:07

jbg


People also ask

How do you access a website running on localhost from iPhone browser?

Accessing localhost from the iPhone will simply do a loopback / try to connect to itself (If it supports that?). What you need to do is find the IP of your desktop machine (e.g. If Windows, go to the Command Prompt and type ipconfig or go to Network and Sharing Centre and look up connection status .

What does UIWebView mean?

Android is powered by Chrome. Mobile Safari UIWebView. The UIWebView is different from the ordinary Safari browser, as it is not a stand-alone browser, but merely browser functionality that is embedded in a third party app that allows the app to display content from the web.

Can I use my iPhone as a server?

A new iPhone app called "ServersMan@iPhone" turns the iPhone into a full-blown Web server. The name is said to be a tribute to Sony's "WalkMan." Once the app is installed, PCs on the internet can access the iPhone to upload or download files through a browser or they can use the webDAV protocol.


1 Answers

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.

like image 184
Lily Ballard Avatar answered Sep 28 '22 05:09

Lily Ballard