AppDelegate.m 6.5 KB

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