IPhone network software disconnection during sleep has been solved

Source: Internet
Author: User

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:

 
 
  1.  IONotificationPortRef notificationPort;  
  2. root_port = IORegisterForSystemPower(self, &notificationPort, powerCallback, &notifier);  
  3. 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];  

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 maintainIPhoneInNetworkNot connectedSleepOf course, it may be too expensive ).

Summary:IPhone NetworkSoftware inSleepThe problem disconnection has been solved. 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.