在后台运行URL请求

我想在一定的时间间隔(例如,每10分钟的应用程序应该进行一个URL调用,并获得一些JSON数据)的URL请求。 应用程序应该能够在后台运行时执行此操作。 可以这样做吗? 如果是这样,这是否违反苹果的服务条款? 有什么限制吗?

随着ios 7新增的应用程序列表,可以在后台运行。 他们是:

1. Apps that play audible content to the user while in the background, such as a music player app 2. Apps that record audio content while in the background. 3. Apps that keep users informed of their location at all times, such as a navigation app 4. Apps that support Voice over Internet Protocol (VoIP) 5. Apps that need to download and process new content regularly 6. Apps that receive regular updates from external accessories 

我们只需要在info.plist中声明应用程序支持的后台任务。 我们需要添加UIBackgroundModes键到我们的应用程序的信息。 plist 。 之后,你可以正常使用你的计时器,如:

 UIBackgroundTaskIdentifier bgTask; UIApplication *app = [UIApplication sharedApplication]; bgTask = [app beginBackgroundTaskWithExpirationHandler:^{ [app endBackgroundTask:bgTask]; }]; self.timer = [NSTimer scheduledTimerWithTimeInterval:600 target:self selector:@selector(startServices) userInfo:nil repeats:YES]; 

希望它会帮助你。

在基本级别上:只需调度一个NSTimer,并在定时器function上启动URL请求。 如果你的目标是iOS7,那么确保你使用的是NSURLSession。 但重要的一点是要知道你是否想在后台做这件事。 如果是的话,你必须find更多有关iOS背景模式,因为你将无法保持你的计时器在这种模式下..什么都不应该导致拒绝。

我认为在iOS中有一些限制,当应用程序处于后台模式时,不能连续发送请求。 当App进入后台模式时,iOS允许一点时间来完成Web请求。 请检查此Apple文档: https : //developer.apple.com/library/ios/documentation/uikit/reference/UIApplication_Class/Reference/Reference.html#//apple_ref/occ/instm/UIApplication/beginBackgroundTaskWithExpirationHandler : /