Simple way to embed a http server into your iPhone app

Posted by face on June 15, 2009

Announcing MongooseDaemon, an objective-c wrapper for the wonderful mongoose http server.

It can be indispensable to be able to explore your iPhone apps directory structure when developing and debugging iPhone applications. You may also want to serve up content from your iPhone application via http.

Erica Sadun’s cookbook has an example of a hand rolled http server. However, it is only an example and is incomplete. I recently needed a http server in an iPhone app and after playing with Erica’s example I quickly realized I didn’t want to be in the business of creating a complete http server.

So I started looking for an existing http server I could embed in my app. I quickly found moongoose.

With just a few minutes of coding, I was able to get mongoose working in my iPhone application. To make it even easier for the next developer, I extracted my wrapper into the MongooseDaemon class and am offering it under a BSD license.

Here is some example code taken directly from one of my apps I am debugging:

Add the following to one of your projects classes .h (MyAppDelegate.h for this example):
1
2
3
4
5
6
7
8
9
  #import "MongooseDaemon.h"
  ....
  @class MongooseDaemon

  @interface MyAppDelegate : NSObject <UIApplicationDelegate, UITabBarControllerDelegate> {
    ...
    MongooseDaemon    *mongooseDaemon;
    ...
  }
And add the following to the class ( MyAppDelegate.m for this example):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
  ...
  @implementation LightyAppDelegate
  ...
  - (void)applicationDidFinishLaunching:(UIApplication *)application {
  mongooseDaemon = [[MongooseDaemon alloc] init];
  [mongooseDaemon startMongooseDaemon:@"8080"];

  ...
  - (void)dealloc {
  ...
    [mongooseDaemon stopMongooseDaemon];
    [mongooseDaemon release];
  ...
  }

Now you can surf to your application. For example, if your iPhones IP on wifi is 192.168.1.100 (sorry a helper method for determining your iPhones IP will be coming soon), then you could surf to http://192.168.1.100:8080/

Serve files in good health!


Digg! Delicious! Technorati Blinklist Furl Reddit