在后台运行iOS App超过10分钟

Ahm*_*mad 5 background objective-c background-process ios

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

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

对于需要执行更多执行时间的任务,您必须请求特定权限才能在后台运行它们而不会被挂起.在iOS中,只允许在后台运行特定的应用类型:

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

随时向用户通知其位置的应用,例如导航应用

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

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

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

   - (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];
        }
        }
Run Code Online (Sandbox Code Playgroud)

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

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

- (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]; } }
Run Code Online (Sandbox Code Playgroud)

Fel*_*lix 4

因此您的应用程序使用位置服务。那么请阅读位置感知编程指南

\n\n

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

\n\n
    \n
  • 如果您的应用依赖位置服务才能正常运行,请location-services添加UIRequiredDeviceCapabilities
  • \n
  • 如果您的应用程序需要 GPS 硬件,请添加gpsUIRequiredDeviceCapabilities
  • \n
  • 如果您需要在后台运行应用程序超过 10 分钟,请添加locationUIBackgroundModes. 然后,您的位置经理将在 10 分钟限制之外提供位置。
  • \n
  • 你还应该设置NSLocationUsageDescription(也可以本地化)
  • \n
\n\n
\n

在后台获取位置事件

\n\n

如果您的应用需要传递位置更新,无论该应用是在前台还是后台,\n 有多种选项可以实现此目的。首选选项是使用重要位置更改服务在适当的时间唤醒您的应用程序以处理新事件。但是,如果您的应用需要使用标准位置服务,您可以将您的应用声明为需要后台位置服务。

\n\n

仅当缺少后台位置服务会损害其运行能力时,应用才应请求这些服务。此外,任何请求后台位置服务的应用都应使用这些服务为用户提供切实的好处。例如,逐向导航应用程序可能是后台位置服务的候选者,因为它需要跟踪用户\xe2\x80\x99 的位置并\n 报告何时进行导航。下一个回合。

\n
\n