2013-10-14 39 views
0

我想在一定的时间间隔内发出url请求(例如,每10分钟的应用程序应该进行url调用并获取一些json数据)。应用程序应该能够在后台运行时执行此操作。这可以做到吗?如果是这样,这是否违反了Apple的服务条款?有任何限制吗?在后台运行URL请求

+0

是的,它可以做...我不认为苹果会拒绝应用程序,因为有很多应用程序支持后台模式运行.. –

+0

这将是很好,如果你通过应用程序审查指南..! –

回答

3

使用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中声明app支持的后台任务。我们需要将 的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]; 

希望它能帮助你。

+0

非常感谢您的详细解答 – sajaz

0

在基本级别:只需调度NSTimer并在定时器功能上启动URL请求即可。如果你的目标是iOS7,那么确保你使用的是NSURLSession。但重要的一点是要知道你是否想在后台做这件事。如果是的话,你必须找到更多关于iOS背景模式的信息,因为你将无法在这种模式下维护定时器。没有什么会导致拒绝。

+0

感谢您的详细解答。是的,我需要应用程序在后台模式下运行。实际要求是,我想跟踪用户当前位置并每10分钟发送一次服务器 – sajaz