GiGaMaskTime/GIGA/AppDelegate.m

160 lines
6.1 KiB
Objective-C
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//
// AppDelegate.m
// GIGA
//
// Created by lianxiang on 2018/8/13.
// Copyright © 2018年 com.giga.ios. All rights reserved.
//
#import "AppDelegate.h"
#import "AppDelegate+GiGaConfig.h"
#import "AppDelegate+ThirdParty.h"
#import "GiGaUserDefault.h"
#import "JPUSHService.h"
#import "GiGaLocalNotificationManager.h"
#import "GiGaBaseAPiRequest.h"
@interface AppDelegate ()
//保持后台长时间运行
@property (nonatomic,assign) UIBackgroundTaskIdentifier backgroundTask;
@property (nonatomic,strong) NSTimer *timer;
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
//[NSThread sleepForTimeInterval:1];//延迟启动
_isMasking = NO;
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
[self registLocalNotification];
[self configThridPartyWithOptions:launchOptions];
[self configEnvironment];
[self setupRootVC];
//广告页
[self creatAdView];
//[self setupTabBarController];
//启动同步用户数据
BOOL islogin = [GiGaUserDefault isUserLogin];
if (islogin) {
[[GiGaUserManager shareUser] synsisUserInfo:nil userErrorMsgBlock:^(NSDictionary *errorCodemsg) {
if ([errorCodemsg[@"code"] integerValue] == 401) {
[[GiGaUserManager shareUser] loginOut];
NC_POST_NAME_OBJECT(kUserLogOutNotify, nil);
//token失效
[GiGaBaseAPiRequest userTokenTimeOutGologinFromVC:self.window.rootViewController];
}
}];
}else{
// 未登录同步默认面膜时间
GiGaBaseAPiRequest *api = [GiGaBaseAPiRequest initWithRequestPath:kUserDefaultMaskTime method:RequestPostMethod parms:nil];
[api requstDataWithResult:^(GiGaAPIResult *result) {
if (result.success) {
float minute = [result.dic[@"minute"] floatValue];
if (minute !=0) {
[GiGaUserDefault savaMaskeTime:minute*60];
}
}
}];
}
//用户关闭后台权限 提醒开启
if ([[UIApplication sharedApplication] backgroundRefreshStatus] != UIBackgroundRefreshStatusAvailable ) {
[self.window.rootViewController jxt_showAlertWithTitle:@"温馨提示" message:@"为保证您正常使用面膜时间,需要您开启后台刷新,请在 设置->面膜时间->应用程序后台刷新 开启" appearanceProcess:^(JXTAlertController * _Nonnull alertMaker) {
alertMaker.addActionCancelTitle(@"下次");
alertMaker.addActionDefaultTitle(@"去开启");
} actionsBlock:^(NSInteger buttonIndex, UIAlertAction * _Nonnull action, JXTAlertController * _Nonnull alertSelf) {
if (buttonIndex == 1) {
NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
if ([[UIApplication sharedApplication] canOpenURL:url]) {
[[UIApplication sharedApplication] openURL:url];
}
}
}];
}
return YES;
}
- (void)applicationWillResignActive:(UIApplication *)application {
}
- (void)applicationDidEnterBackground:(UIApplication *)application {
// 当 开启面膜时间 app长期保持后台模式
if (_isMasking) {
[self creatBackGoundTask];
}
}
-(void)creatBackGoundTask{
//告诉系统当前app在后台有任务处理需要时间
self.backgroundTask = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
[[UIApplication sharedApplication] endBackgroundTask:self.backgroundTask];
self.backgroundTask = UIBackgroundTaskInvalid;
}];
self.timer = [NSTimer scheduledTimerWithTimeInterval:30.0 target:self selector:@selector(applyToActiveForMoreTime) userInfo:nil repeats:YES];
[self.timer fire];
}
-(void)applyToActiveForMoreTime{
//如果系统给的剩余时间小于60秒 就终止当前的后台任务再重新初始化一个后台任务重新让系统分配时间这样一直循环下去保持APP在后台一直处于active状态。
if ([UIApplication sharedApplication].backgroundTimeRemaining < 60.0) {
{//如果剩余时间小于60秒
[[UIApplication sharedApplication] endBackgroundTask:self.backgroundTask];
self.backgroundTask = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
[[UIApplication sharedApplication] endBackgroundTask:self.backgroundTask];
self.backgroundTask = UIBackgroundTaskInvalid;
}];
}
}
}
- (void)applicationWillEnterForeground:(UIApplication *)application {
application.applicationIconBadgeNumber = 0;
[JPUSHService resetBadge];
}
- (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:.
//app 挂起 面膜时间未结束 发送本地通知提醒
if (_isMasking) {
// [[GiGaLocalNotificationManager localNotifiationCenter] sendLocalNotification:@"闹钟提醒!" fireTimeInterval:2 alertAction:@"面膜时间已添加闹钟提醒!" withIdentifier:kLOCALNotifiID_APPTERMINAL];
//进程结束 长期保持后台前提下 用户主动挂起 取消本地推送。
// [[GiGaLocalNotificationManager localNotifiationCenter] cancelAllLocalNoitification];
}
[[GiGaLocalNotificationManager localNotifiationCenter] cancelAllLocalNoitification];
}
-(void)remoteControlReceivedWithEvent:(UIEvent *)event
{
if (event.type == UIEventTypeRemoteControl ) {
}
}
@end