IPhone NetworkThe solution to software disconnection during sleep is described in this article.Iphone NetworkUse of software. If you want to useIPhoneNetworkNetworkFeature and persistent connection, and use Wifi, you may find a problem, that is, inIPhoneWhen you are sleeping, Wifi is interrupted, so that the program cannot be connected. Unofficial iPhone 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 keep your iPhone sleep when it is connected to the Internet. Of course, it may be too expensive ).
Summary:IPhone NetworkAfter introducing the solution to the sleep disconnection problem, I hope this article will help you!