Newer
Older
XinYang_IOS / XYSW / AppDelegate.m
@zhangfeng zhangfeng on 7 Dec 2023 10 KB 1.8.0
//
//  AppDelegate.m
//  XYSW
//
//  Created by Jim on 2021/9/7.
//

#import "AppDelegate.h"
#import "ViewController.h"
#import <MOBFoundation/MobSDK.h>
#import <MOBFoundation/MobSDK+Privacy.h>
#import <MobPush/MobPush.h>
#import "MonitorNetworkStatus.h"
#import "YHBaseNavigationController.h"
#import "LoginViewController.h"
#import "WKWebViewController.h"
#import <AMapFoundationKit/AMapServices.h>
#import <PgySDK/PgyManager.h>
#import <PgyUpdate/PgyUpdateManager.h>

#import <Bugly/Bugly.h>
@interface AppDelegate ()

@end

@implementation AppDelegate

+ (AppDelegate *)shareAppDelegate{
    return (AppDelegate *)[[UIApplication sharedApplication]delegate];
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
   
    
    
    if (Development == 1) {
        //启动基本SDK
        [[PgyManager sharedPgyManager] startManagerWithAppId:@"42cc07e6f943f2d61996c0c64737879b"];
        [[PgyUpdateManager sharedPgyManager] startManagerWithAppId:@"42cc07e6f943f2d61996c0c64737879b"];   // 请将 PGY_APP_ID 换成应用的 App Key
        [[PgyUpdateManager sharedPgyManager] checkUpdate]; //根据build的值来判断
        // 设置用户反馈界面激活方式为三指拖动
        [[PgyManager sharedPgyManager] setFeedbackActiveType:kPGYFeedbackActiveTypeThreeFingersPan];

        // 设置用户反馈界面激活方式为摇一摇
        [[PgyManager sharedPgyManager] setFeedbackActiveType:kPGYFeedbackActiveTypeShake];
    }else{
        [[PgyManager sharedPgyManager] startManagerWithAppId:@"42cc07e6f943f2d61996c0c64737879b"];
        [[PgyUpdateManager sharedPgyManager] startManagerWithAppId:@"42cc07e6f943f2d61996c0c64737879b"];   // 请将 PGY_APP_ID 换成应用的 App Key
        [[PgyUpdateManager sharedPgyManager] checkUpdate]; //根据build的值来判断
        [PgyManager sharedPgyManager].enableFeedback =NO; //关闭用户手势反馈功能
       
    }
    
    //崩溃监控
    //appid : 5695779cbf  App Key : b1b0f6d5-0f57-481d-9bc3-d947ca87f6fb
    [Bugly startWithAppId:@"c640eb30f8"];
    
    //配置高德 Key 前,添加开启 HTTPS 功能的代码,如下
    [[AMapServices sharedServices] setEnableHTTPS:YES];
    [AMapServices sharedServices].apiKey =@"0eeb297a0ff62044b3c1610fba88d209";
    
   //监听网络状态
   [[MonitorNetworkStatus sharedManager] YHProject_startNotifyWithDelegate:self];
   
    //注册推送通知
   [self regiesterMoBPush:launchOptions];
   
    if (LocalTest) {
        WKWebViewController *vc = [[WKWebViewController alloc] init];
        
        self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
        self.window.backgroundColor = [UIColor whiteColor];
        ZXNavigationBarNavigationController *nav = [[ZXNavigationBarNavigationController alloc]initWithRootViewController:vc];
        [self.window setRootViewController:nav];
        [self.window makeKeyAndVisible];
        
        
    //    ViewController *mainVC = [[ViewController alloc] init];
//        [[AppDelegate shareAppDelegate].navigationCtrl pushViewController:mainVC animated:NO];
//        [AppDelegate shareAppDelegate].navigationCtrl.viewControllers = @[mainVC];
        
//        [AppDelegate  startMainViewController];
    }else{
    //    判断进主页还是进登录
       if ([[[NSUserDefaults standardUserDefaults] objectForKey:@"login"] intValue] == 1) {
           [AppDelegate  startMainViewController];
       }else{
           LoginViewController *vc = [[LoginViewController alloc] init];
           self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
           self.window.backgroundColor = [UIColor whiteColor];
           ZXNavigationBarNavigationController *nav = [[ZXNavigationBarNavigationController alloc]initWithRootViewController:vc];
           
           [self.window setRootViewController:nav];
           [self.window makeKeyAndVisible];
//           LoginViewController *loginVC = [[LoginViewController alloc] init];
//           [[AppDelegate shareAppDelegate].navigationCtrl pushViewController:loginVC animated:NO];
//           [AppDelegate shareAppDelegate].navigationCtrl.viewControllers = @[loginVC];
           
//           [AppDelegate  startLoginViewController];
       }
    }
    

    return YES;
}

+(void)startLoginViewController{
    LoginViewController *loginVC = [[LoginViewController alloc] init];
    ZXNavigationBarNavigationController *nav = [[ZXNavigationBarNavigationController alloc]initWithRootViewController:loginVC];
    
    [[AppDelegate shareAppDelegate].window setRootViewController:nav];
    
//    [[AppDelegate shareAppDelegate].navigationCtrl pushViewController:loginVC animated:NO];
//    [AppDelegate shareAppDelegate].navigationCtrl.viewControllers = @[loginVC];
    
}

+(void)startMainViewController{
    WKWebViewController *mainVC = [[WKWebViewController alloc] init];
    ZXNavigationBarNavigationController *nav = [[ZXNavigationBarNavigationController alloc]initWithRootViewController:mainVC];
    
    [[AppDelegate shareAppDelegate].window setRootViewController:nav];
//    ViewController *mainVC = [[ViewController alloc] init];
//    [[AppDelegate shareAppDelegate].navigationCtrl pushViewController:mainVC animated:NO];
//    [AppDelegate shareAppDelegate].navigationCtrl.viewControllers = @[mainVC];
}


+(void)startTestViewController{
//    WKWebViewController *mainVC = [[WKWebViewController alloc] init];
    ViewController *mainVC = [[ViewController alloc] init];
    [[AppDelegate shareAppDelegate].navigationCtrl pushViewController:mainVC animated:NO];
    [AppDelegate shareAppDelegate].navigationCtrl.viewControllers = @[mainVC];
}
#pragma mark --- 注册推送
-(void)regiesterMoBPush:(NSDictionary *)dic{
    // 注意:上传隐私协议接口,具体查看官方文档(http://www.mob.com/wiki/detailed?wiki=MobTechprivacypushios&id=136)
    [MobSDK uploadPrivacyPermissionStatus:YES onResult:^(BOOL success) {
        NSLog(@"-------------->上传结果:%d",success);
    }];
    
    // 设置推送环境
//#ifdef DEBUG
//    [MobPush setAPNsForProduction:NO];
//#else
    [MobPush setAPNsForProduction:YES];
//#endif
    
    [MobSDK registerAppKey:KMBPushAppKey appSecret:KMBPushAppSecret];
    
    //MobPush推送设置(获得角标、声音、弹框提醒权限)
    MPushNotificationConfiguration *configuration = [[MPushNotificationConfiguration alloc] init];
    configuration.types = MPushAuthorizationOptionsBadge | MPushAuthorizationOptionsSound | MPushAuthorizationOptionsAlert;
    [MobPush setupNotification:configuration];
    
    [MobPush getRegistrationID:^(NSString *registrationID, NSError *error) {
        NSLog(@"registrationID = %@--error = %@", registrationID, error);
    }];
    
    // 监听推送通知 MobPush整合了系统iOS10以上及以下不同方式获取推送统一通过此监听获取,开发者通过原始方式亦不影响。
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didReceiveMessage:) name:MobPushDidReceiveMessageNotification object:nil];
}

- (void)applicationWillEnterForeground:(UIApplication *)application
{
    //程序进入前台时,清除角标,但不清空通知栏消息(开发者根据业务需求,自行调用)
    //注意:不建议在进入后台通知(applicationDidEnterBackground:)中调用此方法,原因进入后台将角标清空结果无法通过网络同步到服务器
    [MobPush clearBadge];
}

- (void)applicationDidEnterBackground:(UIApplication *)application
{
//    LoginViewController *vc = (LoginViewController *)[AppDelegate shareAppDelegate].navigationCtrl.viewControllers.firstObject;
//    [vc.providerManager.connection stopVPNTunnel];
}
- (void)applicationWillTerminate:(UIApplication *)application{
    //发送通知关闭VPN
//    LoginViewController *vc = (LoginViewController *)[AppDelegate shareAppDelegate].navigationCtrl.viewControllers.firstObject;
//    [vc.providerManager.connection stopVPNTunnel];
//    [[NSNotificationCenter defaultCenter] postNotificationName:@"applicationWillTerminate" object:nil userInfo:nil];
//    NSLog(@"applicationWillTerminate");
    
}

// 收到推送通知回调
- (void)didReceiveMessage:(NSNotification *)notification
{
    MPushMessage *message = notification.object;
    
    NSLog(@"message:%@", message.convertDictionary);
    
    switch (message.messageType)
    {
        case MPushMessageTypeCustom:
        {// 自定义消息回调
           
        }
            break;
        case MPushMessageTypeAPNs:
        {// APNs回调
            
        }
            break;
        case MPushMessageTypeLocal:
        {// 本地通知回调
            NSString *body = message.notification.body;
            NSString *title = message.notification.title;
            NSString *subtitle = message.notification.subTitle;
            NSInteger badge = message.notification.badge;
            NSString *sound = message.notification.sound;
            NSLog(@"收到本地通知:{\nbody:%@,\ntitle:%@,\nsubtitle:%@,\nbadge:%ld,\nsound:%@,\n}",body, title, subtitle, (long)badge, sound);
        }
            break;
        case MPushMessageTypeClicked:
        {// 点击通知回调
            
            // 测试
            if ([UIApplication sharedApplication].applicationState == UIApplicationStateActive)
            { // 前台
              
            }
            else
            { // 后台
               
            }
        }
        default:
            break;
    }
}


#pragma mark - NetworkStatusNotifyManagerDelegate
- (void)networkStatusNotifyManagerStatusChanged:(BOOL)reachability {
    
    _networkStatus = reachability ? NetWorkStatusReachability : NetWorkStatusNotReachability;
    NSLog(@"网络状态===%@",_networkStatus? @"有网络" : @"无网络");
    
}

#pragma mark --- GET方法
- (UINavigationController *)navigationCtrl{
    
    if (!_navigationCtrl) {
        _navigationCtrl = [ZXNavigationBarNavigationController new];
//        _navigationCtrl = [[YHBaseNavigationController alloc]init];
//        [_navigationCtrl setNavigationBarHidden:YES];
        
        DLog(@"%@",_navigationCtrl);
    }
    return _navigationCtrl;
    
}


@end