Translate

顯示具有 CMMotionManager 標籤的文章。 顯示所有文章
顯示具有 CMMotionManager 標籤的文章。 顯示所有文章

2014年2月27日 星期四

CMMotionManager in the background

首先在TARGETS的Capabilities的Background Modes把Location updates打勾
然後再

- (void)applicationDidEnterBackground:(UIApplication *)application { // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. NSLog(@"applicationDidEnterBackground"); UIApplication *app = [UIApplication sharedApplication]; __block UIBackgroundTaskIdentifier bgTask = [app beginBackgroundTaskWithExpirationHandler:^{ dispatch_async(dispatch_get_main_queue(), ^{ if (bgTask != UIBackgroundTaskInvalid) { [app endBackgroundTask:bgTask]; bgTask = UIBackgroundTaskInvalid; } }); }]; dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ // testTimer = [NSTimer scheduledTimerWithTimeInterval:(1.0/60.0) target:self selector:@selector(doSomeTest) userInfo:nil repeats:YES]; testTimer = [NSTimer scheduledTimerWithTimeInterval:(10.0) target:self selector:@selector(doSomeTest) userInfo:nil repeats:YES]; [testTimer fire]; [[NSRunLoop currentRunLoop] addTimer:testTimer forMode:NSRunLoopCommonModes]; [[NSRunLoop currentRunLoop] run]; dispatch_async(dispatch_get_main_queue(), ^{ if (bgTask != UIBackgroundTaskInvalid) { [app endBackgroundTask:bgTask]; bgTask = UIBackgroundTaskInvalid; } }); }); } -(void)doSomeTest{ }