AppDelegate.m 9.9 KB

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