SceneDelegate.m 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531
  1. //
  2. // SceneDelegate.m
  3. // 唔即云相册
  4. //
  5. // Created by 余衡武 on 2021/12/8.
  6. //
  7. #import "SceneDelegate.h"
  8. #import "HWWebViewController.h"
  9. #import "CalculatorViewController.h"
  10. #import "SetUsePWDViewController.h"
  11. #import "GuideViewController.h"
  12. #import "BaseNavigationController.h"
  13. #import "MySetViewController.h"
  14. #import "SafeForKey.h"
  15. #import "TipsQRCodeViewController.h"
  16. #import "SetPWDFirstViewController.h"
  17. #import "ConnectTestViewController.h"
  18. #import "PlayerViewController.h"
  19. #import "connectDeviceManager.h"
  20. #import "inputPWDViewController.h"
  21. #import "customLaunchView.h"
  22. #import "RSATool.h"
  23. @interface SceneDelegate ()<SetUsePWDViewControllerDelegate,GuideViewControllerDelegate>
  24. @property(nonatomic, strong) CalculatorViewController *calculatorVC;
  25. @property(nonatomic, strong) HWWebViewController *webVC;
  26. @property(nonatomic, strong) inputPWDViewController *inputVC;
  27. @property(nonatomic, strong) NSTimer *checkSNDataTimer;//
  28. @property(nonatomic, strong) customLaunchView *customLaunchV;//
  29. @property(nonatomic, assign) bool isQRCodeType;//第一次 扫码进来的
  30. @property (nonatomic, assign) BOOL isLoginAgainType;// 单点登录 点重新登录进来的
  31. @end
  32. @implementation SceneDelegate
  33. - (void)scene:(UIScene *)scene willConnectToSession:(UISceneSession *)session options:(UISceneConnectionOptions *)connectionOptions API_AVAILABLE(ios(13.0)){
  34. if (scene) {
  35. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(guideOk:) name:GuideOkNotification object:nil];
  36. UIWindowScene *windowScene = (UIWindowScene *)scene;
  37. self.window = [[UIWindow alloc] initWithWindowScene:windowScene];
  38. self.window.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height);
  39. // 初始化数据库
  40. [self initBrowserWindow];
  41. // 加载主页面
  42. [self enterMainVCFromScene];
  43. //
  44. [self getNetWorkPopViewFun];
  45. [self.window makeKeyAndVisible];
  46. }
  47. }
  48. /** 进入登录界面 - 内部使用 */
  49. - (void)enterLoginVCFromScene {
  50. // UIViewController *vc = [[UIViewController alloc] init];
  51. // vc.view.backgroundColor = [UIColor redColor];
  52. // UINavigationController *nvc = [[UINavigationController alloc] initWithRootViewController:vc];
  53. // self.window.rootViewController = nvc;
  54. }
  55. /** 主界面 - 内部使用 */
  56. - (void)enterMainVCFromScene {
  57. // ConnectTestViewController *qrCodeVCNav = [[ConnectTestViewController alloc] init];
  58. // self.window.rootViewController = qrCodeVCNav;
  59. // return;/*临时测试*/
  60. /*先判断本地有无设备 无设备时需要先扫码添加设备*/
  61. NSDictionary *deviceDict = [HWDataManager getObjectWithKey:Const_Have_Add_Device];
  62. if (!deviceDict || ![[deviceDict allKeys] containsObject:Const_Have_Add_Device_SN]){
  63. /*扫码界面*/
  64. TipsQRCodeViewController *qrCodeVC = [[TipsQRCodeViewController alloc] init];
  65. BaseNavigationController *qrCodeVCNav = [[BaseNavigationController alloc] initWithRootViewController:qrCodeVC];
  66. self.window.rootViewController = qrCodeVCNav;
  67. _isQRCodeType = YES;
  68. return;
  69. }
  70. //有设备了先去做链接准备 // 80bec9c5
  71. NSString *SNStr = deviceDict[@"Const_Have_Add_Device_SN"];
  72. NSString *sdnId = [connectDeviceManager shareInstance].DeviceThirdIdMod.data.sdnId;
  73. if(SNStr && !sdnId){
  74. [[connectDeviceManager shareInstance] getThridMsgBySN:SNStr didNetEnd:^(bool didSuc) {
  75. }];
  76. }
  77. // GuideViewController
  78. BOOL haveGuide = [HWDataManager getBoolWithKey:Const_Have_Show_Guide];
  79. if (haveGuide) {
  80. ;
  81. }else {
  82. GuideViewController *guiDeVC = [[GuideViewController alloc] init];
  83. guiDeVC.delegate = self;
  84. guiDeVC.sn = SNStr;
  85. self.window.rootViewController = guiDeVC;
  86. return;
  87. }
  88. // if (![[deviceDict allKeys] containsObject:Const_Have_Add_Device_PWD]){
  89. // /*没有设置密码*/
  90. // SetPWDFirstViewController *qrCodeVC = [[SetPWDFirstViewController alloc] init];
  91. // BaseNavigationController *qrCodeVCNav = [[BaseNavigationController alloc] initWithRootViewController:qrCodeVC];
  92. // self.window.rootViewController = qrCodeVCNav;
  93. //
  94. // return;
  95. // }
  96. //确保通过SN号拿到密码了
  97. //非扫码进入 进入到这里很可能还没联网拿到设备最新信息
  98. if(![connectDeviceManager shareInstance].DeviceThirdIdMod){
  99. if(!_checkSNDataTimer){
  100. _checkSNDataTimer = [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(enterMainVCFromScene) userInfo:nil repeats:YES];
  101. BOOL haveOpenMask = [HWDataManager getBoolWithKey:Const_Mask_View_Open];
  102. //添加默认启动图片
  103. if(!_customLaunchV && !haveOpenMask){
  104. _customLaunchV = [[customLaunchView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_W, SCREEN_H)];
  105. [self.window addSubview:_customLaunchV];
  106. }
  107. }
  108. return;
  109. }
  110. if(_checkSNDataTimer){
  111. [_checkSNDataTimer invalidate];
  112. [_customLaunchV removeFromSuperview];
  113. _customLaunchV = nil;
  114. }
  115. //是否已经有密码了 有就是输入密码 没有就是设置密码
  116. NSString *curPwd = [connectDeviceManager shareInstance].DeviceThirdIdMod.data.password;
  117. sdnId = [connectDeviceManager shareInstance].DeviceThirdIdMod.data.sdnId;
  118. if(!sdnId){
  119. HLog(@"没有拿到sdnId");
  120. [[connectDeviceManager shareInstance] getThridMsgBySN:SNStr didNetEnd:^(bool didSuc) {
  121. }];
  122. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  123. mainBlock(^{
  124. [self enterMainVCFromScene];
  125. });
  126. });
  127. return;
  128. }
  129. if(curPwd && curPwd.length > 0){
  130. /*设置密码*/
  131. // inputPWDViewController *nextVC = [[inputPWDViewController alloc] init];
  132. // BaseNavigationController *nextVCNav = [[BaseNavigationController alloc] initWithRootViewController:nextVC];
  133. // self.window.rootViewController = nextVCNav;
  134. }
  135. else{
  136. /*没有设置密码*/
  137. SetPWDFirstViewController *qrCodeVC = [[SetPWDFirstViewController alloc] init];
  138. BaseNavigationController *qrCodeVCNav = [[BaseNavigationController alloc] initWithRootViewController:qrCodeVC];
  139. self.window.rootViewController = qrCodeVCNav;
  140. return;
  141. }
  142. //每次进入
  143. PlayerViewController *playerRootVC = [[PlayerViewController alloc] init];
  144. if(self.isLoginAgainType){
  145. playerRootVC.isLoginAgainType = self.isLoginAgainType;
  146. }
  147. playerRootVC.isPwdVCShow = YES;
  148. BaseNavigationController *playerNav = [[BaseNavigationController alloc] initWithRootViewController:playerRootVC];
  149. self.window.rootViewController = playerNav;
  150. self.isLoginAgainType = NO;
  151. if(!_isQRCodeType){
  152. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  153. mainBlock(^{
  154. [playerRootVC setShowImgAndVoiceTypeFun:NO];
  155. });
  156. });
  157. [self showCalculatorVC];
  158. _isQRCodeType = NO;
  159. }
  160. // MySetViewController *playerRootVC = [[MySetViewController alloc] init];
  161. // BaseNavigationController *playerNav = [[BaseNavigationController alloc] initWithRootViewController:playerRootVC];
  162. // self.window.rootViewController = playerNav;
  163. }
  164. - (void)setPwdOk{
  165. [self enterMainVCFromScene];
  166. // NSArray *dataArray = [BaseModel bg_findAll:DB_BrowserWindows_TableName];
  167. //
  168. // if (dataArray.count == 0) { // 浏览器没有窗口
  169. // // 加载首页
  170. // HWWebViewController *vc = [[HWWebViewController alloc] init];
  171. // vc.webUrl = Const_HomeUrl;
  172. // UINavigationController *nvc = [[UINavigationController alloc] initWithRootViewController:vc];
  173. // self.window.rootViewController = nvc;
  174. //
  175. // }else {
  176. // // 浏览器当前窗口索引ID
  177. // NSInteger ID = [HWDataManager getIntegerWithKey:BrowserWindowsCurrentID];
  178. // NSString *where = [NSString stringWithFormat:@"where %@=%ld",bg_sqlKey(@"ID"),(long)ID];
  179. // NSArray* finfAlls = [BaseModel bg_find:DB_BrowserWindows_TableName where:where];
  180. // BaseModel *model = finfAlls.firstObject;
  181. //
  182. // // 加载网页
  183. // HWWebViewController *vc = [[HWWebViewController alloc] init];
  184. // vc.webUrl = model.webUrl;
  185. // UINavigationController *nvc = [[UINavigationController alloc] initWithRootViewController:vc];
  186. // self.window.rootViewController = nvc;
  187. // }
  188. }
  189. /** 登录界面 - 外部使用*/
  190. + (void)enterLoginVC {
  191. // SetPWDViewController *vc = [[SetPWDViewController alloc] init];
  192. // UINavigationController *nvc = [[UINavigationController alloc] initWithRootViewController:vc];
  193. // UIWindow *keyWindow = [iTools getKeyWindow];
  194. // keyWindow.rootViewController = nvc;
  195. }
  196. /** 主界面 - 外部使用 */
  197. + (void)enterMainVC {
  198. // AlbumHomeViewController *vc = [[AlbumHomeViewController alloc] init];
  199. // UINavigationController *nvc = [[UINavigationController alloc] initWithRootViewController:vc];
  200. // UIWindow *keyWindow = [iTools getKeyWindow];
  201. // keyWindow.rootViewController = nvc;
  202. }
  203. - (void)sceneDidDisconnect:(UIScene *)scene API_AVAILABLE(ios(13.0)){
  204. }
  205. - (void)sceneDidBecomeActive:(UIScene *)scene API_AVAILABLE(ios(13.0)){
  206. BOOL haveGuide = [HWDataManager getBoolWithKey:Const_Have_Show_Guide];
  207. if (haveGuide) {
  208. ;
  209. }else {
  210. return;
  211. }
  212. [self showCalculatorVC];
  213. }
  214. - (void)sceneWillResignActive:(UIScene *)scene API_AVAILABLE(ios(13.0)){
  215. BOOL haveGuide = [HWDataManager getBoolWithKey:Const_Have_Show_Guide];
  216. if (haveGuide) {
  217. ;
  218. }else {
  219. return;
  220. }
  221. [self showCalculatorVC];
  222. }
  223. - (void)sceneWillEnterForeground:(UIScene *)scene API_AVAILABLE(ios(13.0)){
  224. }
  225. - (void)sceneDidEnterBackground:(UIScene *)scene API_AVAILABLE(ios(13.0)){
  226. BOOL haveGuide = [HWDataManager getBoolWithKey:Const_Have_Show_Guide];
  227. if (haveGuide) {
  228. ;
  229. }else {
  230. return;
  231. }
  232. [self showCalculatorVC];
  233. }
  234. #pragma mark 初始化数据库-DB_BrowserWindows_TableName
  235. - (void)initBrowserWindow {
  236. // 浏览器当前所有窗口
  237. NSArray *dataArray = [BaseModel bg_findAll:DB_BrowserWindows_TableName];
  238. // 浏览器当前窗口索引ID
  239. NSInteger ID = [HWDataManager getIntegerWithKey:BrowserWindowsCurrentID];
  240. NSString *IDValue = [NSString stringWithFormat:@"%ld", ID];
  241. NSString *where = [NSString stringWithFormat:@"where %@=%@",bg_sqlKey(@"ID"),bg_sqlValue(IDValue)];
  242. NSArray *modelArr = [BaseModel bg_find:DB_BrowserWindows_TableName where:where];
  243. if (dataArray.count == 0) { // 浏览器没有窗口
  244. BaseModel *model = [[BaseModel alloc] init];
  245. model.ID = 0;
  246. model.name = @"主页";
  247. // model.iconFile = imageUrl;
  248. model.webUrl = Const_HomeUrl;
  249. // 写入数据库
  250. model.bg_tableName = DB_BrowserWindows_TableName;
  251. [model bg_saveOrUpdateAsync:^(BOOL isSuccess) {
  252. HLog(@"BaseModel写入:%@", isSuccess ? @"成功":@"失败");
  253. }];
  254. // 浏览器当前窗口索引ID
  255. [HWDataManager setIntegerWithKey:BrowserWindowsCurrentID value:0];
  256. }else if (modelArr.count == 0) { // 新建窗口
  257. BaseModel *model = [[BaseModel alloc] init];
  258. model.ID = ID;
  259. model.name = @"主页";
  260. // model.iconFile = imageUrl;
  261. model.webUrl = Const_HomeUrl;
  262. // 写入数据库
  263. model.bg_tableName = DB_BrowserWindows_TableName;
  264. [model bg_saveOrUpdateAsync:^(BOOL isSuccess) {
  265. HLog(@"BaseModel写入:%@", isSuccess ? @"成功":@"失败");
  266. }];
  267. }else { // 更新窗口数据
  268. // 浏览器当前窗口索引ID
  269. // NSInteger ID = [HWDataManager getIntegerWithKey:BrowserWindowsCurrentID];
  270. //
  271. // BaseModel *model = [[BaseModel alloc] init];
  272. // model.ID = ID;
  273. // model.name = @"主页";
  274. //// model.iconFile = imageUrl;
  275. // model.webUrl = Const_HomeUrl;
  276. //
  277. // // 更新数据库
  278. // model.bg_tableName = DB_BrowserWindows_TableName;
  279. // [model bg_saveOrUpdateAsync:^(BOOL isSuccess) {
  280. // HLog(@"BaseModel更新: %@", isSuccess ? @"成功":@"失败");
  281. // }];
  282. }
  283. }
  284. #pragma mark 获取当前屏幕的截图
  285. //- (UIImage *)getScreenShotImage {
  286. // CGSize size = [UIScreen mainScreen].bounds.size;
  287. // CGFloat scale = [UIScreen mainScreen].scale;
  288. // UIGraphicsBeginImageContextWithOptions(size, YES, scale);
  289. // [self.window.layer renderInContext:UIGraphicsGetCurrentContext()];
  290. // UIImage * image = UIGraphicsGetImageFromCurrentImageContext();
  291. // UIGraphicsEndImageContext();
  292. // return image;
  293. //}
  294. #pragma mark 计算器伪装视图
  295. /*CalculatorViewController*/
  296. - (void)showCalculatorVC{
  297. /*无有效时长直接返回不加载任何加密界面*/
  298. NSString *str = nil;
  299. NSDictionary *deviceDict = [HWDataManager getObjectWithKey:Const_Have_Add_Device];
  300. //本地密码补在判断 密码改在服务器保持
  301. if ([[deviceDict allKeys] containsObject:Const_Have_Add_Device_PWD]){
  302. str = [deviceDict objectForKey:Const_Have_Add_Device_PWD];
  303. }
  304. if (!str || [str isEqualToString:@""])
  305. {
  306. return;
  307. }
  308. // BOOL haveOpenMask = [HWDataManager getBoolWithKey:Const_Mask_View_Open];
  309. // if (!haveOpenMask){
  310. // //输入密码界面
  311. // [self closeCalculatorVC];
  312. // _inputVC = [[inputPWDViewController alloc] init];
  313. // [self.window addSubview:_inputVC.view];
  314. // return;
  315. // }
  316. BOOL haveVaildTime = [HWDataManager getBoolWithKey:Const_Have_No_VaildTime];
  317. if (haveVaildTime == YES){/*无有效时间*/
  318. return;
  319. }
  320. NSInteger maskModel = [HWDataManager getIntegerWithKey:Const_Mask_View_Model];
  321. if (maskModel == 0){
  322. //输入密码界面
  323. [self closeCalculatorVC];
  324. _inputVC = [[inputPWDViewController alloc] init];
  325. [self.window addSubview:_inputVC.view];
  326. }
  327. else if (maskModel == 2){
  328. /*浏览器*/
  329. [self closeCalculatorVC];
  330. _webVC = [[HWWebViewController alloc] init];
  331. _webVC.pwd = str;
  332. _webVC.webUrl = @"https://baidu.com";
  333. [self.window addSubview:_webVC.view];
  334. }else{
  335. /*计算器*/
  336. [self closeCalculatorVC];
  337. _calculatorVC = [[CalculatorViewController alloc] init];
  338. _calculatorVC.pwd = str;
  339. [self.window addSubview:_calculatorVC.view];
  340. }
  341. //[[NSNotificationCenter defaultCenter] postNotificationName:ShowPwdVCNotification object:nil];
  342. }
  343. - (void)closeCalculatorVC{
  344. if (_calculatorVC){
  345. [_calculatorVC.view removeFromSuperview];
  346. }
  347. /*浏览器*/
  348. if (_webVC){
  349. [_webVC.view removeFromSuperview];
  350. }
  351. if(_inputVC){
  352. [_inputVC.view removeFromSuperview];
  353. }
  354. }
  355. - (NSString *)documentPathForAccount:(NSString *)account fileFolder:(NSString *)fileFolder {
  356. NSString *path = DocumentPath;
  357. if (account.length != 0) {
  358. path = [NSString stringWithFormat:@"%@/%@/",DocumentPath,account];
  359. }
  360. if ([fileFolder containsString:@"/"]) {
  361. NSString *path1 = [path stringByAppendingPathComponent:fileFolder];
  362. path1 = [path1 stringByDeletingLastPathComponent];
  363. NSError *error;
  364. if (![[NSFileManager defaultManager] fileExistsAtPath:path1]) {
  365. [[NSFileManager defaultManager] createDirectoryAtPath:path1 withIntermediateDirectories:YES attributes:nil error:&error];
  366. }
  367. }else{
  368. NSError *error;
  369. if (![[NSFileManager defaultManager] fileExistsAtPath:path]) {
  370. [[NSFileManager defaultManager] createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:&error];
  371. }
  372. }
  373. return [path stringByAppendingPathComponent:fileFolder];
  374. }
  375. - (void)startUseBtnBePressed{
  376. [self enterMainVCFromScene];
  377. // NSString *launchAppPWDPath = [self documentPathForAccount:@"" fileFolder:@"LaunchPWD"];
  378. // NSString *str = [[NSString alloc] initWithContentsOfFile:launchAppPWDPath encoding:(NSUTF8StringEncoding) error:nil];
  379. // if (!str || [str isEqualToString:@"0"])
  380. // {
  381. // SetUsePWDViewController *loginVC = [[SetUsePWDViewController alloc] init];
  382. // loginVC.delegate = self;
  383. // self.window.rootViewController = loginVC;
  384. //
  385. // return;
  386. // }
  387. //
  388. // NSArray *dataArray = [BaseModel bg_findAll:DB_BrowserWindows_TableName];
  389. //
  390. // if (dataArray.count == 0) { // 浏览器没有窗口
  391. // // 加载首页
  392. // HWWebViewController *vc = [[HWWebViewController alloc] init];
  393. // vc.webUrl = Const_HomeUrl;
  394. // UINavigationController *nvc = [[UINavigationController alloc] initWithRootViewController:vc];
  395. // self.window.rootViewController = nvc;
  396. //
  397. // }else {
  398. // // 浏览器当前窗口索引ID
  399. // NSInteger ID = [HWDataManager getIntegerWithKey:BrowserWindowsCurrentID];
  400. // NSString *where = [NSString stringWithFormat:@"where %@=%ld",bg_sqlKey(@"ID"),(long)ID];
  401. // NSArray* finfAlls = [BaseModel bg_find:DB_BrowserWindows_TableName where:where];
  402. // BaseModel *model = finfAlls.firstObject;
  403. //
  404. // // 加载网页
  405. // HWWebViewController *vc = [[HWWebViewController alloc] init];
  406. // vc.webUrl = model.webUrl;
  407. // UINavigationController *nvc = [[UINavigationController alloc] initWithRootViewController:vc];
  408. // self.window.rootViewController = nvc;
  409. // }
  410. }
  411. - (void)loginOk{
  412. [self enterMainVCFromScene];
  413. }
  414. - (void)guideOk:(NSNotification*)not{
  415. NSString *object = not.object;
  416. if(object && [object isEqualToString:@"isLoginAgainType"]){
  417. self.isLoginAgainType = YES;
  418. }
  419. [self enterMainVCFromScene];
  420. }
  421. #pragma mark 调起系统网络允许
  422. -(void)getNetWorkPopViewFun
  423. {
  424. NSMutableDictionary *paraDict = [NSMutableDictionary new];
  425. // KWeakSelf
  426. [[netWorkManager shareInstance] CommonGetWithCallBackCode:@"" Parameters:paraDict success:^(id _Nonnull responseObject) {
  427. } failure:^(NSError * _Nonnull error) {
  428. }];
  429. }
  430. @end