解决iPhone网络软件在睡眠情况断线问题
时间: 2009-06-11 19:55 点击: 次
如果你希望使用iPhone的网络功能并保持长连接,并使用Wifi的话,你可能会发现一个问题,那就是在iPhone处于睡眠状态时,Wifi会中断,这样程序就无法保持连接。(iPhone非官方SDK)
下面的代码可能会帮你解决这个问题。
以下代码摘自MobileChat:
首先在applicationDidFinishLaunching方法中添加以下代码:
IONotificationPortRef notificationPort;
root_port = IORegisterForSystemPower(self, ¬ificationPort, powerCallback, ¬ifier);
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([self wifiKeepAliveIsSet]) {
IOCancelPowerChange(root_port, (long)messageArgument);
//}
break;
case kIOMessageSystemHasPoweredOn:
break;
}
}
这样就可以保持iPhone在网络连接的状况下不睡眠了(当然,可能会比较费电 ^_^)。
顶一下(1)
100%
踩一下(0)
0%
打印 上一篇:iPhone横屏效果代码
下一篇:获取iPhone用户手机号
发表评论
本类文章点击排行榜
- [06-11] CocoaChina整理的开源iPhone程序不
- [06-11] 获取iPhone用户手机号
- [05-11] 制作iPhone PXL安装包的方法
- [06-11] iPhone横屏效果代码
- [06-11] 创建iPhone锁定划动条的方法
- [06-11] iPhone视频播放器例子
- [06-11] 如何搭建自己的iPhone Installe
- [06-11] 解决iPhone网络软件在睡眠情况
- [06-11] 在iPhone 2.0(非App Store)上执行
最近更新
- [01-28] 在iPhone App中使用Thumb指令集
- [01-22] 用ToolChain开发App,且和DEV有相同环境
- [06-11] 在iPhone 2.0(非App Store)上执行程序的方法
- [06-11] 将iPhone toolchain 1.x移植到toolchain 2.x
- [06-11] 在程序中关闭iPhone
- [06-11] 创建iPhone锁定划动条的方法
- [06-11] 获取iPhone用户手机号
- [06-11] 解决iPhone网络软件在睡眠情况断线问题
- [06-11] iPhone横屏效果代码

在iPhone App中使用Thumb指令集
创建iPhone锁定划动条的方法
如何搭建自己的iPhone Installer软件源