AppDelegate.m 14 KB

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