AppDelegate.m 14 KB

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