在iOS 5中,当应用程序进入后台时,wi-fi连接丢失。
但我想在设备休眠前的4-5分钟内使用wi-fi连接,因为一些任务可以在应用程序进入后台后的4-5分钟内执行。
我想这可以通过使用beginBackgroundTaskWithExpirationHandler:
来实现,但是我不能解决这个问题
发布于 2012-08-07 17:59:00
只需禁用iPhone即可进入睡眠模式
-(void) sleepModeDisable{
[[UIApplication sharedApplication] setIdleTimerDisabled:NO];
[[UIApplication sharedApplication] setIdleTimerDisabled:YES];
}
每10秒调用一次此函数,这可能会对您有所帮助
发布于 2012-08-07 19:52:10
我处理这个问题的方法是对我发送的每个网络请求使用beginBackgroundTaskWithExpirationHandler
。
这样,即使我的应用程序移到了后台,我也能确保我所有的网络都能完成。
我通常使用一个单例对象来处理所有网络请求,因此在发送请求之前,我会调用
- (void)startBackgroundTask
{
// ask for extra time if this is called when app go to suspended
UIApplication *application = [UIApplication sharedApplication];
_bgTask = [application beginBackgroundTaskWithExpirationHandler:^{
// Clean up any unfinished task business by marking where you.
// stopped or ending the task outright.
[application endBackgroundTask:_bgTask];
_bgTask = UIBackgroundTaskInvalid;
}];
}
当我得到一个响应(成功/失败)或者如果我取消了请求,我调用
- (void)stopBackgroudTask
{
UIApplication *app = [UIApplication sharedApplication];
if (_bgTask != UIBackgroundTaskInvalid) {
[app endBackgroundTask:_bgTask];
_bgTask = UIBackgroundTaskInvalid;
}
}
*别忘了定义UIBackgroundTaskIdentifier *_bgTask;
另外,如果你计划大量使用Wi-Fi,你应该在你的plist文件中设置Application uses Wi-Fi
键为YES,否则你的Wi-Fi将在30分钟后关闭,即使你的应用程序正在运行。
发布于 2015-10-01 15:12:40
这里没有火箭科学,这是为了节省电池,这是在iOS的行为,Wi-Fi关闭时,手机锁定,除非你告诉iOS,你的应用程序需要一个持久的Wi-Fi,然后它不会关闭它时,你的应用程序运行。
为此,只需将UIRequiresPersistentWiFi
添加到您的info.plist并将其标记为YES
https://stackoverflow.com/questions/11843395
复制相似问题