iOSAPP后台持续运行

释放双眼,带上耳机,听听看~!
//1.设置定时器
@implementation AppDelegate
{
    NSTimer* _timer;
}
//2.app进入后台  启动定时器
- (void)applicationWillEnterForeground:(UIApplication *)application {
    [_timersetFireDate:[NSDatedistantFuture]];
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}
//3.后台运行函数
-(void)logAction
{
    NSLog(@"在后台运行");
}
- (void)applicationDidEnterBackground:(UIApplication *)application {
    _timer =  [NSTimerscheduledTimerWithTimeInterval:1.0ftarget:selfselector:@selector(logAction)userInfo:nilrepeats:YES];
    [[NSRunLoopcurrentRunLoop] addTimer:_timerforMode:NSDefaultRunLoopMode];
    UIApplication*   app = [UIApplicationsharedApplication];
    __block   UIBackgroundTaskIdentifier bgTask;
    bgTask = [app beginBackgroundTaskWithExpirationHandler:^{
        dispatch_async(dispatch_get_main_queue(), ^{
            if (bgTask != UIBackgroundTaskInvalid){
                bgTask = UIBackgroundTaskInvalid;
            }
        });
    }];
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0), ^{
        dispatch_async(dispatch_get_main_queue(), ^{
            if (bgTask != UIBackgroundTaskInvalid){
                bgTask = UIBackgroundTaskInvalid;
            }
        });
    });
 
    // 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.
}

 

人已赞赏
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
有新消息 消息中心
搜索