Skip to content

Commit

Permalink
Implemented shutdown of HTTPServer on app suspension and restart on a…
Browse files Browse the repository at this point in the history
…pp resume
  • Loading branch information
vronin committed Feb 20, 2013
1 parent bccfaa3 commit dad4e41
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 14 deletions.
47 changes: 33 additions & 14 deletions Samples/iPhoneHTTPServer/Classes/iPhoneHTTPServerAppDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,21 @@ @implementation iPhoneHTTPServerAppDelegate
@synthesize window;
@synthesize viewController;

- (void)startServer
{
// Start the server (and check for problems)

NSError *error;
if([httpServer start:&error])
{
DDLogInfo(@"Started HTTP Server on port %hu", [httpServer listeningPort]);
}
else
{
DDLogError(@"Error starting HTTP Server: %@", error);
}
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Configure our logging framework.
Expand All @@ -36,26 +51,30 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(
DDLogInfo(@"Setting document root: %@", webPath);

[httpServer setDocumentRoot:webPath];

// Start the server (and check for problems)

NSError *error;
if([httpServer start:&error])
{
DDLogInfo(@"Started HTTP Server on port %hu", [httpServer listeningPort]);
}
else
{
DDLogError(@"Error starting HTTP Server: %@", error);
}


[self startServer];

// Add the view controller's view to the window and display.
[window addSubview:viewController.view];
[window makeKeyAndVisible];

return YES;
}

- (void)applicationWillEnterForeground:(UIApplication *)application
{
[self startServer];
}

- (void)applicationDidEnterBackground:(UIApplication *)application
{
// There is no public(allowed in AppStore) method for iOS to run continiously in the background for our purposes (serving HTTP).
// So, we stop the server when the app is paused (if a users exits from the app or locks a device) and
// restart the server when the app is resumed (based on this document: http://developer.apple.com/library/ios/#technotes/tn2277/_index.html )

[httpServer stop];
}



@end
3 changes: 3 additions & 0 deletions Samples/iPhoneHTTPServer/README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ This is a bare bones project demonstrating how to embed the CocoaHTTPServer in a

Notice the Web folder. This folder is added to the project as a resource, and the folder itself is copied into the iPhone app. The contents of the folder are set as the document root of the http server.

Critical note: The server is running only while application is in foreground. As soon as you exit from the application (by switching to another app or locking a device) the server will be stopped. The server will be restarted when you enter the application again.
Read this Apple technical note for the details: http://developer.apple.com/library/ios/#technotes/tn2277/_index.html

INSTRUCTIONS:

Open the Xcode project, and build and go.
Expand Down

0 comments on commit dad4e41

Please sign in to comment.