IPhone network software disconnection during sleep

Source: Internet
Author: User

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:

 
 
  1. IONotificationPortRef notificationPort;  
  2.  
  3. root_port = IORegisterForSystemPower(self, &notificationPort, powerCallback, &notifier);  
  4.  
  5. CFRunLoopAddSource(CFRunLoopGetCurrent(), IONotificationPortGetRunLoopSource(notificationPor t), kCFRunLoopCommonModes);  

Then add the following global method to add it out of all classes)

 
 
  1. void powerCallback(void *refCon, io_service_t service, natural_t messageType, void *messageArgument) {  
  2. [(YourAppnameApp*)refCon powerMessageReceived: messageType withArgument: messageArgument];  
  3. }  

Add the following code to your program:

 
 
  1. - (void)powerMessageReceived:(natural_t)messageType withArgument:(void *) messageArgument {  
  2. switch (messageType) {  
  3. case kIOMessageSystemWillSleep:  
  4. IOAllowPowerChange(root_port, (long)messageArgument);   
  5. break;  
  6. case kIOMessageCanSystemSleep:  
  7. //if([self wifiKeepAliveIsSet]) {  
  8. IOCancelPowerChange(root_port, (long)messageArgument);  
  9. //}  
  10. break;   
  11. case kIOMessageSystemHasPoweredOn:  
  12. break;  
  13. }  

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!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.