今天接到一起找工作的同学的电话诉苦,本来他还是在那种iOS培训机构培训过,其实我还真看不起那培训机构,还在一家公司干过有过两个app上过线。今天面试可能遇到那种传说中的技术总监类的人物吧,直接被问道无语,同学确实做东西不用心记,好的IDE确实能加快工程进度和提高代码质量,但对于初学者来说容易让他产生依赖,一问一些关键的技术就答不出来了,连些用啥类也说不出来,最要命的是人家认定他干的没啥技术含量,如果让我看也许也是这么认为的,这就是说平时的学习工作一定要消化知识,知其然也知其所以然。电话中人家提到了多任务的知识,这个其实对于我来说也是盲区,今天就查了查《iOS5 Programminig CookBook》找到了Multitasking这一章,看了一下,确实挺费力的,先把今天的做的贴出来吧,做个记录:
先是检测本设备支不支持多任务:
- (BOOL) isMultitaskingSupported{
BOOL result = NO;
if ([[UIDevice currentDevice]
respondsToSelector:@selector(isMultitaskingSupported)]){
result = [[UIDevice currentDevice] isMultitaskingSupported];
}
return result;
}
- (BOOL) application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
if ([self isMultitaskingSupported]){ NSLog(@"Multitasking is supported.");
} else {
NSLog(@"Multitasking is not supported.");
}
self.window = [[UIWindow alloc] initWithFrame: [[UIScreen mainScreen] bounds]];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}
下一节这个内容我有些困难,不是百分百调出来,不知是作者的代码有问题,功能是在用户按了home按钮后程序在后台执行:
- (void) timerMethod:(NSTimer *)paramSender{
NSTimeInterval backgroundTimeRemaining =
[[UIApplication sharedApplication] backgroundTimeRemaining];
if (backgroundTimeRemaining == DBL_MAX){
NSLog(@"Background Time Remaining = Undetermined");
} else {
NSLog(@"Background Time Remaining = %.02f Seconds",
backgroundTimeRemaining);
}
}
- (void)applicationDidEnterBackground:(UIApplication *)application{
if ([self isMultitaskingSupported] == NO){
return;
}
self.myTimer =
[NSTimer scheduledTimerWithTimeInterval:1.0f
target:self selector:@selector(timerMethod:) userInfo:nil
repeats:YES];
self.backgroundTaskIdentifier =
[application beginBackgroundTaskWithExpirationHandler:^(void) {
[self endBackgroundTask]; }];
}
- (void) endBackgroundTask{ dispatch_queue_t mainQueue = dispatch_get_main_queue(); __weak Completing_a_Long_Running_Task_in_the_BackgroundAppDelegate *weakSelf = self; dispatch_async(mainQueue, ^(void) { Completing_a_Long_Running_Task_in_the_BackgroundAppDelegate *strongSelf = weakSelf; if (strongSelf != nil){ [strongSelf.myTimer invalidate]; [[UIApplication sharedApplication] endBackgroundTask:self.backgroundTaskIdentifier]; strongSelf.backgroundTaskIdentifier = UIBackgroundTaskInvalid; }
}); }
当程序切到后台时就在控制台一秒输出个Background Time Remaining = %.02f Seconds,但是方法endBackgroundTask:一直报错,是里面这个__weak声明的类型报错,这个__weak我也不知道是个什么东西,真是惭愧啊,再学学吧。今天就抛下这个问题了,明天解决一下~