- (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.
[[NSNotificationCenter defaultCenter]postNotificationName:UIApplicationDidEnterBackgroundNotification object:nil];
}
- (void)applicationWillEnterForeground:(UIApplication *)application {
// Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
[[NSNotificationCenter defaultCenter]postNotificationName:UIApplicationWillEnterForegroundNotification object:nil];
}
// // YTHandlerEnterBackground.h // 分时租赁 // // Created by 柯其谱 on 17/2/24. // Copyright © 2017年 柯其谱. All rights reserved. // #import <Foundation/Foundation.h> #import <UIKit/UIKit.h> /** 进入后台block typedef */ typedef void(^YTHandlerEnterBackgroundBlock)(NSNotification * _Nonnull note, NSTimeInterval stayBackgroundTime); /** 处理进入后台并计算留在后台时间间隔类 */ @interface YTHandlerEnterBackground : NSObject /** 添加观察者并处理后台 */ + (void)addObserverUsingBlock:(nullable YTHandlerEnterBackgroundBlock)block; /** 移除后台观察者 */ + (void)removeNotificationObserver:(nullable id)observer; @end
//
// YTHandlerEnterBackground.m
// 分时租赁
//
// Created by 柯其谱 on 17/2/24.
// Copyright © 2017年 柯其谱. All rights reserved.
//
#import "YTHandlerEnterBackground.h"
@implementation YTHandlerEnterBackground
+ (void)addObserverUsingBlock:(YTHandlerEnterBackgroundBlock)block {
__block CFAbsoluteTime enterBackgroundTime;
[[NSNotificationCenter defaultCenter]addObserverForName:UIApplicationDidEnterBackgroundNotification object:nil queue:nil usingBlock:^(NSNotification * _Nonnull note) {
if (![note.object isKindOfClass:[UIApplication class]]) {
enterBackgroundTime = CFAbsoluteTimeGetCurrent();
}
}];
__block CFAbsoluteTime enterForegroundTime;
[[NSNotificationCenter defaultCenter]addObserverForName:UIApplicationWillEnterForegroundNotification object:nil queue:nil usingBlock:^(NSNotification * _Nonnull note) {
if (![note.object isKindOfClass:[UIApplication class]]) {
enterForegroundTime = CFAbsoluteTimeGetCurrent();
CFAbsoluteTime timeInterval = enterForegroundTime-enterBackgroundTime;
block? block(note, timeInterval): nil;
}
}];
}
+ (void)removeNotificationObserver:(id)observer {
if (!observer) {
return;
}
[[NSNotificationCenter defaultCenter]removeObserver:observer name:UIApplicationDidEnterBackgroundNotification object:nil];
[[NSNotificationCenter defaultCenter]removeObserver:observer name:UIApplicationWillEnterForegroundNotification object:nil];
}
@end
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[YTHandlerEnterBackground addObserverUsingBlock:^(NSNotification * _Nonnull note, NSTimeInterval stayBackgroundTime) {
self.rentTimerInterval = self.rentTimerInterval-stayBackgroundTime;
}];
}
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
[self.timer invalidate];
[YTHandlerEnterBackground removeNotificationObserver:self];
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
self.locService.delegate = self;
[self.locService startUserLocationService];
//进入后台再进入前台重新开始定位
[YTHandlerEnterBackground addObserverUsingBlock:^(NSNotification * _Nonnull note, NSTimeInterval stayBackgroundTime) {
[self.locService startUserLocationService];
}];
}
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
//停止定位
self.locService.delegate = nil;
[self.locService stopUserLocationService];
//移除后台监听
[YTHandlerEnterBackground removeNotificationObserver:self];
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有