在后台运行URL请求 [英] Running URL Requests in the Background

查看:87
本文介绍了在后台运行URL请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to make url request in a certain time interval (e.g. every 10 minutes app should make a url call and get some json data). App should be able to do this when running in background. Can this be done? If so, does this violate Apple terms of service? Are there any restrictions?

推荐答案

使用ios 7添加了可在后台运行的新应用程序列表。它们是:

With ios 7 new list of apps are added which can run in background. They are:

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中
即可。之后,您可以使用计时器正常工作,例如:

We just need to declare app's supported background tasks in info.plist. We need to add The UIBackgroundModes key to our app’s Info. plist. After that you can work normally with your timer, like:

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];

希望它能为您提供帮助。

Hope it will help you.

这篇关于在后台运行URL请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆