AppDelegate.m 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821
  1. //
  2. // AppDelegate.m
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 04/09/14.
  6. // Copyright (c) 2014 Marino Faggiana. All rights reserved.
  7. //
  8. // Author Marino Faggiana <marino.faggiana@nextcloud.com>
  9. //
  10. // This program is free software: you can redistribute it and/or modify
  11. // it under the terms of the GNU General Public License as published by
  12. // the Free Software Foundation, either version 3 of the License, or
  13. // (at your option) any later version.
  14. //
  15. // This program is distributed in the hope that it will be useful,
  16. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. // GNU General Public License for more details.
  19. //
  20. // You should have received a copy of the GNU General Public License
  21. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. //
  23. #import "AppDelegate.h"
  24. #import "NCBridgeSwift.h"
  25. #import "NSNotificationCenter+MainThread.h"
  26. #import "NCPushNotification.h"
  27. #import <QuartzCore/QuartzCore.h>
  28. @import Firebase;
  29. @class NCViewerRichdocument;
  30. @class NCSettingsBundleHelper;
  31. @interface AppDelegate() <TOPasscodeViewControllerDelegate>
  32. @end
  33. @implementation AppDelegate
  34. + (void)initialize
  35. {
  36. [[NSUserDefaults standardUserDefaults] registerDefaults:@{@"UserAgent": [CCUtility getUserAgent]}];
  37. }
  38. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
  39. {
  40. BOOL isSimulatorOrTestFlight = [[NCUtility shared] isSimulatorOrTestFlight];
  41. if (![CCUtility getDisableCrashservice] && NCBrandOptions.shared.disable_crash_service == false) {
  42. [FIRApp configure];
  43. }
  44. [CCUtility createDirectoryStandard];
  45. [CCUtility emptyTemporaryDirectory];
  46. // Networking
  47. [[NCCommunicationCommon shared] setupWithDelegate:[NCNetworking shared]];
  48. [[NCCommunicationCommon shared] setupWithUserAgent:[CCUtility getUserAgent]];
  49. // LOG
  50. NSInteger levelLog = [CCUtility getLogLevel];
  51. [[NCCommunicationCommon shared] setLevelLog:levelLog];
  52. NSString *pathDirectoryGroup = [[CCUtility getDirectoryGroup] path];
  53. [[NCCommunicationCommon shared] setPathLog: pathDirectoryGroup];
  54. [[NCCommunicationCommon shared] setCopyLogToDocumentDirectory: true];
  55. NSString *versionNextcloudiOS = [NSString stringWithFormat:[NCBrandOptions shared].textCopyrightNextcloudiOS, NCUtility.shared.getVersionApp];
  56. if (isSimulatorOrTestFlight) {
  57. [[NCCommunicationCommon shared] writeLog:[NSString stringWithFormat:@"Start session with level %lu %@ (Simulator / TestFlight)", (unsigned long)levelLog, versionNextcloudiOS]];
  58. } else {
  59. [[NCCommunicationCommon shared] writeLog:[NSString stringWithFormat:@"Start session with level %lu %@", (unsigned long)levelLog, versionNextcloudiOS]];
  60. }
  61. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(initializeMain:) name:NCBrandGlobal.shared.notificationCenterInitializeMain object:nil];
  62. // Set account, if no exists clear all
  63. tableAccount *tableAccount = [[NCManageDatabase shared] getAccountActive];
  64. if (tableAccount == nil) {
  65. // remove all the keys Chain
  66. [CCUtility deleteAllChainStore];
  67. // remove all the App group key
  68. [[NSUserDefaults standardUserDefaults] removePersistentDomainForName:[[NSBundle mainBundle] bundleIdentifier]];
  69. } else {
  70. // FIX 3.0.5 lost urlbase
  71. if (tableAccount.urlBase.length == 0) {
  72. NSString *user = [tableAccount.user stringByAppendingString:@" "];
  73. NSString *urlBase = [tableAccount.account stringByReplacingOccurrencesOfString:user withString:@""];
  74. tableAccount.urlBase = urlBase;
  75. [[NCManageDatabase shared] updateAccount:tableAccount];
  76. tableAccount = [[NCManageDatabase shared] getAccountActive];
  77. }
  78. [self settingAccount:tableAccount.account urlBase:tableAccount.urlBase user:tableAccount.user userID:tableAccount.userID password:[CCUtility getPassword:tableAccount.account]];
  79. }
  80. // UserDefaults
  81. self.ncUserDefaults = [[NSUserDefaults alloc] initWithSuiteName:[NCBrandOptions shared].capabilitiesGroups];
  82. self.listProgressMetadata = [NSMutableDictionary new];
  83. self.listFilesVC = [NSMutableDictionary new];
  84. self.listFavoriteVC = [NSMutableDictionary new];
  85. self.listOfflineVC = [NSMutableDictionary new];
  86. self.pasteboardOcIds = [NSMutableArray new];
  87. // Push Notification
  88. [application registerForRemoteNotifications];
  89. // Display notification
  90. [UNUserNotificationCenter currentNotificationCenter].delegate = self;
  91. UNAuthorizationOptions authOptions = UNAuthorizationOptionAlert | UNAuthorizationOptionSound | UNAuthorizationOptionBadge;
  92. [[UNUserNotificationCenter currentNotificationCenter] requestAuthorizationWithOptions:authOptions completionHandler:^(BOOL granted, NSError * _Nullable error) { }];
  93. //AV Session
  94. [[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayback error:nil];
  95. [[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
  96. // Start Timer
  97. [self startTimerErrorNetworking];
  98. // Store review
  99. if ([[NCUtility shared] isSimulatorOrTestFlight] == false) {
  100. NCStoreReview *review = [NCStoreReview new];
  101. [review incrementAppRuns];
  102. [review showStoreReview];
  103. }
  104. // Detect Dark mode
  105. if (@available(iOS 13.0, *)) {
  106. if ([CCUtility getDarkModeDetect]) {
  107. if ([[UITraitCollection currentTraitCollection] userInterfaceStyle] == UIUserInterfaceStyleDark) {
  108. [CCUtility setDarkMode:YES];
  109. } else {
  110. [CCUtility setDarkMode:NO];
  111. }
  112. }
  113. }
  114. if ([NCBrandOptions shared].disable_intro) {
  115. [CCUtility setIntro:YES];
  116. if (self.account == nil || self.account.length == 0) {
  117. [self openLoginView:nil selector:NCBrandGlobal.shared.introLogin openLoginWeb:false];
  118. }
  119. } else {
  120. if ([CCUtility getIntro] == NO) {
  121. UIViewController *introViewController = [[UIStoryboard storyboardWithName:@"NCIntro" bundle:[NSBundle mainBundle]] instantiateInitialViewController];
  122. UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController: introViewController];
  123. self.window.rootViewController = navController;
  124. [self.window makeKeyAndVisible];
  125. }
  126. }
  127. // init home
  128. [[NSNotificationCenter defaultCenter] postNotificationOnMainThreadName:NCBrandGlobal.shared.notificationCenterInitializeMain object:nil userInfo:nil];
  129. // Passcode
  130. dispatch_async(dispatch_get_main_queue(), ^{
  131. [self passcodeWithAutomaticallyPromptForBiometricValidation:true];
  132. });
  133. // Auto upload
  134. self.networkingAutoUpload = [NCNetworkingAutoUpload new];
  135. // Background task: register
  136. if (@available(iOS 13.0, *)) {
  137. [[BGTaskScheduler sharedScheduler] registerForTaskWithIdentifier:NCBrandGlobal.shared.refreshTask usingQueue:nil launchHandler:^(BGTask *task) {
  138. [self handleRefreshTask:task];
  139. }];
  140. [[BGTaskScheduler sharedScheduler] registerForTaskWithIdentifier:NCBrandGlobal.shared.processingTask usingQueue:nil launchHandler:^(BGTask *task) {
  141. [self handleProcessingTask:task];
  142. }];
  143. } else {
  144. // Background Fetch
  145. [application setMinimumBackgroundFetchInterval:UIApplicationBackgroundFetchIntervalMinimum];
  146. }
  147. return YES;
  148. }
  149. //
  150. // L' applicazione si dimetterà dallo stato di attivo
  151. //
  152. - (void)applicationWillResignActive:(UIApplication *)application
  153. {
  154. if (self.account == nil || self.account.length == 0) { return; }
  155. // Dismiss FileViewInFolder
  156. if (self.activeFileViewInFolder != nil ) {
  157. [self.activeFileViewInFolder dismissViewControllerAnimated:false completion:^{
  158. self.activeFileViewInFolder = nil;
  159. }];
  160. }
  161. }
  162. //
  163. // L' applicazione entrerà in primo piano (attivo solo dopo il background)
  164. //
  165. - (void)applicationWillEnterForeground:(UIApplication *)application
  166. {
  167. if (self.account == nil || self.account.length == 0) { return; }
  168. [[NCCommunicationCommon shared] writeLog:@"Application will enter in foreground"];
  169. [[NSNotificationCenter defaultCenter] postNotificationOnMainThreadName:NCBrandGlobal.shared.notificationCenterApplicationWillEnterForeground object:nil];
  170. // Request Passcode
  171. [self passcodeWithAutomaticallyPromptForBiometricValidation:true];
  172. // Initialize Auto upload
  173. [[NCAutoUpload shared] initAutoUploadWithViewController:nil completion:^(NSInteger items) { }];
  174. // Read active directory
  175. [[NSNotificationCenter defaultCenter] postNotificationOnMainThreadName:NCBrandGlobal.shared.notificationCenterReloadDataSourceNetworkForced object:nil];
  176. // Required unsubscribing / subscribing
  177. [[NCPushNotification shared] pushNotification];
  178. // RichDocument
  179. [[NSNotificationCenter defaultCenter] postNotificationOnMainThreadName:NCBrandGlobal.shared.notificationCenterRichdocumentGrabFocus object:nil];
  180. // Request Service Server Nextcloud
  181. [[NCService shared] startRequestServicesServer];
  182. }
  183. //
  184. // L' applicazione entrerà in primo piano (attivo sempre)
  185. //
  186. - (void)applicationDidBecomeActive:(UIApplication *)application
  187. {
  188. [NCSettingsBundleHelper setVersionAndBuildNumber];
  189. if (self.account == nil || self.account.length == 0) { return; }
  190. // Brand
  191. #if defined(HC)
  192. tableAccount *account = [[NCManageDatabase shared] getAccountActive];
  193. if (account.hcIsTrial == true || account.hcTrialExpired == true || account.hcNextGroupExpirationGroupExpired == true) {
  194. HCTrial *vc = [[UIStoryboard storyboardWithName:@"HCTrial" bundle:nil] instantiateInitialViewController];
  195. vc.account = account;
  196. [self.window.rootViewController presentViewController:vc animated:YES completion:nil];
  197. }
  198. #endif
  199. [[NCNetworking shared] verifyUploadZombie];
  200. }
  201. //
  202. // L' applicazione è entrata nello sfondo
  203. //
  204. - (void)applicationDidEnterBackground:(UIApplication *)application
  205. {
  206. if (self.account == nil || self.account.length == 0) { return; }
  207. [[NCCommunicationCommon shared] writeLog:@"Application did enter in background"];
  208. [[NSNotificationCenter defaultCenter] postNotificationOnMainThreadName:NCBrandGlobal.shared.notificationCenterApplicationDidEnterBackground object:nil];
  209. [self passcodeWithAutomaticallyPromptForBiometricValidation:false];
  210. if (@available(iOS 13.0, *)) {
  211. [self scheduleAppRefresh];
  212. [self scheduleBackgroundProcessing];
  213. }
  214. }
  215. //
  216. // L'applicazione terminerà
  217. //
  218. - (void)applicationWillTerminate:(UIApplication *)application
  219. {
  220. [[NCCommunicationCommon shared] writeLog:@"bye bye"];
  221. }
  222. // NotificationCenter
  223. - (void)initializeMain:(NSNotification *)notification
  224. {
  225. if (self.account == nil || self.account.length == 0) { return; }
  226. // Clear error certificate
  227. [CCUtility setCertificateError:self.account error:NO];
  228. // Setting Theming
  229. [[NCBrandColor shared] settingThemingColorWithAccount:self.account];
  230. // close detail
  231. [[NSNotificationCenter defaultCenter] postNotificationOnMainThreadName:NCBrandGlobal.shared.notificationCenterMenuDetailClose object:nil];
  232. // Start Auto Upload
  233. [[NCAutoUpload shared] initAutoUploadWithViewController:nil completion:^(NSInteger items) { }];
  234. // Start services
  235. [[NCService shared] startRequestServicesServer];
  236. // Registeration push notification
  237. [[NCPushNotification shared] pushNotification];
  238. // Registeration domain File Provider
  239. //FileProviderDomain *fileProviderDomain = [FileProviderDomain new];
  240. //[fileProviderDomain removeAllDomains];
  241. //[fileProviderDomain registerDomains];
  242. [[NCCommunicationCommon shared] writeLog:@"initialize Main"];
  243. }
  244. #pragma mark Login / checkErrorNetworking
  245. - (void)checkErrorNetworking
  246. {
  247. if (self.account == nil || self.account.length == 0) { return; }
  248. // check unauthorized server (401)
  249. if ([CCUtility getPassword:self.account].length == 0) {
  250. [self openLoginView:self.window.rootViewController selector:NCBrandGlobal.shared.introLogin openLoginWeb:true];
  251. }
  252. // check certificate untrusted (-1202)
  253. if ([CCUtility getCertificateError:self.account]) {
  254. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"_ssl_certificate_untrusted_", nil) message:NSLocalizedString(@"_connect_server_anyway_", nil) preferredStyle:UIAlertControllerStyleAlert];
  255. [alertController addAction: [UIAlertAction actionWithTitle:NSLocalizedString(@"_yes_", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
  256. [[NCNetworking shared] wrtiteCertificateWithDirectoryCertificate:[CCUtility getDirectoryCerificates]];
  257. [self startTimerErrorNetworking];
  258. }]];
  259. [alertController addAction: [UIAlertAction actionWithTitle:NSLocalizedString(@"_no_", nil) style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
  260. [self startTimerErrorNetworking];
  261. }]];
  262. [self.window.rootViewController presentViewController:alertController animated:YES completion:^{
  263. // Stop timer error network
  264. [self.timerErrorNetworking invalidate];
  265. }];
  266. }
  267. }
  268. - (void)openLoginView:(UIViewController *)viewController selector:(NSInteger)selector openLoginWeb:(BOOL)openLoginWeb
  269. {
  270. // use appConfig [MDM]
  271. if ([NCBrandOptions shared].use_configuration) {
  272. if (!(_appConfigView.isViewLoaded && _appConfigView.view.window)) {
  273. self.appConfigView = [[UIStoryboard storyboardWithName:@"CCLogin" bundle:nil] instantiateViewControllerWithIdentifier:@"NCAppConfigView"];
  274. [self showLoginViewController:self.appConfigView forContext:viewController];
  275. }
  276. return;
  277. }
  278. // only for personalized LoginWeb [customer]
  279. if ([NCBrandOptions shared].use_login_web_personalized) {
  280. if (!(_activeLoginWeb.isViewLoaded && _activeLoginWeb.view.window)) {
  281. self.activeLoginWeb = [[UIStoryboard storyboardWithName:@"CCLogin" bundle:nil] instantiateViewControllerWithIdentifier:@"NCLoginWeb"];
  282. self.activeLoginWeb.urlBase = [[NCBrandOptions shared] loginBaseUrl];
  283. [self showLoginViewController:self.activeLoginWeb forContext:viewController];
  284. }
  285. return;
  286. }
  287. // normal login
  288. if (selector == NCBrandGlobal.shared.introSignup) {
  289. if (!(_activeLoginWeb.isViewLoaded && _activeLoginWeb.view.window)) {
  290. self.activeLoginWeb = [[UIStoryboard storyboardWithName:@"CCLogin" bundle:nil] instantiateViewControllerWithIdentifier:@"NCLoginWeb"];
  291. if (selector == NCBrandGlobal.shared.introSignup) {
  292. self.activeLoginWeb.urlBase = [[NCBrandOptions shared] linkloginPreferredProviders];
  293. } else {
  294. self.activeLoginWeb.urlBase = self.urlBase;
  295. }
  296. [self showLoginViewController:self.activeLoginWeb forContext:viewController];
  297. }
  298. } else if ([NCBrandOptions shared].disable_intro && [NCBrandOptions shared].disable_request_login_url) {
  299. self.activeLoginWeb = [[UIStoryboard storyboardWithName:@"CCLogin" bundle:nil] instantiateViewControllerWithIdentifier:@"NCLoginWeb"];
  300. self.activeLoginWeb.urlBase = [[NCBrandOptions shared] loginBaseUrl];
  301. [self showLoginViewController:self.activeLoginWeb forContext:viewController];
  302. } else if (openLoginWeb) {
  303. if (!(_activeLoginWeb.isViewLoaded && _activeLoginWeb.view.window)) {
  304. self.activeLoginWeb = [[UIStoryboard storyboardWithName:@"CCLogin" bundle:nil] instantiateViewControllerWithIdentifier:@"NCLoginWeb"];
  305. self.activeLoginWeb.urlBase = self.urlBase;
  306. [self showLoginViewController:self.activeLoginWeb forContext:viewController];
  307. }
  308. } else {
  309. if (!(_activeLogin.isViewLoaded && _activeLogin.view.window)) {
  310. _activeLogin = [[UIStoryboard storyboardWithName:@"CCLogin" bundle:nil] instantiateViewControllerWithIdentifier:@"CCLoginNextcloud"];
  311. [self showLoginViewController:_activeLogin forContext:viewController];
  312. }
  313. }
  314. }
  315. -(void)showLoginViewController:(UIViewController *)viewController forContext:(UIViewController *)contextViewController
  316. {
  317. if (contextViewController == NULL) {
  318. UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:viewController];
  319. navigationController.navigationBar.barStyle = UIBarStyleBlack;
  320. navigationController.navigationBar.tintColor = NCBrandColor.shared.customerText;
  321. navigationController.navigationBar.barTintColor = NCBrandColor.shared.customer;
  322. [navigationController.navigationBar setTranslucent:false];
  323. self.window.rootViewController = navigationController;
  324. [self.window makeKeyAndVisible];
  325. } else if ([contextViewController isKindOfClass:[UINavigationController class]]) {
  326. UINavigationController *navigationController = ((UINavigationController *)contextViewController);
  327. [navigationController pushViewController:viewController animated:true];
  328. } else {
  329. UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:viewController];
  330. navigationController.modalPresentationStyle = UIModalPresentationFullScreen;
  331. navigationController.navigationBar.barStyle = UIBarStyleBlack;
  332. navigationController.navigationBar.tintColor = NCBrandColor.shared.customerText;
  333. navigationController.navigationBar.barTintColor = NCBrandColor.shared.customer;
  334. [navigationController.navigationBar setTranslucent:false];
  335. [contextViewController presentViewController:navigationController animated:true completion:nil];
  336. }
  337. }
  338. - (void)startTimerErrorNetworking
  339. {
  340. self.timerErrorNetworking = [NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(checkErrorNetworking) userInfo:nil repeats:YES];
  341. }
  342. #pragma mark Account & Communication
  343. - (void)settingAccount:(NSString *)account urlBase:(NSString *)urlBase user:(NSString *)user userID:(NSString *)userID password:(NSString *)password
  344. {
  345. self.account = account;
  346. self.urlBase = urlBase;
  347. self.user = user;
  348. self.userID = userID;
  349. self.password = password;
  350. (void)[NCNetworkingNotificationCenter shared];
  351. [[NCCommunicationCommon shared] setupWithAccount:account user:user userId:userID password:password urlBase:urlBase];
  352. NSInteger serverVersionMajor = [[NCManageDatabase shared] getCapabilitiesServerIntWithAccount:account elements:NCElementsJSON.shared.capabilitiesVersionMajor];
  353. if (serverVersionMajor > 0) {
  354. [[NCCommunicationCommon shared] setupWithNextcloudVersion:serverVersionMajor];
  355. }
  356. [[NCCommunicationCommon shared] setupWithWebDav:[[NCUtilityFileSystem shared] getWebDAVWithAccount:account]];
  357. [[NCCommunicationCommon shared] setupWithDav:[[NCUtilityFileSystem shared] getDAV]];
  358. }
  359. - (void)deleteAccount:(NSString *)account wipe:(BOOL)wipe
  360. {
  361. // Push Notification
  362. tableAccount *accountPN = [[NCManageDatabase shared] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  363. [[NCPushNotification shared] unsubscribingNextcloudServerPushNotification:accountPN.account urlBase:accountPN.urlBase user:accountPN.user withSubscribing:false];
  364. [self settingAccount:nil urlBase:nil user:nil userID:nil password:nil];
  365. /* DELETE ALL FILES LOCAL FS */
  366. NSArray *results = [[NCManageDatabase shared] getTableLocalFilesWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account] sorted:@"ocId" ascending:NO];
  367. for (tableLocalFile *result in results) {
  368. [CCUtility removeFileAtPath:[CCUtility getDirectoryProviderStorageOcId:result.ocId]];
  369. }
  370. // Clear database
  371. [[NCManageDatabase shared] clearDatabaseWithAccount:account removeAccount:true];
  372. [CCUtility clearAllKeysEndToEnd:account];
  373. [CCUtility clearAllKeysPushNotification:account];
  374. [CCUtility setCertificateError:account error:false];
  375. [CCUtility setPassword:account password:nil];
  376. if (wipe) {
  377. NSArray *listAccount = [[NCManageDatabase shared] getAccounts];
  378. if ([listAccount count] > 0) {
  379. NSString *newAccount = listAccount[0];
  380. tableAccount *tableAccount = [[NCManageDatabase shared] setAccountActive:newAccount];
  381. [self settingAccount:newAccount urlBase:tableAccount.urlBase user:tableAccount.user userID:tableAccount.userID password:[CCUtility getPassword:tableAccount.account]];
  382. [[NSNotificationCenter defaultCenter] postNotificationOnMainThreadName:NCBrandGlobal.shared.notificationCenterInitializeMain object:nil userInfo:nil];
  383. } else {
  384. [self openLoginView:self.window.rootViewController selector:NCBrandGlobal.shared.introLogin openLoginWeb:false];
  385. }
  386. }
  387. }
  388. #pragma mark Push Notifications
  389. -(void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler
  390. {
  391. completionHandler(UNNotificationPresentationOptionAlert);
  392. }
  393. -(void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(nonnull UNNotificationResponse *)response withCompletionHandler:(nonnull void (^)(void))completionHandler
  394. {
  395. completionHandler();
  396. }
  397. - (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
  398. {
  399. [[NCPushNotification shared] registerForRemoteNotificationsWithDeviceToken:deviceToken];
  400. }
  401. - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
  402. {
  403. [[NCPushNotification shared] applicationdidReceiveRemoteNotification:userInfo fetchCompletionHandler:^(UIBackgroundFetchResult result) {
  404. completionHandler(result);
  405. }];
  406. }
  407. #pragma mark Background Task
  408. -(void)scheduleAppRefresh API_AVAILABLE(ios(13.0))
  409. {
  410. NSError *error = NULL;
  411. BGAppRefreshTaskRequest *request = [[BGAppRefreshTaskRequest alloc] initWithIdentifier:NCBrandGlobal.shared.refreshTask];
  412. request.earliestBeginDate = [NSDate dateWithTimeIntervalSinceNow:5*60]; // after 5 minutes
  413. BOOL success = [[BGTaskScheduler sharedScheduler] submitTaskRequest:request error:&error];
  414. if (success) {
  415. [[NCCommunicationCommon shared] writeLog:[NSString stringWithFormat:@"Refresh task success submit request %@", request]];
  416. } else {
  417. [[NCCommunicationCommon shared] writeLog:[NSString stringWithFormat:@"Refresh task failed to submit request: %@", error]];
  418. }
  419. }
  420. -(void)scheduleBackgroundProcessing API_AVAILABLE(ios(13.0))
  421. {
  422. NSError *error = NULL;
  423. BGProcessingTaskRequest *request = [[BGProcessingTaskRequest alloc] initWithIdentifier:NCBrandGlobal.shared.processingTask];
  424. request.earliestBeginDate = [NSDate dateWithTimeIntervalSinceNow:5*60]; // after 5 minutes
  425. request.requiresNetworkConnectivity = YES;
  426. request.requiresExternalPower = NO;
  427. BOOL success = [[BGTaskScheduler sharedScheduler] submitTaskRequest:request error:&error];
  428. if (success) {
  429. [[NCCommunicationCommon shared] writeLog:[NSString stringWithFormat:@"Background Processing task success submit request %@", request]];
  430. } else {
  431. [[NCCommunicationCommon shared] writeLog:[NSString stringWithFormat:@"Background Processing task failed to submit request: %@", error]];
  432. }
  433. }
  434. -(void)handleRefreshTask:(BGTask *)task API_AVAILABLE(ios(13.0))
  435. {
  436. if (self.account == nil || self.account.length == 0) {
  437. [task setTaskCompletedWithSuccess:true];
  438. return;
  439. }
  440. [[NCCommunicationCommon shared] writeLog:@"Start handler refresh task [Auto upload]"];
  441. [[NCAutoUpload shared] initAutoUploadWithViewController:nil completion:^(NSInteger items) {
  442. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 5 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
  443. [[NCCommunicationCommon shared] writeLog:[NSString stringWithFormat:@"Completition handler refresh task with %lu uploads [Auto upload]", (unsigned long)items]];
  444. [task setTaskCompletedWithSuccess:true];
  445. });
  446. }];
  447. }
  448. -(void)handleProcessingTask:(BGTask *)task API_AVAILABLE(ios(13.0))
  449. {
  450. if (self.account == nil || self.account.length == 0) {
  451. [task setTaskCompletedWithSuccess:true];
  452. return;
  453. }
  454. [[NCCommunicationCommon shared] writeLog:@"Start handler processing task [Synchronize Favorite & Offline]"];
  455. [[NCNetworking shared] listingFavoritescompletionWithSelector:NCBrandGlobal.shared.selectorReadFile completion:^(NSString *account, NSArray<tableMetadata *> *metadatas, NSInteger errorCode, NSString * errorDescription) {
  456. [[NCCommunicationCommon shared] writeLog:[NSString stringWithFormat:@"Completition listing favorite with error %lu", (unsigned long)errorCode]];
  457. }];
  458. [[NCService shared] synchronizeOfflineWithAccount:self.account];
  459. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 25 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
  460. [[NCCommunicationCommon shared] writeLog:@"Completition handler processing task [Synchronize Favorite & Offline]"];
  461. [task setTaskCompletedWithSuccess:true];
  462. });
  463. }
  464. #pragma mark Fetch
  465. - (void)application:(UIApplication *)application performFetchWithCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
  466. {
  467. if (@available(iOS 13.0, *)) {
  468. [[NCCommunicationCommon shared] writeLog:@"Start perform Fetch on iOS >= 13 denied"];
  469. } else {
  470. if (self.account == nil || self.account.length == 0) {
  471. completionHandler(UIBackgroundFetchResultNoData);
  472. return;
  473. }
  474. [[NCCommunicationCommon shared] writeLog:@"Start perform Fetch [Auto upload]"];
  475. [[NCAutoUpload shared] initAutoUploadWithViewController:nil completion:^(NSInteger items) {
  476. [[NCCommunicationCommon shared] writeLog:[NSString stringWithFormat:@"Completition perform Fetch with %lu uploads [Auto upload]", (unsigned long)items]];
  477. if (items == 0) {
  478. completionHandler(UIBackgroundFetchResultNoData);
  479. } else {
  480. completionHandler(UIBackgroundFetchResultNewData);
  481. }
  482. }];
  483. }
  484. }
  485. #pragma mark Operation Networking & Session
  486. //
  487. // Method called by the system when all the background task has end
  488. //
  489. - (void)application:(UIApplication *)application handleEventsForBackgroundURLSession:(NSString *)identifier completionHandler:(void (^)(void))completionHandler
  490. {
  491. [[NCCommunicationCommon shared] writeLog:[NSString stringWithFormat:@"Start handle Events For Background URLSession: %@", identifier]];
  492. self.backgroundSessionCompletionHandler = completionHandler;
  493. }
  494. #pragma mark OpenURL
  495. - (BOOL)application:(UIApplication *)application openURL:(NSURL *)url options:(NSDictionary<NSString *,id> *)options
  496. {
  497. if (self.account == nil || self.account.length == 0) { return YES; }
  498. NSString *scheme = url.scheme;
  499. NSString *fileName;
  500. NSString *serverUrl;
  501. if ([scheme isEqualToString:@"nextcloud"]) {
  502. NSString *action = url.host;
  503. if ([action isEqualToString:@"open-file"]) {
  504. NSURLComponents *urlComponents = [NSURLComponents componentsWithURL:url resolvingAgainstBaseURL:NO];
  505. NSArray *queryItems = urlComponents.queryItems;
  506. NSString *user = [CCUtility valueForKey:@"user" fromQueryItems:queryItems];
  507. NSString *path = [CCUtility valueForKey:@"path" fromQueryItems:queryItems];
  508. NSString *link = [CCUtility valueForKey:@"link" fromQueryItems:queryItems];
  509. tableAccount *matchedAccount = nil;
  510. // verify parameter
  511. if (user.length == 0 || path.length == 0 || [[NSURL URLWithString:link] host].length == 0) {
  512. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"_error_", nil) message:NSLocalizedString(@"_error_parameter_schema_", nil) preferredStyle:UIAlertControllerStyleAlert];
  513. UIAlertAction *okAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"_ok_", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {}];
  514. [alertController addAction:okAction];
  515. [self.window.rootViewController presentViewController:alertController animated:YES completion:nil];
  516. } else {
  517. tableAccount *account = [[NCManageDatabase shared] getAccountActive];
  518. if (account) {
  519. NSURL *accountURL = [NSURL URLWithString:account.urlBase];
  520. NSString *accountUser = account.user;
  521. if ([link containsString:accountURL.host] && [user isEqualToString:accountUser]) {
  522. matchedAccount = account;
  523. } else {
  524. NSArray *accounts = [[NCManageDatabase shared] getAllAccount];
  525. for (tableAccount *account in accounts) {
  526. NSURL *accountURL = [NSURL URLWithString:account.urlBase];
  527. NSString *accountUser = account.user;
  528. if ([link containsString:accountURL.host] && [user isEqualToString:accountUser]) {
  529. matchedAccount = [[NCManageDatabase shared] setAccountActive:account.account];
  530. [self settingAccount:matchedAccount.account urlBase:matchedAccount.urlBase user:matchedAccount.user userID:matchedAccount.userID password:[CCUtility getPassword:matchedAccount.account]];
  531. [[NSNotificationCenter defaultCenter] postNotificationOnMainThreadName:NCBrandGlobal.shared.notificationCenterInitializeMain object:nil userInfo:nil];
  532. }
  533. }
  534. }
  535. if (matchedAccount) {
  536. NSString *webDAV = [[NCUtilityFileSystem shared] getWebDAVWithAccount:self.account];
  537. if ([path containsString:@"/"]) {
  538. fileName = [path lastPathComponent];
  539. serverUrl = [NSString stringWithFormat:@"%@/%@/%@", matchedAccount.urlBase, webDAV, [path stringByDeletingLastPathComponent]];
  540. } else {
  541. fileName = path;
  542. serverUrl = [NSString stringWithFormat:@"%@/%@", matchedAccount.urlBase, webDAV];
  543. }
  544. [[NCCollectionCommon shared] openFileViewInFolderWithServerUrl:serverUrl fileName:fileName];
  545. } else {
  546. NSString *domain = [[NSURL URLWithString:link] host];
  547. NSString *fileName = [path lastPathComponent];
  548. NSString *message = [NSString stringWithFormat:NSLocalizedString(@"_account_not_available_", nil), user, domain, fileName];
  549. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"_info_", nil) message:message preferredStyle:UIAlertControllerStyleAlert];
  550. UIAlertAction *okAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"_ok_", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {}];
  551. [alertController addAction:okAction];
  552. [self.window.rootViewController presentViewController:alertController animated:YES completion:nil];
  553. }
  554. }
  555. }
  556. }
  557. return YES;
  558. }
  559. return YES;
  560. }
  561. #pragma mark Passcode + Delegate
  562. - (void)passcodeWithAutomaticallyPromptForBiometricValidation:(BOOL)automaticallyPromptForBiometricValidation
  563. {
  564. LAContext *laContext = [LAContext new];
  565. NSError *error;
  566. BOOL isBiometryAvailable = false;
  567. if ([[CCUtility getPasscode] length] == 0 || [self.account length] == 0 || [CCUtility getNotPasscodeAtStart]) return;
  568. if (self.passcodeViewController == nil) {
  569. self.passcodeViewController = [[TOPasscodeViewController alloc] initWithStyle:TOPasscodeViewStyleTranslucentLight passcodeType:TOPasscodeTypeSixDigits];
  570. if (@available(iOS 13.0, *)) {
  571. if ([[UITraitCollection currentTraitCollection] userInterfaceStyle] == UIUserInterfaceStyleDark) {
  572. self.passcodeViewController.style = TOPasscodeViewStyleTranslucentDark;
  573. }
  574. }
  575. self.passcodeViewController.delegate = self;
  576. self.passcodeViewController.keypadButtonShowLettering = false;
  577. if (CCUtility.getEnableTouchFaceID && [laContext canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:&error]) {
  578. if (error == NULL) {
  579. if (laContext.biometryType == LABiometryTypeFaceID) {
  580. self.passcodeViewController.biometryType = TOPasscodeBiometryTypeFaceID;
  581. self.passcodeViewController.allowBiometricValidation = true;
  582. isBiometryAvailable = true;
  583. } else if (laContext.biometryType == LABiometryTypeTouchID) {
  584. self.passcodeViewController.biometryType = TOPasscodeBiometryTypeTouchID;
  585. self.passcodeViewController.allowBiometricValidation = true;
  586. isBiometryAvailable = true;
  587. } else {
  588. isBiometryAvailable = false;
  589. NSLog(@"No Biometric support");
  590. }
  591. }
  592. }
  593. [self.window.rootViewController presentViewController:self.passcodeViewController animated:YES completion:^{
  594. [self enableTouchFaceID:automaticallyPromptForBiometricValidation];
  595. }];
  596. } else {
  597. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 0.5 * NSEC_PER_SEC), dispatch_get_main_queue(), ^(void) {
  598. [self enableTouchFaceID:automaticallyPromptForBiometricValidation];
  599. });
  600. }
  601. }
  602. - (void)didInputCorrectPasscodeInPasscodeViewController:(TOPasscodeViewController *)passcodeViewController
  603. {
  604. [passcodeViewController dismissViewControllerAnimated:YES completion:^{
  605. self.passcodeViewController = nil;
  606. }];
  607. }
  608. - (BOOL)passcodeViewController:(TOPasscodeViewController *)passcodeViewController isCorrectCode:(NSString *)code
  609. {
  610. return [code isEqualToString:[CCUtility getPasscode]];
  611. }
  612. - (void)didPerformBiometricValidationRequestInPasscodeViewController:(TOPasscodeViewController *)passcodeViewController
  613. {
  614. [[LAContext new] evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics localizedReason:[[NCBrandOptions shared] brand] reply:^(BOOL success, NSError * _Nullable error) {
  615. if (success) {
  616. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 0.5 * NSEC_PER_SEC), dispatch_get_main_queue(), ^(void) {
  617. [passcodeViewController dismissViewControllerAnimated:YES completion:^{
  618. self.passcodeViewController = nil;
  619. }];
  620. });
  621. }
  622. }];
  623. }
  624. - (void)enableTouchFaceID:(BOOL)automaticallyPromptForBiometricValidation
  625. {
  626. if (CCUtility.getEnableTouchFaceID && automaticallyPromptForBiometricValidation && self.passcodeViewController.view.window) {
  627. [[LAContext new] evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics localizedReason:[[NCBrandOptions shared] brand] reply:^(BOOL success, NSError * _Nullable error) {
  628. if (success) {
  629. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 0.5 * NSEC_PER_SEC), dispatch_get_main_queue(), ^(void) {
  630. [self.passcodeViewController dismissViewControllerAnimated:YES completion:^{
  631. self.passcodeViewController = nil;
  632. }];
  633. });
  634. }
  635. }];
  636. }
  637. }
  638. @end