Strange behavior after relaunching an iOS app after termination

94 views Asked by At

I'm developing an in-house iOS app that requires an HTTP Server to be running all the time.

To do so, I use a background task to keep my application alive (playing audio in the background), and then I start the HTTP server. This is the code from my AppDelegate.

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    bgTask = [[MyBackgroundTask alloc] init];
    [bgTask startBackgroundTaskWithTarget:self selector:@selector(backgroundCallback:)];

    httpServer = [[HTTPServer alloc] init];
    httpServer.interface = @"localhost";    
    [httpServer setType:@"_http._tcp."];
    [httpServer setPort:8080];
    [httpServer start];
}

And it works perfectly... almost!

Sometimes, the application, while running in the background, is being killed. For example:

Jun 24 11:27:24 My-iPhone SpringBoard[43] <Warning>: Application 'UIKitApplication:com.test.MyApp[0x668d]' was killed by jetsam.

And obviously the HTTP Server goes down. But when I relaunch the app after this termination, the HTTP Server won't start! (When I type localhost:8080 in Safari, I see an error message).

I don't have a problem with termination, that's completely okay. But I want my HTTP Server to be up and running when I reopen the app. But why that does not happen?

(Please note that if I close the app myself, everything works good).

1

There are 1 answers

1
Mikael On

Maybe :

Be prepared to handle connection failures in your network-based sockets. The system may tear down socket connections while your app is suspended for any number of reasons. As long as your socket-based code is prepared for other types of network failures, such as a lost signal or network transition, this should not lead to any unusual problems. When your app resumes, if it encounters a failure upon using a socket, simply reestablish the connection.

or any other reasons from https://developer.apple.com/library/ios/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/BackgroundExecution/BackgroundExecution.html