AppDelegate.m 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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. //
  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. if(self.supportScreenRotateType){
  113. return UIInterfaceOrientationMaskAllButUpsideDown;
  114. }
  115. return UIInterfaceOrientationMaskPortrait;
  116. }
  117. #pragma mark - UISceneSession lifecycle
  118. - (UISceneConfiguration *)application:(UIApplication *)application configurationForConnectingSceneSession:(UISceneSession *)connectingSceneSession options:(UISceneConnectionOptions *)options {
  119. // Called when a new scene session is being created.
  120. // Use this method to select a configuration to create the new scene with.
  121. return [[UISceneConfiguration alloc] initWithName:@"Default Configuration" sessionRole:connectingSceneSession.role];
  122. }
  123. - (void)application:(UIApplication *)application didDiscardSceneSessions:(NSSet<UISceneSession *> *)sceneSessions {
  124. // Called when the user discards a scene session.
  125. // If any sessions were discarded while the application was not running, this will be called shortly after application:didFinishLaunchingWithOptions.
  126. // Use this method to release any resources that were specific to the discarded scenes, as they will not return.
  127. }
  128. - (void)applicationWillEnterForeground:(UIApplication *)application {
  129. // 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.
  130. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.4 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  131. NSString *orderNumber = [HWDataManager getStringWithKey:Const_AirpayOrWXorderNum];
  132. if (orderNumber && ![orderNumber isEqualToString:@""])
  133. {
  134. [[NSNotificationCenter defaultCenter] postNotificationName:NotNameAirpayOrWXorderNum object:orderNumber];
  135. }
  136. });
  137. }
  138. - (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {
  139. return [WXApi handleOpenURL:url delegate:self];
  140. }
  141. - (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {
  142. if (YES == [TencentOAuth CanHandleOpenURL:url])
  143. {
  144. return [TencentOAuth HandleOpenURL:url];
  145. }
  146. return [WXApi handleOpenURL:url delegate:self];
  147. }
  148. - (BOOL)application:(UIApplication *)application continueUserActivity:(NSUserActivity *)userActivity restorationHandler:(void (^)(NSArray<id<UIUserActivityRestoring>> * _Nullable))restorationHandler
  149. {
  150. if([userActivity.activityType isEqualToString:NSUserActivityTypeBrowsingWeb]) {
  151. NSURL *url = userActivity.webpageURL;
  152. if(url && [TencentOAuth CanHandleUniversalLink:url]) {
  153. UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"CI UniversalLink" message:url.description delegate:nil cancelButtonTitle:@"ok" otherButtonTitles:nil, nil];
  154. [alertView show];
  155. //[QQApiInterface handleOpenUniversallink:url delegate:(id<QQApiInterfaceDelegate>)[QQApiShareEntry class]];
  156. return [TencentOAuth HandleUniversalLink:url];
  157. }
  158. }
  159. return [WXApi handleOpenUniversalLink:userActivity delegate:self];
  160. }
  161. #pragma mark 监听网络变化
  162. -(void)MonitorNetworkChangesFun
  163. {
  164. [[AFNetworkReachabilityManager sharedManager] setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
  165. HLog(@"Reachability: %@", AFStringFromNetworkReachabilityStatus(status));
  166. [[NSNotificationCenter defaultCenter] postNotificationName:NetWorkChangeNotification object:nil];
  167. }];
  168. [[AFNetworkReachabilityManager sharedManager] startMonitoring];
  169. }
  170. #pragma mark 多国语言相关
  171. -(void)setLanguagesFun{
  172. NSArray *arLanguages = [[NSUserDefaults standardUserDefaults] objectForKey:@"AppleLanguages"];
  173. NSLog(@"arLanguages:%@",arLanguages);
  174. ///获取设备当前地区的代码和APP语言环境
  175. NSString *languageCode = [NSLocale preferredLanguages][0];
  176. // 获取国际通用国家地区代码(应该和手机本身有关)
  177. NSString *countryCode = [NSString stringWithFormat:@"-%@", [[NSLocale currentLocale] objectForKey:NSLocaleCountryCode]];
  178. // if (languageCode) {
  179. // languageCode = [languageCode stringByReplacingOccurrencesOfString:countryCode withString:@""];
  180. // }
  181. NSLog(@"countryCode:%@ languageCode : %@",countryCode, languageCode);
  182. ///当前APP使用的语言
  183. NSString *preferredLanguage = [[[NSBundle mainBundle] preferredLocalizations] firstObject];
  184. //获取设备当前地区的代码和APP语言环境
  185. NSString *localeIdentifier = [[NSLocale currentLocale] objectForKey:NSLocaleIdentifier];
  186. NSLog(@"preferredLanguage:%@ localeIdentifier : %@",preferredLanguage, localeIdentifier);
  187. //目前支持 中文(简体 繁体) 英文 日语(还没做)
  188. if([languageCode rangeOfString:preferredLanguage].location != NSNotFound){
  189. // if([preferredLanguage rangeOfString:@"ja"].location != NSNotFound){
  190. // //日文现在也显示英文
  191. // [self setDefaultEnglishFun];
  192. // }
  193. // else
  194. {
  195. [DDYLanguageTool ddy_SetLanguage:@"" complete:^(NSError *error) {
  196. // 刷新rootVC等
  197. }];
  198. }
  199. }
  200. else{
  201. [self setDefaultEnglishFun];
  202. }
  203. }
  204. #pragma mark 设置当前显示语言为中文
  205. - (void)setDefaultEnglishFun{
  206. //默认设为英文
  207. [DDYLanguageTool ddy_SetLanguage:@"en" complete:^(NSError *error) {
  208. // 刷新rootVC等
  209. }];
  210. }
  211. - (void)batteryLevelChanged:(NSNotification *)notification {
  212. // 获取当前设备的电池电量
  213. UIDevice *device = notification.object;
  214. float batteryLevel = device.batteryLevel;
  215. // 根据电量级别执行相应的操作
  216. if (batteryLevel < 0.2) {
  217. NSLog(@"Low battery level. Please charge the device.");
  218. } else if (batteryLevel < 0.5) {
  219. NSLog(@"Medium battery level.");
  220. } else {
  221. NSLog(@"High battery level.");
  222. }
  223. }
  224. - (void)handleCrashException:(NSString*)exceptionMessage exceptionCategory:(JJExceptionGuardCategory)exceptionCategory extraInfo:(nullable NSDictionary*)info{
  225. NSMutableString *totalLogstr = [NSMutableString new];
  226. if(exceptionMessage){
  227. [totalLogstr appendString:exceptionMessage];
  228. [totalLogstr appendString:@"\n\n\n"];
  229. }
  230. if(info){
  231. NSData *jsonData = [NSJSONSerialization dataWithJSONObject:info options:info error:nil];
  232. if(jsonData && jsonData.length>0){
  233. NSString *jsonStr = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
  234. if(jsonStr){
  235. [totalLogstr appendString:jsonStr];
  236. }
  237. }
  238. }
  239. //写日志
  240. if(totalLogstr && totalLogstr.length >0){
  241. [cachesFileManager writeCrashLogsWithMsg:totalLogstr];
  242. }
  243. }
  244. - (NSString*)NASShareFileService
  245. {
  246. //http://transfer.armclouding.com:10012/getFile?path=/sdcard/Download/mmexport1712039794930_4124382.png
  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