I am trying to allow my application to run in the background for more than 10 minutes, in accordance with this and my good below. (I want to use a long background to track location, my code here just uses a counter for testing purposes) Can anyone point out what the problem is?
Realization of long-term background tasks
For tasks that require longer execution time, you must request specific permissions to run them in the background without pausing them. On iOS, only certain types of applications are allowed to run in the background:
Applications that play audio content to the user in the background, such as a music player application
Applications that constantly inform users of their location, such as a navigation application
Voice over Internet Protocol (VoIP) Applications
Press apps that need to download and process new content. Applications that receive regular updates from external accessories
Applications implementing these services should declare the services they support and use the system framework to implement the relevant aspects of these services. The announcement of the services allows the system to know which ones you are using, but in some cases the system ones actually prohibit the suspension of your application.
- (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 launch an iOS application in the background?
---- , 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:^{
[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]; } }