iPhone網(wǎng)絡(luò)軟件在睡眠情況斷線問題解決
iPhone網(wǎng)絡(luò)軟件在睡眠情況斷線問題解決是本文要介紹的內(nèi)容,主要是來學(xué)習(xí)iphone網(wǎng)絡(luò)軟件的使用。如果你希望使用iPhone的網(wǎng)絡(luò)功能并保持長連接,并使用Wifi的話,你可能會發(fā)現(xiàn)一個問題,那就是在iPhone處于睡眠狀態(tài)時,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在網(wǎng)絡(luò)連接的狀況下不睡眠了(當(dāng)然,可能會比較費(fèi)電 ^_^)。
小結(jié):iPhone網(wǎng)絡(luò)軟件在睡眠情況斷線問題解決的內(nèi)容介紹完了,希望通過本文的學(xué)習(xí)能對你有所幫助!