Translate

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{ }

2014年2月4日 星期二

轉帖 The official raywenderlich.com Objective-C style guide

The official raywenderlich.com Objective-C style guide

Singletons

Singleton objects should use a thread-safe pattern for creating their shared instance.
+ (instancetype)sharedInstance { static id sharedInstance = nil; static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ sharedInstance = [[self alloc] init]; }); return sharedInstance; }

轉帖google analytics iOS

google-analytics-ios