AppDelegate.m 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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. #import <JJException/JJException.h>
  17. @interface AppDelegate ()<JJExceptionHandle>
  18. {
  19. CMMotionManager *cmManager;
  20. }
  21. @end
  22. @implementation AppDelegate
  23. + (AppDelegate*)sharedAppDelegate
  24. {
  25. static AppDelegate *appDelegate = nil;
  26. static dispatch_once_t onceToken;
  27. dispatch_once(&onceToken, ^{
  28. if (appDelegate == nil) {
  29. appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  30. }
  31. });
  32. return appDelegate;
  33. }
  34. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
  35. //保护App,一般常见的问题不会导致闪退,增强App的健壮性,同时会将错误抛出来,根据每个App自身的日志渠道记录
  36. [JJException configExceptionCategory:JJExceptionGuardAll];
  37. [JJException startGuardException];
  38. [JJException registerExceptionHandle:self];
  39. //Default value:NO no表示异常不退出 YES 表示退出 开发时应该设置为YES
  40. //JJException.exceptionWhenTerminate = YES;
  41. //test code
  42. // NSArray * arr = @[];
  43. // NSString *str = arr[2];
  44. [self setLanguagesFun];
  45. //设置默认值
  46. // self.couldPhone_W_PHONE = 720.0;
  47. // self.couldPhone_H_PHONE = 1280.0;
  48. self.couldPhone_W_PHONE = 1080.0;
  49. self.couldPhone_H_PHONE = 1920.0;
  50. [[UIButton appearance] setExclusiveTouch:YES];
  51. cmManager = [[CMMotionManager alloc] init];
  52. if (cmManager.isAccelerometerAvailable){
  53. HLog(@"\n-------摇一摇可用-----");
  54. }else{
  55. HLog(@"\n-------摇一摇不可用-----");
  56. }
  57. cmManager.accelerometerUpdateInterval = 0.1;
  58. [cmManager startAccelerometerUpdates];
  59. [cmManager startDeviceMotionUpdatesToQueue:[NSOperationQueue new] withHandler:^(CMDeviceMotion * _Nullable motion, NSError * _Nullable error) {
  60. // CMRotationRate rotationRate = motion.rotationRate;
  61. // CGFloat rotationRatex = rotationRate.x;
  62. // CGFloat rotationRatey = rotationRate.y;
  63. // CGFloat rotationRatez = rotationRate.z;
  64. CMAcceleration accrleration = motion.gravity;
  65. CGFloat rotationGravityz = accrleration.z;
  66. //HLog(@"rotationRatey: %f",rotationRatey)
  67. //HLog(@"rotationGravityz: %f",rotationGravityz)
  68. //if (rotationRatey > 7){
  69. if (rotationGravityz > 0.85){
  70. BOOL haveOpenMask = [HWDataManager getBoolWithKey:Consn_Fanzhuan_Exit_app_Open];
  71. BOOL isPrivacyMode = [connectDeviceManager shareInstance].DeviceThirdIdMod.data.isPrivacyMode;
  72. if (haveOpenMask && isPrivacyMode){
  73. exit(0);/*强制退出app*/
  74. }
  75. }
  76. }];
  77. // [[ShearDeviceUDPManager shareInstance] startShearchDevice];
  78. // [[ShearDeviceUDPManager shareInstance] shearchDeviceLoop];
  79. //启动后台保活
  80. [AudioSessionObject shareManager];
  81. [self MonitorNetworkChangesFun];
  82. #ifdef DEBUG
  83. [[PLeakSniffer sharedInstance] installLeakSniffer];
  84. #else
  85. [Bugly startWithAppId:@"179559a521"];
  86. #endif
  87. [SVProgressHUD setDefaultStyle:(SVProgressHUDStyleDark)];
  88. [[UIDevice currentDevice] setBatteryMonitoringEnabled:YES];
  89. [[UIDevice currentDevice] batteryLevel];
  90. // 监听电池电量变化通知
  91. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(batteryLevelChanged:) name:UIDeviceBatteryLevelDidChangeNotification object:nil];
  92. return YES;
  93. }
  94. /// 在这里写支持的旋转方向,为了防止横屏方向,应用启动时候界面变为横屏模式
  95. - (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {
  96. return UIInterfaceOrientationMaskPortrait;
  97. }
  98. #pragma mark - UISceneSession lifecycle
  99. - (UISceneConfiguration *)application:(UIApplication *)application configurationForConnectingSceneSession:(UISceneSession *)connectingSceneSession options:(UISceneConnectionOptions *)options {
  100. // Called when a new scene session is being created.
  101. // Use this method to select a configuration to create the new scene with.
  102. return [[UISceneConfiguration alloc] initWithName:@"Default Configuration" sessionRole:connectingSceneSession.role];
  103. }
  104. - (void)application:(UIApplication *)application didDiscardSceneSessions:(NSSet<UISceneSession *> *)sceneSessions {
  105. // Called when the user discards a scene session.
  106. // If any sessions were discarded while the application was not running, this will be called shortly after application:didFinishLaunchingWithOptions.
  107. // Use this method to release any resources that were specific to the discarded scenes, as they will not return.
  108. }
  109. - (void)applicationWillEnterForeground:(UIApplication *)application {
  110. // 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.
  111. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.4 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  112. NSString *orderNumber = [HWDataManager getStringWithKey:Const_AirpayOrWXorderNum];
  113. if (orderNumber && ![orderNumber isEqualToString:@""])
  114. {
  115. [[NSNotificationCenter defaultCenter] postNotificationName:NotNameAirpayOrWXorderNum object:orderNumber];
  116. }
  117. });
  118. }
  119. #pragma mark 监听网络变化
  120. -(void)MonitorNetworkChangesFun
  121. {
  122. [[AFNetworkReachabilityManager sharedManager] setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
  123. HLog(@"Reachability: %@", AFStringFromNetworkReachabilityStatus(status));
  124. [[NSNotificationCenter defaultCenter] postNotificationName:NetWorkChangeNotification object:nil];
  125. }];
  126. [[AFNetworkReachabilityManager sharedManager] startMonitoring];
  127. }
  128. #pragma mark 多国语言相关
  129. -(void)setLanguagesFun{
  130. NSArray *arLanguages = [[NSUserDefaults standardUserDefaults] objectForKey:@"AppleLanguages"];
  131. NSLog(@"arLanguages:%@",arLanguages);
  132. ///获取设备当前地区的代码和APP语言环境
  133. NSString *languageCode = [NSLocale preferredLanguages][0];
  134. // 获取国际通用国家地区代码(应该和手机本身有关)
  135. NSString *countryCode = [NSString stringWithFormat:@"-%@", [[NSLocale currentLocale] objectForKey:NSLocaleCountryCode]];
  136. // if (languageCode) {
  137. // languageCode = [languageCode stringByReplacingOccurrencesOfString:countryCode withString:@""];
  138. // }
  139. NSLog(@"countryCode:%@ languageCode : %@",countryCode, languageCode);
  140. ///当前APP使用的语言
  141. NSString *preferredLanguage = [[[NSBundle mainBundle] preferredLocalizations] firstObject];
  142. //获取设备当前地区的代码和APP语言环境
  143. NSString *localeIdentifier = [[NSLocale currentLocale] objectForKey:NSLocaleIdentifier];
  144. NSLog(@"preferredLanguage:%@ localeIdentifier : %@",preferredLanguage, localeIdentifier);
  145. //目前支持 中文(简体 繁体) 英文 日语(还没做)
  146. if([languageCode rangeOfString:preferredLanguage].location != NSNotFound){
  147. // if([preferredLanguage rangeOfString:@"ja"].location != NSNotFound){
  148. // //日文现在也显示英文
  149. // [self setDefaultEnglishFun];
  150. // }
  151. // else
  152. {
  153. [DDYLanguageTool ddy_SetLanguage:@"" complete:^(NSError *error) {
  154. // 刷新rootVC等
  155. }];
  156. }
  157. }
  158. else{
  159. [self setDefaultEnglishFun];
  160. }
  161. }
  162. #pragma mark 设置当前显示语言为中文
  163. - (void)setDefaultEnglishFun{
  164. //默认设为英文
  165. [DDYLanguageTool ddy_SetLanguage:@"en" complete:^(NSError *error) {
  166. // 刷新rootVC等
  167. }];
  168. }
  169. - (void)batteryLevelChanged:(NSNotification *)notification {
  170. // 获取当前设备的电池电量
  171. UIDevice *device = notification.object;
  172. float batteryLevel = device.batteryLevel;
  173. // 根据电量级别执行相应的操作
  174. if (batteryLevel < 0.2) {
  175. NSLog(@"Low battery level. Please charge the device.");
  176. } else if (batteryLevel < 0.5) {
  177. NSLog(@"Medium battery level.");
  178. } else {
  179. NSLog(@"High battery level.");
  180. }
  181. }
  182. - (void)handleCrashException:(NSString*)exceptionMessage exceptionCategory:(JJExceptionGuardCategory)exceptionCategory extraInfo:(nullable NSDictionary*)info{
  183. NSMutableString *totalLogstr = [NSMutableString new];
  184. if(exceptionMessage){
  185. [totalLogstr appendString:exceptionMessage];
  186. [totalLogstr appendString:@"\n\n\n"];
  187. }
  188. if(info){
  189. NSData *jsonData = [NSJSONSerialization dataWithJSONObject:info options:info error:nil];
  190. if(jsonData && jsonData.length>0){
  191. NSString *jsonStr = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
  192. if(jsonStr){
  193. [totalLogstr appendString:jsonStr];
  194. }
  195. }
  196. }
  197. //写日志
  198. if(totalLogstr && totalLogstr.length >0){
  199. [cachesFileManager writeCrashLogsWithMsg:totalLogstr];
  200. }
  201. }
  202. @end