AppDelegate.m 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. //
  2. // AppDelegate.m
  3. // 唔即云相册
  4. //
  5. // Created by 余衡武 on 2021/12/8.
  6. //
  7. #import "AppDelegate.h"
  8. #import <CoreMotion/CoreMotion.h>
  9. #import "ShearDeviceUDPManager.h"
  10. #import "AudioSessionObject.h"
  11. #import "DDYLanguageTool.h"
  12. #import "AFNetworkReachabilityManager.h"
  13. #import "PLeakSniffer.h"
  14. #import "connectDeviceManager.h"
  15. #import <Bugly/Bugly.h>
  16. @interface AppDelegate (){
  17. CMMotionManager *cmManager;
  18. }
  19. @end
  20. @implementation AppDelegate
  21. + (AppDelegate*)sharedAppDelegate
  22. {
  23. static AppDelegate *appDelegate = nil;
  24. static dispatch_once_t onceToken;
  25. dispatch_once(&onceToken, ^{
  26. if (appDelegate == nil) {
  27. appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  28. }
  29. });
  30. return appDelegate;
  31. }
  32. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
  33. [self setLanguagesFun];
  34. //设置默认值
  35. // self.couldPhone_W_PHONE = 720.0;
  36. // self.couldPhone_H_PHONE = 1280.0;
  37. self.couldPhone_W_PHONE = 1080.0;
  38. self.couldPhone_H_PHONE = 1920.0;
  39. [[UIButton appearance] setExclusiveTouch:YES];
  40. cmManager = [[CMMotionManager alloc] init];
  41. if (cmManager.isAccelerometerAvailable){
  42. HLog(@"\n-------摇一摇可用-----");
  43. }else{
  44. HLog(@"\n-------摇一摇不可用-----");
  45. }
  46. cmManager.accelerometerUpdateInterval = 0.1;
  47. [cmManager startAccelerometerUpdates];
  48. [cmManager startDeviceMotionUpdatesToQueue:[NSOperationQueue new] withHandler:^(CMDeviceMotion * _Nullable motion, NSError * _Nullable error) {
  49. // CMRotationRate rotationRate = motion.rotationRate;
  50. // CGFloat rotationRatex = rotationRate.x;
  51. // CGFloat rotationRatey = rotationRate.y;
  52. // CGFloat rotationRatez = rotationRate.z;
  53. CMAcceleration accrleration = motion.gravity;
  54. CGFloat rotationGravityz = accrleration.z;
  55. //HLog(@"rotationRatey: %f",rotationRatey)
  56. //HLog(@"rotationGravityz: %f",rotationGravityz)
  57. //if (rotationRatey > 7){
  58. if (rotationGravityz > 0.85){
  59. BOOL haveOpenMask = [HWDataManager getBoolWithKey:Consn_Fanzhuan_Exit_app_Open];
  60. BOOL isPrivacyMode = [connectDeviceManager shareInstance].DeviceThirdIdMod.data.isPrivacyMode;
  61. if (haveOpenMask && isPrivacyMode){
  62. exit(0);/*强制退出app*/
  63. }
  64. }
  65. }];
  66. // [[ShearDeviceUDPManager shareInstance] startShearchDevice];
  67. // [[ShearDeviceUDPManager shareInstance] shearchDeviceLoop];
  68. //启动后台保活
  69. [AudioSessionObject shareManager];
  70. [self MonitorNetworkChangesFun];
  71. [[PLeakSniffer sharedInstance] installLeakSniffer];
  72. [Bugly startWithAppId:@"179559a521"];
  73. [[UIDevice currentDevice] setBatteryMonitoringEnabled:YES];
  74. [[UIDevice currentDevice] batteryLevel];
  75. // 监听电池电量变化通知
  76. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(batteryLevelChanged:) name:UIDeviceBatteryLevelDidChangeNotification object:nil];
  77. return YES;
  78. }
  79. /// 在这里写支持的旋转方向,为了防止横屏方向,应用启动时候界面变为横屏模式
  80. - (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {
  81. return UIInterfaceOrientationMaskPortrait;
  82. }
  83. #pragma mark - UISceneSession lifecycle
  84. - (UISceneConfiguration *)application:(UIApplication *)application configurationForConnectingSceneSession:(UISceneSession *)connectingSceneSession options:(UISceneConnectionOptions *)options {
  85. // Called when a new scene session is being created.
  86. // Use this method to select a configuration to create the new scene with.
  87. return [[UISceneConfiguration alloc] initWithName:@"Default Configuration" sessionRole:connectingSceneSession.role];
  88. }
  89. - (void)application:(UIApplication *)application didDiscardSceneSessions:(NSSet<UISceneSession *> *)sceneSessions {
  90. // Called when the user discards a scene session.
  91. // If any sessions were discarded while the application was not running, this will be called shortly after application:didFinishLaunchingWithOptions.
  92. // Use this method to release any resources that were specific to the discarded scenes, as they will not return.
  93. }
  94. - (void)applicationWillEnterForeground:(UIApplication *)application {
  95. // 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.
  96. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.4 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  97. NSString *orderNumber = [HWDataManager getStringWithKey:Const_AirpayOrWXorderNum];
  98. if (orderNumber && ![orderNumber isEqualToString:@""])
  99. {
  100. [[NSNotificationCenter defaultCenter] postNotificationName:NotNameAirpayOrWXorderNum object:orderNumber];
  101. }
  102. });
  103. }
  104. #pragma mark 监听网络变化
  105. -(void)MonitorNetworkChangesFun
  106. {
  107. [[AFNetworkReachabilityManager sharedManager] setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
  108. HLog(@"Reachability: %@", AFStringFromNetworkReachabilityStatus(status));
  109. [[NSNotificationCenter defaultCenter] postNotificationName:NetWorkChangeNotification object:nil];
  110. }];
  111. [[AFNetworkReachabilityManager sharedManager] startMonitoring];
  112. }
  113. #pragma mark 多国语言相关
  114. -(void)setLanguagesFun{
  115. NSArray *arLanguages = [[NSUserDefaults standardUserDefaults] objectForKey:@"AppleLanguages"];
  116. NSLog(@"arLanguages:%@",arLanguages);
  117. ///获取设备当前地区的代码和APP语言环境
  118. NSString *languageCode = [NSLocale preferredLanguages][0];
  119. // 获取国际通用国家地区代码(应该和手机本身有关)
  120. NSString *countryCode = [NSString stringWithFormat:@"-%@", [[NSLocale currentLocale] objectForKey:NSLocaleCountryCode]];
  121. // if (languageCode) {
  122. // languageCode = [languageCode stringByReplacingOccurrencesOfString:countryCode withString:@""];
  123. // }
  124. NSLog(@"countryCode:%@ languageCode : %@",countryCode, languageCode);
  125. ///当前APP使用的语言
  126. NSString *preferredLanguage = [[[NSBundle mainBundle] preferredLocalizations] firstObject];
  127. //获取设备当前地区的代码和APP语言环境
  128. NSString *localeIdentifier = [[NSLocale currentLocale] objectForKey:NSLocaleIdentifier];
  129. NSLog(@"preferredLanguage:%@ localeIdentifier : %@",preferredLanguage, localeIdentifier);
  130. //目前支持 中文(简体 繁体) 英文 日语(还没做)
  131. if([languageCode rangeOfString:preferredLanguage].location != NSNotFound){
  132. // if([preferredLanguage rangeOfString:@"ja"].location != NSNotFound){
  133. // //日文现在也显示英文
  134. // [self setDefaultEnglishFun];
  135. // }
  136. // else
  137. {
  138. [DDYLanguageTool ddy_SetLanguage:@"" complete:^(NSError *error) {
  139. // 刷新rootVC等
  140. }];
  141. }
  142. }
  143. else{
  144. [self setDefaultEnglishFun];
  145. }
  146. }
  147. #pragma mark 设置当前显示语言为中文
  148. - (void)setDefaultEnglishFun{
  149. //默认设为英文
  150. [DDYLanguageTool ddy_SetLanguage:@"en" complete:^(NSError *error) {
  151. // 刷新rootVC等
  152. }];
  153. }
  154. - (void)batteryLevelChanged:(NSNotification *)notification {
  155. // 获取当前设备的电池电量
  156. UIDevice *device = notification.object;
  157. float batteryLevel = device.batteryLevel;
  158. // 根据电量级别执行相应的操作
  159. if (batteryLevel < 0.2) {
  160. NSLog(@"Low battery level. Please charge the device.");
  161. } else if (batteryLevel < 0.5) {
  162. NSLog(@"Medium battery level.");
  163. } else {
  164. NSLog(@"High battery level.");
  165. }
  166. }
  167. @end