2014-10-11 59 views
0

具体来说,我正着眼于后台应用程序收到位置服务更新(重大位置更改)的情况。在这种情况下,背景会发生什么?任何用户代码都可以运行,包括待定计时器吗?NSTimer *上的选择器可以在后台执行吗?

+0

我不这么认为,我觉得只有将被调用外景经理的委托方法,但你为什么不试试吧?测试应该很简单 – Paulw11 2014-10-11 20:03:27

+0

那么你能继续吗? – bllakjakk 2014-10-12 08:46:40

+0

@bllakjakk在下面的链接是我的主要错误,我问这个问题试图了解可能造成它的原因。 http://salesforce.stackexchange.com/questions/51272/sfcrypto-keychain-data-missing-or-corrupted – 2014-10-12 14:07:29

回答

0

你可以像下面使用后台任务和定时器:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
    { 
     // Override point for customization after application launch. 
     self.locationManager = [[CLLocationManager alloc] init]; 
     self.locationManager.delegate = self; 

     [self.locationManager startUpdatingLocation]; 
     return YES; 
    } 

- (void)applicationDidEnterBackground:(UIApplication *)application 
{ 

    self.bgTask = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{ 


     [[UIApplication sharedApplication] endBackgroundTask:self.bgTask]; 
     self.bgTask = UIBackgroundTaskInvalid; 
    }]; 


    self.timer = [NSTimer scheduledTimerWithTimeInterval:60 
                target:self 
               selector:@selector(changeAccuracy) 
               userInfo:nil 
               repeats:YES]; 
} 


- (void) changeAccuracy { 
    [self.locationManager setDesiredAccuracy:kCLLocationAccuracyBest]; 
    [self.locationManager setDistanceFilter:kCLDistanceFilterNone]; 
} 

-(void)locationManager:(CLLocationManager *)lm didUpdateLocations:(NSArray *)locations{ 
    CLLocation *location = [locations lastObject]; 

    NSLog(@"Location returned: %f, %f Accuracy: %f", location.coordinate.latitude, location.coordinate.longitude, location.horizontalAccuracy); 
    [lm setDesiredAccuracy:kCLLocationAccuracyThreeKilometers]; 
    [lm setDistanceFilter:99999]; 
} 
+0

谢谢,但我的问题不是如何实现它与后台任务。我的问题是,定时服务和定时定时器是否会发生(定时器触发)? – 2014-10-12 14:12:24

+0

肯定是的。 Apple允许在后台使用某些特定服务。位置就是其中之一。它可以使用后台任务和计时器来实现。它在https://developer.apple.com/library/ios/documentation/iphone/conceptual/iPhoneOSProgrammingGuide/BackgroundExecution/BackgroundExecution.html上有记录 – bllakjakk 2014-10-12 17:22:30