AppDelegate.m 14 KB

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