在后台运行 iOS App 超过 10 分钟 [英] Running iOS App in background for more than 10 minutes

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

问题描述

我正试图让我的应用程序在后台运行超过 10 分钟,根据这个和我下面的好处.(我想使用长时间后台运行来跟踪位置,我这里的代码只是使用计数器进行测试)任何人都可以帮助指出问题所在?

I am trying to allow my app to run in the background for more that 10 minutes, according to this and my good below. (I want to use long background running to keep track of a location, my code here simply just use a counter for testing purposes) Anyone can help point out what the problem is?

实现长时间运行的后台任务

对于需要更多执行时间来实现的任务,您必须请求特定权限以在后台运行它们而无需他们被停职.在 iOS 中,只允许特定的应用程序类型在后台运行:

For tasks that require more execution time to implement, you must request specific permissions to run them in the background without their being suspended. In iOS, only specific app types are allowed to run in the background:

在后台向用户播放可听内容的应用,例如音乐播放器应用

Apps that play audible content to the user while in the background, such as a music player app

让用户随时了解其位置的应用,例如一个导航应用

Apps that keep users informed of their location at all times, such as a navigation app

支持互联网协议语音 (VoIP) 的应用

Apps that support Voice over Internet Protocol (VoIP)

需要下载和处理新内容的报亭应用程序接收来自外部附件的定期更新

Newsstand apps that need to download and process new content Apps that receive regular updates from external accessories

实现这些服务的应用必须声明它们的服务支持和使用系统框架来实现相关方面那些服务.声明服务让系统知道哪些您使用的服务,但在某些情况下是系统框架实际上可以防止您的应用程序被暂停.

Apps that implement these services must declare the services they support and use system frameworks to implement the relevant aspects of those services. Declaring the services lets the system know which services you use, but in some cases it is the system frameworks that actually prevent your application from being suspended.

   - (void)viewDidLoad {
             [super viewDidLoad];

        counterTask = [[UIApplication sharedApplication]
                       beginBackgroundTaskWithExpirationHandler:^{

                       }];
                count=0;

        theTimer=[NSTimer scheduledTimerWithTimeInterval:0.1
                                                  target:self
                                                selector:@selector(countUp)
                                                userInfo:nil
                                                 repeats:YES];
         }
    - (void)countUp {

        {
            count++;
            NSString *currentCount;
            currentCount=[[NSString alloc] initWithFormat:@"%ld",count];
            theCount.text=currentCount;
            [currentCount release];
        }
        }

另一个问题:我可以让 iOS 应用在后台永远运行吗?

Another Question: Can I have an iOS App run in the background forever?

----添加位置的编辑代码,仍然没有运行超过10分钟,对我做错了什么有帮助吗?

----Edited code to add location, still doesnt run for more than 10 mins, any help to what i'm doing wrong?

- (void)viewDidLoad {
     [super viewDidLoad];

 count=0;

locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.distanceFilter = kCLDistanceFilterNone;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;

 [locationManager startUpdatingLocation];

counterTask = [[UIApplication sharedApplication]
               beginBackgroundTaskWithExpirationHandler:^{
                   // If you're worried about exceeding 10 minutes, handle it here

                   [locationManager startUpdatingLocation];
               }];
theTimer=[NSTimer scheduledTimerWithTimeInterval:0.1
                                          target:self
                                        selector:@selector(countUp)
                                        userInfo:nil
                                         repeats:YES];

 }



     (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
 NSLog(@"OldLocation %f %f", oldLocation.coordinate.latitude, oldLocation.coordinate.longitude); NSLog(@"NewLocation %f %f", newLocation.coordinate.latitude, newLocation.coordinate.longitude);

    count++; NSString *currentCount;
 currentCount=[[NSString alloc] initWithFormat:@"%ld",count];
 theCount.text=currentCount; [currentCount release];
 }

    (void)countUp { [locationManager startUpdatingLocation];
 { count++; NSString *currentCount; 
currentCount=[[NSString alloc] initWithFormat:@"%ld",count]; 
theCount.text=currentCount;
 [currentCount release]; } }

推荐答案

所以你的应用使用了定位服务.那么请阅读位置感知编程指南.

So your app uses location services. Then please read the Location Awareness Programming Guide.

您需要对 Info.plist 进行一些更改:

You need to make some changes to your Info.plist:

  • 如果您的应用依赖定位服务才能正常运行,请将 location-services 添加到 UIRequiredDeviceCapabilities
  • 如果您的应用需要 GPS 硬件,请将 gps 添加到 UIRequiredDeviceCapabilities
  • 如果您需要在后台运行超过 10 分钟的应用程序,请将 location 添加到 UIBackgroundModes.然后,您的位置经理将提供超过 10 分钟限制的位置.
  • 你还应该设置NSLocationUsageDescription(也可以本地化)
  • If your app relies on location services to function properly, add location-services to UIRequiredDeviceCapabilities
  • if your app requires GPS hardware, add gps to UIRequiredDeviceCapabilities
  • if you need to run your app longer then 10 minutes in the background, add location to UIBackgroundModes. Then your location manager will deliver locations beyond the 10-minute-limit.
  • you should also set NSLocationUsageDescription (can also be localized)

在后台获取位置事件

如果您的应用需要提供位置更新,无论该应用是否在前景或背景,有多种选择.这首选选项是使用重大位置更改服务在适当的时间唤醒您的应用程序以处理新事件.然而,如果您的应用需要使用标准位置服务,您可以声明您的应用需要后台定位服务.

If your app needs location updates delivered whether the app is in the foreground or background, there are multiple options for doing so. The preferred option is to use the significant location change service to wake your app at appropriate times to handle new events. However, if your app needs to use the standard location service, you can declare your app as needing background location services.

只有在缺席的情况下,应用才应请求后台定位服务这些服务中的一些会损害其运营能力.此外,任何请求后台位置服务的应用程序都应该使用那些为用户提供切实利益的服务.例如,一个转弯导航应用程序可能是背景的候选者定位服务,因为它需要跟踪用户的位置和报告下一次转弯的时间.

An app should request background location services only if the absence of those services would impair its ability to operate. In addition, any app that requests background location services should use those services to provide a tangible benefit to the user. For example, a turn-by-turn navigation app would be a likely candidate for background location services because of its need to track the user’s position and report when it is time to make the next turn.

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

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