李程站 发表于 2013-2-4 02:28:21

iPhone网络软件在睡眠情况断线问题解决

iPhone网络软件在睡眠情况断线问题解决是本文要介绍的内容,主要是来学习iphone网络软件的使用。如果你希望使用iPhone的网络功能并保持长连接,并使用Wifi的话,你可能会发现一个问题,那就是在iPhone处于睡眠状态时,Wifi会中断,这样程序就无法保持连接。(iPhone非官方SDK)
下面的代码可能会帮你解决这个问题。
以下代码摘自MobileChat:
首先在applicationDidFinishLaunching方法中添加以下代码:

[*]IONotificationPortRef notificationPort;
[*]
[*]root_port = IORegisterForSystemPower(self, &notificationPort, powerCallback, &notifier);
[*]
[*]CFRunLoopAddSource(CFRunLoopGetCurrent(), IONotificationPortGetRunLoopSource(notificationPor t), kCFRunLoopCommonModes);
接着添加如下全局方法(在所有类之外添加)

[*]void powerCallback(void *refCon, io_service_t service, natural_t messageType, void *messageArgument) {
[*][(YourAppnameApp*)refCon powerMessageReceived: messageType withArgument: messageArgument];
[*]}
在你的程序里添加下面的代码:

[*]- (void)powerMessageReceived:(natural_t)messageType withArgument:(void *) messageArgument {
[*]switch (messageType) {
[*]case kIOMessageSystemWillSleep:
[*]IOAllowPowerChange(root_port, (long)messageArgument);   
[*]break;
[*]case kIOMessageCanSystemSleep:
[*]//if() {
[*]IOCancelPowerChange(root_port, (long)messageArgument);
[*]//}
[*]break;   
[*]case kIOMessageSystemHasPoweredOn:
[*]break;
[*]}
[*]}
这样就可以保持iPhone在网络连接的状况下不睡眠了(当然,可能会比较费电 ^_^)。
小结:iPhone网络软件在睡眠情况断线问题解决的内容介绍完了,希望通过本文的学习能对你有所帮助!
【编辑推荐】

[*]iPhone开发中如何将制作图片放大缩小代码实现案例
[*]解析iPhone操作队列和Java线程池
[*]iPhone开发中BSD Socket轻松学习教程
[*]iPhone开发应用中如何使BMP读取交显示解决方法
[*]iPhone开发之GameKit蓝牙实例讲解
[*]iPhone开发应用中如何以消息通知方式设置旋转View
页: [1]
查看完整版本: iPhone网络软件在睡眠情况断线问题解决