ios - 后台运行 iOS App 超过 10 分钟

标签 ios objective-c background background-process

我正在尝试让我的应用程序在后台运行超过 10 分钟,根据这个和我下面的好处。 (我想使用长时间的后台运行来跟踪位置,我这里的代码只是使用一个计数器来进行测试) 谁能帮忙指出是什么问题?

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

另一个问题:我可以让 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]; } }

最佳答案

因此您的应用使用定位服务。那么请阅读 Location Awareness Programming Guide .

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

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

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 - 后台运行 iOS App 超过 10 分钟,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16031120/

相关文章:

css - 如果 css 背景图像在以后的 css 中被覆盖,它仍然会被调用吗?

javascript - 如何在这里正确使用 isNaN?

ios - 来自移动应用程序的 REST API - 使用验证码保护第一次通话是否有意义?

ios - 从命令行终端使用位码启用和位码禁用为设备和通用配置创建 iOS 框架

ios - 在 Swift 4 中垂直调整 .xib(单元格)的大小

objective-c - NSDictionary objectForKey 返回 nil 值

ios - Phonegap 在 IOS 后台使用地理定位

ios - Xcode 签名有效,但 faSTLane 无效

php - iPhone 中的 RESTful Web 服务 : Data not inserted to database

iphone - 如何在ipad中获取指定宽度和高度的popover frame