IPhone NetworkSoftware inSleepDisconnection is the content of this article. If you want to useIPhoneOfNetworkFeature and persistent connection, and use Wifi, you may find a problem, that is, inIPhoneInSleepIf the connection is not established, the connection is interrupted.IPhoneUnofficial sdks)
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, ¬ificationPort, powerCallback, ¬ifier);
- CFRunLoopAddSource(CFRunLoopGetCurrent(), IONotificationPortGetRunLoopSource(notificationPor t), kCFRunLoopCommonModes);
Then add the following global method to add it out of 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 maintainIPhoneInNetworkNot connectedSleepOf course, it may be too expensive ).
Summary:IPhone NetworkSoftware inSleepThe problem disconnection has been solved. I hope this article will help you.