AppDelegate.m 14 KB

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