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

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

问题描述

我试图让我的应用程序在后台更是运行10分钟,根据这一点,下面我的好。 (我想用长后台运行,保持位置的轨道,我的code在这里只是简单地用于测试目的的计数器)
任何人都可以帮助指出的问题是什么?


  

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


  
  

对于需要更多时间执行任务的落实,必须
  请求特定的权限,而不运行它们在后台
  他们被暂停。在iOS中,只有特定类型的应用程序允许
  在后台运行:


  
  

应用程序的播放音​​频内容给用户,同时在后台运行,
  如音乐播放器应用程序


  
  

应用程序,使用户了解自己的位置在任何时候,如
  导航应用


  
  

这是支持互联网语音传输协议(VoIP)

应用程序
  
  

这需要下载和处理新的内容应用报刊杂志应用程序,
  定期接收最新消息来自外部附件


  
  

这是实现这些服务的应用必须申报他们的服务
  支持和使用的系统框架,以执行相关方面
  这些服务。声明服务让系统知道哪些
  您使用的服务,但在某些情况下,它是在系统框架,
  实际上$ P $被暂停pvent您的应用程序。


   - (无效)viewDidLoad中{
             [超级viewDidLoad中];        counterTask = [[UIApplication的sharedApplication]
                       beginBackgroundTaskWithExpirationHandler:^ {                       }];
                计数= 0;        theTimer = [的NSTimer scheduledTimerWithTimeInterval:0.1
                                                  目标:自我
                                                选择:@selector(COUNTUP)
                                                用户信息:无
                                                 重复:YES];
         }
     - (无效){COUNTUP        {
            算上++;
            * NSString的CURRENTCOUNT;
            CURRENTCOUNT = [[NSString的页头] initWithFormat:@%LD,计数]
            theCount.text = CURRENTCOUNT;
            [CURRENTCOUNT发布]
        }
        }

另一个问题:我有一个iOS应用程序永远在后台运行

----编辑code以添加位置,还是犯规运行时间超过10分钟,任何人的帮助我在做什么错了?

   - (无效)viewDidLoad中{
     [超级viewDidLoad中]; 计数= 0;的LocationManager = [[CLLocationManager的alloc]初始化];
locationManager.delegate =自我;
locationManager.distanceFilter = kCLDistanceFilterNone;
locationManager.desiredAccuracy = kCLLocationAccuracyBest; [的LocationManager startUpdatingLocation]counterTask = [[UIApplication的sharedApplication]
               beginBackgroundTaskWithExpirationHandler:^ {
                   //如果你担心超过10分钟,在这里处理它                   [的LocationManager startUpdatingLocation]
               }];
theTimer = [的NSTimer scheduledTimerWithTimeInterval:0.1
                                          目标:自我
                                        选择:@selector(COUNTUP)
                                        用户信息:无
                                         重复:YES]; }     (无效)的LocationManager:(CLLocationManager *)经理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);    算上++; * NSString的CURRENTCOUNT;
 CURRENTCOUNT = [[NSString的页头] initWithFormat:@%LD,计数]
 theCount.text = CURRENTCOUNT; [CURRENTCOUNT发布]
 }    (无效){COUNTUP [的LocationManager startUpdatingLocation]
 {算++; * NSString的CURRENTCOUNT;
CURRENTCOUNT = [[NSString的页头] initWithFormat:@%LD,计数]
theCount.text = CURRENTCOUNT;
 [CURRENTCOUNT发布] }}


解决方案

所以,你的应用程序使用定位服务。那么请阅读<一href=\"http://developer.apple.com/library/ios/#documentation/userexperience/conceptual/LocationAwarenessPG/CoreLocation/CoreLocation.html\"相对=nofollow>位置感知编程指南。

您需要做出一些更改的Info.plist:


  • 如果您的应用程序依赖于位置服务正常运行,请添加位置服务 UIRequiredDeviceCapabilities

  • 如果您的应用需要GPS硬件,添加 GPS UIRequiredDeviceCapabilities

  • 如果你需要运行你的应用程序不再那么的背景下10分钟,加入位置 UIBackgroundModes 。然后你的位置管理器将提供超过10分钟的极限位置。

  • 您还应该设置 NSLocationUsageDescription (也可以本地化)


  

获取位置活动的背景


  
  

如果您的应用程序需要的位置更新交付的应用程序是否在
  前景或背景,有这样做的多个选项。该
  preferred选项是使用显著位置更改服务
  唤醒在适当的时间应用程序来处理新的事件。然而,如果
  您的应用需要使用标准定位服务,你可以声明
  您的应用程序需要作为背景的​​定位服务。


  
  

这是应用程序应该要求的背景位置服务只有在缺席
  这些服务将削弱其运营的能力。此外,
  请求后台位置服务应该使用那些任何应用程序
  服务提供了实惠给用户。例如,一个
  转由turn导航的应用程序将是背景可能的候选人
  因为其需要跟踪该用户的位置的定位服务和
  当它的时间,使下一回合的报告。


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?

Implementing Long-Running Background Tasks

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

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

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

----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.

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

  • 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)

Getting Location Events in the Background

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应用为超过10分钟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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