Solve the Problem of iPhone network software disconnection during sleep

Source: Internet
Author: User

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 ).

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.