Sunday, December 7, 2008

HTTP Server - iPhone example

The CocoaHTTPServer project just posted a sample iPhone project that demonstrates

  • How to incorporate the server code into an iPhone project

  • How to dynamically create an index page listing files on the phone

  • How to accept uploaded files, and process multipart form data





This is all thanks to developer Nonnus who graciously decided to share the code with the development community!

You can grab the code from the downloads page.

And coming soon to the project:
Support for PUT and other methods.

9 comments:

jason said...

wow. thanks. before the weekend i was stuck using a bad borked iphone http server. suddenly there's a good one released over the weekend and i'm in good shape.

thanks again.

Chris Hanson said...

Great example and very helpful! Thank you much!

I'll mention little problem with the code that others may struggle with.

If after starting the app, the user doesn't quickly turn "on" the server, the port that is displayed on screen is 0. The problem is that if "LocalhostAdressesResolved" calls the displayInfoUpdate routine before the user turns on the server the first time, "port" has a value of 0 and that's what is displayed. Future changes to "port" are not updated on the screen.

Again, not a big problem to change this in other implementations, but I thought this might save someone else a bit of debugging.

Robbie Hanson said...

Thanks for mentioning it.

The developer who wrote the example code wanted me to make a few important changes, so I also went ahead and modified it to update the displayed port when the server is started. The latest download is "iPhoneHTTPServer2.zip".

Krikke said...

This iphone http server is really helping me forward. I was wondering if it is possible to download a directory (containing a bunch of files) in one go, instead of downloading the files one by one.

Thanks alot and happy New Year!

Robbie Hanson said...

Hi Krikke,

Yes, this is possible. You could tar the directory, and then maybe compress it using something like gzip.

Krikke said...

Robbie,

I knew gzip is available for the iPhone, but I have no idea whether it's possible to tar a directory on the iPhone. Do you have any experience with that?

Thanks.

Anonymous said...

I'm trying to get this example to work for me but I haven't been able to. In the simulator I can use safari to see the http and if I put a file in the documents folder of the app I am able to see it in safari but when I try to upload a file the app crashes and I get "The application iPhoneHTTPServer quit unexpectedly". I get this crash log...

Application Specific Information:
iPhone Simulator 2.2 (77.4.9), iPhone OS 2.2.1 (5H11)
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSConcreteData offsetInFile]: unrecognized selector sent to instance 0x44667a0'

I don't know what to do to fix it. Can anyone help?

Also when I try to mount the http on the desktop I get a warning in the console error 405 so I guess it is not meant to do that.

Thank You

Alain

formersjsu said...

Is it possible to upload multiple files? Even if the user could specify and upload 5 files at a time, it would be cool.

thilina3001 said...

Hey, the code is not there.. Could someone give me the project please ?