AppDelegate.m 6.7 KB

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