Http://www.cnblogs.com/zhwl/archive/2013/01/09/2852795.html
If you want to use the iPhone's network functions, maintain persistent connections, and use WiFi, you may find that WiFi is interrupted when the iPhone is sleeping, in this way, the program cannot maintain the connection. (Unofficial iPhone SDK)
The following code may help you solve this problem.
The following code is taken from mobilechat:
First, add the following code to the applicationdidfinishlaunching method:
Ionotificationportref notificationport;
Root_port = ioregisterforsystempower (self, & notificationport, powercallback, & notifier );
Cfrunloopaddsource (cfrunloopgetcurrent (), ionotificationportgetrunloopsource (icationicationpor T), kcfrunloopcommonmodes );
Next, add the following global method (add outside all classes)
Void powercallback (void * refcon, io_service_t service, natural_t messagetype, void * messageargument)
{
[(Yourappnameapp *) refcon powermessagereceived: messagetype withargument: messageargument];
}
Add the following code to your program:
-(Void) powermessagereceived :( natural_t) messagetype withargument :( void *) messageargument {
Switch (messagetype ){
Case kiomessagesystemwillsleep:
Ioallowpowerchange (root_port, (long) messageargument );
Break;
Case kiomessagecansystemsleep:
// If ([self wifikeepaliveisset]) {
Iocancelpowerchange (root_port, (long) messageargument );
//}
Break;
Case kiomessagesystemhaspoweredon:
Break;
}
}
In this way, you can keep your iPhone sleep when it is connected to the Internet (of course, it may be too expensive ).