博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
GCD之后台程序运行
阅读量:6954 次
发布时间:2019-06-27

本文共 2451 字,大约阅读时间需要 8 分钟。

点击Home键进入后台时进行计时,直到从新启动,超过三分钟启动手势

////  AppDelegate.m//  GCDDown////  Created by City--Online on 15/4/21.//  Copyright (c) 2015年 CYW. All rights reserved.// #import "AppDelegate.h" @interface AppDelegate ()@property (assign, nonatomic) UIBackgroundTaskIdentifier backgroundUpdateTask;@property(nonatomic,assign)NSInteger count;@property(nonatomic,strong)NSTimer *timer; @end @implementation AppDelegate  - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    // Override point for customization after application launch.    return YES;} - (void)applicationWillResignActive:(UIApplication *)application {    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.} - (void)applicationDidEnterBackground:(UIApplication *)application {    NSLog(@"进入后台");    [self beingBackgroundUpdateTask];    // 在这里加上你需要长久运行的代码    self.timer=[NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(updatecount) userInfo:nil repeats:YES];    [self.timer fire];    [self endBackgroundUpdateTask];}-(void)updatecount{    self.count++;}- (void)beingBackgroundUpdateTask{    self.backgroundUpdateTask = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{        [self endBackgroundUpdateTask];    }];} - (void)endBackgroundUpdateTask{    [[UIApplication sharedApplication] endBackgroundTask: self.backgroundUpdateTask];    self.backgroundUpdateTask = UIBackgroundTaskInvalid;}- (void)applicationWillEnterForeground:(UIApplication *)application {    NSLog(@"进入前台");    NSLog(@"%ld",self.count);    self.count=0;    [self.timer invalidate];     } - (void)applicationDidBecomeActive:(UIApplication *)application {    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.} - (void)applicationWillTerminate:(UIApplication *)application {    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.} @end

转载地址:http://ofnil.baihongyu.com/

你可能感兴趣的文章
true or false in JavaScript
查看>>
Android学习笔记6:使用Intent1
查看>>
js实现继承的几种方式
查看>>
[LintCode/LeetCode] Two Strings are Anagrams/Valid Anagram
查看>>
Consul入门03 - 注册服务
查看>>
[Centos]necessary tools for newbie
查看>>
前端临床手札——单元测试
查看>>
Java IO : File
查看>>
MySQL Replication
查看>>
JavaScript数组去重总结
查看>>
MVVM_Android-CleanArchitecture
查看>>
iOS开发-协议Protocol&代理delegate
查看>>
【系统架构师修炼之道】(4):绪论——Zachman 框架
查看>>
Foxify v0.10.7 发布,基于 TypeScript 的 Node 框架
查看>>
Python数据结构——双端队列
查看>>
GitHub 项目推荐:用深度学习让你的照片变得美丽 ...
查看>>
另类文件加密 图片当密码给文本加密
查看>>
MySQL数据库如何解决大数据量存储问题
查看>>
CENTOS6.5 yum配置
查看>>
《自顶向下网络设计(第3版)》——1.6 复习题
查看>>