AppDelegate.m 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955
  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 "CCGraphics.h"
  25. #import "NCBridgeSwift.h"
  26. #import "NCAutoUpload.h"
  27. #import "NCPushNotificationEncryption.h"
  28. #import <QuartzCore/QuartzCore.h>
  29. @import Firebase;
  30. @class NCViewerRichdocument;
  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.sharedInstance.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. NSInteger logLevel = [CCUtility getLogLevel];
  50. [[NCCommunicationCommon shared] setFileLogWithLevel:logLevel];
  51. NSString *versionApp = [NSString stringWithFormat:@"%@.%@", [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleShortVersionString"], [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleVersion"]];
  52. NSString *versionNextcloudiOS = [NSString stringWithFormat:[NCBrandOptions sharedInstance].textCopyrightNextcloudiOS, versionApp];
  53. if (isSimulatorOrTestFlight) {
  54. [[NCCommunicationCommon shared] writeLog:[NSString stringWithFormat:@"Start session with level %lu %@ (Simulator / TestFlight)", (unsigned long)logLevel, versionNextcloudiOS]];
  55. } else {
  56. [[NCCommunicationCommon shared] writeLog:[NSString stringWithFormat:@"Start session with level %lu %@", (unsigned long)logLevel, versionNextcloudiOS]];
  57. }
  58. //
  59. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(initializeMain:) name:k_notificationCenter_initializeMain object:nil];
  60. // Set account, if no exists clear all
  61. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountActive];
  62. if (tableAccount == nil) {
  63. // remove all the keys Chain
  64. [CCUtility deleteAllChainStore];
  65. // remove all the App group key
  66. [[NSUserDefaults standardUserDefaults] removePersistentDomainForName:[[NSBundle mainBundle] bundleIdentifier]];
  67. } else {
  68. // FIX 3.0.5 lost urlbase
  69. if (tableAccount.urlBase.length == 0) {
  70. NSString *user = [tableAccount.user stringByAppendingString:@" "];
  71. NSString *urlBase = [tableAccount.account stringByReplacingOccurrencesOfString:user withString:@""];
  72. tableAccount.urlBase = urlBase;
  73. [[NCManageDatabase sharedInstance] updateAccount:tableAccount];
  74. tableAccount = [[NCManageDatabase sharedInstance] getAccountActive];
  75. }
  76. [self settingAccount:tableAccount.account urlBase:tableAccount.urlBase user:tableAccount.user userID:tableAccount.userID password:[CCUtility getPassword:tableAccount.account]];
  77. }
  78. // UserDefaults
  79. self.ncUserDefaults = [[NSUserDefaults alloc] initWithSuiteName:[NCBrandOptions sharedInstance].capabilitiesGroups];
  80. // Background Fetch
  81. [application setMinimumBackgroundFetchInterval:UIApplicationBackgroundFetchIntervalMinimum];
  82. self.listProgressMetadata = [NSMutableDictionary new];
  83. self.listFilesVC = [NSMutableDictionary new];
  84. self.listFavoriteVC = [NSMutableDictionary new];
  85. self.listOfflineVC = [NSMutableDictionary new];
  86. // Push Notification
  87. [application registerForRemoteNotifications];
  88. // Display notification
  89. [UNUserNotificationCenter currentNotificationCenter].delegate = self;
  90. UNAuthorizationOptions authOptions = UNAuthorizationOptionAlert | UNAuthorizationOptionSound | UNAuthorizationOptionBadge;
  91. [[UNUserNotificationCenter currentNotificationCenter] requestAuthorizationWithOptions:authOptions completionHandler:^(BOOL granted, NSError * _Nullable error) { }];
  92. //AV Session
  93. [[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayback error:nil];
  94. [[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
  95. // Start Timer
  96. self.timerUpdateApplicationIconBadgeNumber = [NSTimer scheduledTimerWithTimeInterval:k_timerUpdateApplicationIconBadgeNumber target:self selector:@selector(updateApplicationIconBadgeNumber) userInfo:nil repeats:YES];
  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 sharedInstance].disable_intro) {
  115. [CCUtility setIntro:YES];
  116. if (self.account.length == 0) {
  117. [self openLoginView:nil selector:k_intro_login 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:k_notificationCenter_initializeMain 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. return YES;
  136. }
  137. //
  138. // L' applicazione si dimetterà dallo stato di attivo
  139. //
  140. - (void)applicationWillResignActive:(UIApplication *)application
  141. {
  142. if (self.account.length == 0) return;
  143. // Dismiss FileViewInFolder
  144. if (self.activeFileViewInFolder != nil ) {
  145. [self.activeFileViewInFolder dismissViewControllerAnimated:false completion:^{
  146. self.activeFileViewInFolder = nil;
  147. }];
  148. }
  149. [self updateApplicationIconBadgeNumber];
  150. }
  151. //
  152. // L' applicazione entrerà in primo piano (attivo solo dopo il background)
  153. //
  154. - (void)applicationWillEnterForeground:(UIApplication *)application
  155. {
  156. if (self.account.length == 0) { return; }
  157. [[NSNotificationCenter defaultCenter] postNotificationOnMainThreadName:k_notificationCenter_applicationWillEnterForeground object:nil];
  158. // Request Passcode
  159. [self passcodeWithAutomaticallyPromptForBiometricValidation:true];
  160. // Initialize Auto upload
  161. [[NCAutoUpload sharedInstance] initStateAutoUpload];
  162. // Read active directory
  163. [[NSNotificationCenter defaultCenter] postNotificationOnMainThreadName:k_notificationCenter_reloadDataSourceNetworkForced object:nil];
  164. // Required unsubscribing / subscribing
  165. [self pushNotification];
  166. // RichDocument
  167. [[NSNotificationCenter defaultCenter] postNotificationOnMainThreadName:k_notificationCenter_richdocumentGrabFocus object:nil];
  168. // Request Service Server Nextcloud
  169. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 1 * NSEC_PER_SEC), dispatch_get_main_queue(), ^(void) {
  170. [[NCService shared] startRequestServicesServer];
  171. });
  172. }
  173. //
  174. // L' applicazione entrerà in primo piano (attivo sempre)
  175. //
  176. - (void)applicationDidBecomeActive:(UIApplication *)application
  177. {
  178. if (self.account.length == 0) { return; }
  179. // Brand
  180. #if defined(HC)
  181. tableAccount *account = [[NCManageDatabase sharedInstance] getAccountActive];
  182. if (account.hcIsTrial == true || account.hcTrialExpired == true || account.hcNextGroupExpirationGroupExpired == true) {
  183. HCTrial *vc = [[UIStoryboard storyboardWithName:@"HCTrial" bundle:nil] instantiateInitialViewController];
  184. vc.account = account;
  185. [self.window.rootViewController presentViewController:vc animated:YES completion:nil];
  186. }
  187. #endif
  188. [[NCNetworking shared] verifyUploadZombie];
  189. }
  190. //
  191. // L' applicazione è entrata nello sfondo
  192. //
  193. - (void)applicationDidEnterBackground:(UIApplication *)application
  194. {
  195. if (self.account.length == 0) { return; }
  196. [[NSNotificationCenter defaultCenter] postNotificationOnMainThreadName:k_notificationCenter_applicationDidEnterBackground object:nil];
  197. [self passcodeWithAutomaticallyPromptForBiometricValidation:false];
  198. }
  199. //
  200. // L'applicazione terminerà
  201. //
  202. - (void)applicationWillTerminate:(UIApplication *)application
  203. {
  204. [[NCCommunicationCommon shared] writeLog:@"bye bye"];
  205. }
  206. // NotificationCenter
  207. - (void)initializeMain:(NSNotification *)notification
  208. {
  209. if (self.account.length == 0) return;
  210. // Clear error certificate
  211. [CCUtility setCertificateError:self.account error:NO];
  212. // Setting Theming
  213. [[NCBrandColor sharedInstance] settingThemingColor];
  214. // close detail
  215. [[NSNotificationCenter defaultCenter] postNotificationOnMainThreadName:k_notificationCenter_menuDetailClose object:nil];
  216. // Not Photos Video in library ? then align and Init Auto Upload
  217. NSArray *recordsPhotoLibrary = [[NCManageDatabase sharedInstance] getPhotoLibraryWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", self.account]];
  218. if ([recordsPhotoLibrary count] == 0) {
  219. [[NCAutoUpload sharedInstance] alignPhotoLibrary];
  220. }
  221. // Start Auto Upload
  222. [[NCAutoUpload sharedInstance] initStateAutoUpload];
  223. // Start services
  224. [[NCService shared] startRequestServicesServer];
  225. // Registeration push notification
  226. [self pushNotification];
  227. // Registeration domain File Provider
  228. //FileProviderDomain *fileProviderDomain = [FileProviderDomain new];
  229. //[fileProviderDomain removeAllDomains];
  230. //[fileProviderDomain registerDomains];
  231. [[NCCommunicationCommon shared] writeLog:@"initialize Main"];
  232. }
  233. #pragma --------------------------------------------------------------------------------------------
  234. #pragma mark ===== Login / checkErrorNetworking =====
  235. #pragma --------------------------------------------------------------------------------------------
  236. - (void)checkErrorNetworking
  237. {
  238. // test
  239. if (self.account.length == 0) return;
  240. // check unauthorized server (401)
  241. if ([CCUtility getPassword:self.account].length == 0) {
  242. [self openLoginView:self.window.rootViewController selector:k_intro_login openLoginWeb:true];
  243. }
  244. // check certificate untrusted (-1202)
  245. if ([CCUtility getCertificateError:self.account]) {
  246. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"_ssl_certificate_untrusted_", nil) message:NSLocalizedString(@"_connect_server_anyway_", nil) preferredStyle:UIAlertControllerStyleAlert];
  247. [alertController addAction: [UIAlertAction actionWithTitle:NSLocalizedString(@"_yes_", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
  248. [[NCNetworking shared] wrtiteCertificateWithDirectoryCertificate:[CCUtility getDirectoryCerificates]];
  249. [self startTimerErrorNetworking];
  250. }]];
  251. [alertController addAction: [UIAlertAction actionWithTitle:NSLocalizedString(@"_no_", nil) style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
  252. [self startTimerErrorNetworking];
  253. }]];
  254. [self.window.rootViewController presentViewController:alertController animated:YES completion:^{
  255. // Stop timer error network
  256. [self.timerErrorNetworking invalidate];
  257. }];
  258. }
  259. }
  260. - (void)openLoginView:(UIViewController *)viewController selector:(NSInteger)selector openLoginWeb:(BOOL)openLoginWeb
  261. {
  262. // use appConfig [MDM]
  263. if ([NCBrandOptions sharedInstance].use_configuration) {
  264. if (!(_appConfigView.isViewLoaded && _appConfigView.view.window)) {
  265. self.appConfigView = [[UIStoryboard storyboardWithName:@"CCLogin" bundle:nil] instantiateViewControllerWithIdentifier:@"NCAppConfigView"];
  266. [self showLoginViewController:self.appConfigView forContext:viewController];
  267. }
  268. return;
  269. }
  270. // only for personalized LoginWeb [customer]
  271. if ([NCBrandOptions sharedInstance].use_login_web_personalized) {
  272. if (!(_activeLoginWeb.isViewLoaded && _activeLoginWeb.view.window)) {
  273. self.activeLoginWeb = [[UIStoryboard storyboardWithName:@"CCLogin" bundle:nil] instantiateViewControllerWithIdentifier:@"NCLoginWeb"];
  274. self.activeLoginWeb.urlBase = [[NCBrandOptions sharedInstance] loginBaseUrl];
  275. [self showLoginViewController:self.activeLoginWeb forContext:viewController];
  276. }
  277. return;
  278. }
  279. // normal login
  280. if (selector == k_intro_signup) {
  281. if (!(_activeLoginWeb.isViewLoaded && _activeLoginWeb.view.window)) {
  282. self.activeLoginWeb = [[UIStoryboard storyboardWithName:@"CCLogin" bundle:nil] instantiateViewControllerWithIdentifier:@"NCLoginWeb"];
  283. if (selector == k_intro_signup) {
  284. self.activeLoginWeb.urlBase = [[NCBrandOptions sharedInstance] linkloginPreferredProviders];
  285. } else {
  286. self.activeLoginWeb.urlBase = self.urlBase;
  287. }
  288. [self showLoginViewController:self.activeLoginWeb forContext:viewController];
  289. }
  290. } else if ([NCBrandOptions sharedInstance].disable_intro && [NCBrandOptions sharedInstance].disable_request_login_url) {
  291. self.activeLoginWeb = [[UIStoryboard storyboardWithName:@"CCLogin" bundle:nil] instantiateViewControllerWithIdentifier:@"NCLoginWeb"];
  292. self.activeLoginWeb.urlBase = [[NCBrandOptions sharedInstance] loginBaseUrl];
  293. [self showLoginViewController:self.activeLoginWeb forContext:viewController];
  294. } else if (openLoginWeb) {
  295. if (!(_activeLoginWeb.isViewLoaded && _activeLoginWeb.view.window)) {
  296. self.activeLoginWeb = [[UIStoryboard storyboardWithName:@"CCLogin" bundle:nil] instantiateViewControllerWithIdentifier:@"NCLoginWeb"];
  297. self.activeLoginWeb.urlBase = self.urlBase;
  298. [self showLoginViewController:self.activeLoginWeb forContext:viewController];
  299. }
  300. } else {
  301. if (!(_activeLogin.isViewLoaded && _activeLogin.view.window)) {
  302. _activeLogin = [[UIStoryboard storyboardWithName:@"CCLogin" bundle:nil] instantiateViewControllerWithIdentifier:@"CCLoginNextcloud"];
  303. [self showLoginViewController:_activeLogin forContext:viewController];
  304. }
  305. }
  306. }
  307. -(void)showLoginViewController:(UIViewController *)viewController forContext:(UIViewController *)contextViewController
  308. {
  309. if (contextViewController == NULL) {
  310. UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:viewController];
  311. navigationController.navigationBar.barStyle = UIBarStyleBlack;
  312. navigationController.navigationBar.tintColor = NCBrandColor.sharedInstance.customerText;
  313. navigationController.navigationBar.barTintColor = NCBrandColor.sharedInstance.customer;
  314. [navigationController.navigationBar setTranslucent:false];
  315. self.window.rootViewController = navigationController;
  316. [self.window makeKeyAndVisible];
  317. } else if ([contextViewController isKindOfClass:[UINavigationController class]]) {
  318. UINavigationController *navigationController = ((UINavigationController *)contextViewController);
  319. [navigationController pushViewController:viewController animated:true];
  320. } else {
  321. UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:viewController];
  322. navigationController.modalPresentationStyle = UIModalPresentationFullScreen;
  323. navigationController.navigationBar.barStyle = UIBarStyleBlack;
  324. navigationController.navigationBar.tintColor = NCBrandColor.sharedInstance.customerText;
  325. navigationController.navigationBar.barTintColor = NCBrandColor.sharedInstance.customer;
  326. [navigationController.navigationBar setTranslucent:false];
  327. [contextViewController presentViewController:navigationController animated:true completion:nil];
  328. }
  329. }
  330. - (void)startTimerErrorNetworking
  331. {
  332. self.timerErrorNetworking = [NSTimer scheduledTimerWithTimeInterval:k_timerErrorNetworking target:self selector:@selector(checkErrorNetworking) userInfo:nil repeats:YES];
  333. }
  334. #pragma --------------------------------------------------------------------------------------------
  335. #pragma mark ===== Account & Communication =====
  336. #pragma --------------------------------------------------------------------------------------------
  337. - (void)settingAccount:(NSString *)account urlBase:(NSString *)urlBase user:(NSString *)user userID:(NSString *)userID password:(NSString *)password
  338. {
  339. self.account = account;
  340. self.urlBase = urlBase;
  341. self.user = user;
  342. self.userID = userID;
  343. self.password = password;
  344. (void)[NCNetworkingNotificationCenter shared];
  345. [[NCCommunicationCommon shared] setupWithAccount:account user:user userId:userID password:password urlBase:urlBase];
  346. [self settingSetupCommunication:account];
  347. }
  348. - (void)deleteAccount:(NSString *)account wipe:(BOOL)wipe
  349. {
  350. // Push Notification
  351. tableAccount *accountPN = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  352. [self unsubscribingNextcloudServerPushNotification:accountPN.account urlBase:accountPN.urlBase user:accountPN.user withSubscribing:false];
  353. [self settingAccount:nil urlBase:nil user:nil userID:nil password:nil];
  354. /* DELETE ALL FILES LOCAL FS */
  355. NSArray *results = [[NCManageDatabase sharedInstance] getTableLocalFilesWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account] sorted:@"ocId" ascending:NO];
  356. for (tableLocalFile *result in results) {
  357. [CCUtility removeFileAtPath:[CCUtility getDirectoryProviderStorageOcId:result.ocId]];
  358. }
  359. // Clear database
  360. [[NCManageDatabase sharedInstance] clearDatabaseWithAccount:account removeAccount:true];
  361. [CCUtility clearAllKeysEndToEnd:account];
  362. [CCUtility clearAllKeysPushNotification:account];
  363. [CCUtility setCertificateError:account error:false];
  364. [CCUtility setPassword:account password:nil];
  365. if (wipe) {
  366. NSArray *listAccount = [[NCManageDatabase sharedInstance] getAccounts];
  367. if ([listAccount count] > 0) {
  368. NSString *newAccount = listAccount[0];
  369. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] setAccountActive:newAccount];
  370. [self settingAccount:newAccount urlBase:tableAccount.urlBase user:tableAccount.user userID:tableAccount.userID password:[CCUtility getPassword:tableAccount.account]];
  371. [[NSNotificationCenter defaultCenter] postNotificationOnMainThreadName:k_notificationCenter_initializeMain object:nil userInfo:nil];
  372. } else {
  373. [self openLoginView:self.window.rootViewController selector:k_intro_login openLoginWeb:false];
  374. }
  375. }
  376. }
  377. - (void)settingSetupCommunication:(NSString *)account
  378. {
  379. NSInteger serverVersionMajor = [[NCManageDatabase sharedInstance] getCapabilitiesServerIntWithAccount:account elements:NCElementsJSON.shared.capabilitiesVersionMajor];
  380. if (serverVersionMajor > 0) {
  381. [[NCCommunicationCommon shared] setupWithNextcloudVersion:serverVersionMajor];
  382. }
  383. [[NCCommunicationCommon shared] setupWithWebDav:[[NCUtility shared] getWebDAVWithAccount:account]];
  384. [[NCCommunicationCommon shared] setupWithDav:[[NCUtility shared] getDAV]];
  385. }
  386. #pragma --------------------------------------------------------------------------------------------
  387. #pragma mark ===== Push Notifications =====
  388. #pragma --------------------------------------------------------------------------------------------
  389. - (void)pushNotification
  390. {
  391. // test
  392. if (self.account.length == 0 || self.pushKitToken.length == 0) return;
  393. for (tableAccount *result in [[NCManageDatabase sharedInstance] getAllAccount]) {
  394. NSString *token = [CCUtility getPushNotificationToken:result.account];
  395. if (![token isEqualToString:self.pushKitToken]) {
  396. if (token != nil) {
  397. // unsubscribing + subscribing
  398. [self unsubscribingNextcloudServerPushNotification:result.account urlBase:result.urlBase user:result.user withSubscribing:true];
  399. } else {
  400. [self subscribingNextcloudServerPushNotification:result.account urlBase:result.urlBase user:result.user];
  401. }
  402. }
  403. }
  404. }
  405. - (void)subscribingNextcloudServerPushNotification:(NSString *)account urlBase:(NSString *)urlBase user:(NSString *)user
  406. {
  407. // test
  408. if (self.account.length == 0 || self.pushKitToken.length == 0) return;
  409. [[NCPushNotificationEncryption sharedInstance] generatePushNotificationsKeyPair:account];
  410. NSString *pushTokenHash = [[NCEndToEndEncryption sharedManager] createSHA512:self.pushKitToken];
  411. NSData *pushPublicKey = [CCUtility getPushNotificationPublicKey:account];
  412. NSString *pushDevicePublicKey = [[NSString alloc] initWithData:pushPublicKey encoding:NSUTF8StringEncoding];
  413. NSString *proxyServerPath = [NCBrandOptions sharedInstance].pushNotificationServerProxy;
  414. [[NCCommunication shared] subscribingPushNotificationWithServerUrl:urlBase account:account user:user password:[CCUtility getPassword:account] pushTokenHash:pushTokenHash devicePublicKey:pushDevicePublicKey proxyServerUrl:proxyServerPath customUserAgent:nil addCustomHeaders:nil completionHandler:^(NSString *account, NSString *deviceIdentifier, NSString *signature, NSString *publicKey, NSInteger errorCode, NSString *errorDescription) {
  415. if (errorCode == 0) {
  416. NSString *userAgent = [NSString stringWithFormat:@"%@ (Strict VoIP)", [CCUtility getUserAgent]];
  417. [[NCCommunication shared] subscribingPushProxyWithProxyServerUrl:proxyServerPath pushToken:self.pushKitToken deviceIdentifier:deviceIdentifier signature:signature publicKey:publicKey userAgent:userAgent completionHandler:^(NSInteger errorCode, NSString *errorDescription) {
  418. if (errorCode == 0) {
  419. [[NCCommunicationCommon shared] writeLog:@"Subscribed to Push Notification server & proxy successfully"];
  420. [CCUtility setPushNotificationToken:account token:self.pushKitToken];
  421. [CCUtility setPushNotificationDeviceIdentifier:account deviceIdentifier:deviceIdentifier];
  422. [CCUtility setPushNotificationDeviceIdentifierSignature:account deviceIdentifierSignature:signature];
  423. [CCUtility setPushNotificationSubscribingPublicKey:account publicKey:publicKey];
  424. }
  425. }];
  426. }
  427. }];
  428. }
  429. - (void)unsubscribingNextcloudServerPushNotification:(NSString *)account urlBase:(NSString *)urlBase user:(NSString *)user withSubscribing:(BOOL)subscribing
  430. {
  431. if (self.account.length == 0) return;
  432. NSString *deviceIdentifier = [CCUtility getPushNotificationDeviceIdentifier:account];
  433. NSString *signature = [CCUtility getPushNotificationDeviceIdentifierSignature:account];
  434. NSString *publicKey = [CCUtility getPushNotificationSubscribingPublicKey:account];
  435. [[NCCommunication shared] unsubscribingPushNotificationWithServerUrl:urlBase account:account user:user password:[CCUtility getPassword:account] customUserAgent:nil addCustomHeaders:nil completionHandler:^(NSString *account, NSInteger errorCode, NSString *errorDescription) {
  436. if (errorCode == 0) {
  437. NSString *userAgent = [NSString stringWithFormat:@"%@ (Strict VoIP)", [CCUtility getUserAgent]];
  438. NSString *proxyServerPath = [NCBrandOptions sharedInstance].pushNotificationServerProxy;
  439. [[NCCommunication shared] unsubscribingPushProxyWithProxyServerUrl:proxyServerPath deviceIdentifier:deviceIdentifier signature:signature publicKey:publicKey userAgent:userAgent completionHandler:^(NSInteger errorCode, NSString *errorDescription) {
  440. if (errorCode == 0) {
  441. [[NCCommunicationCommon shared] writeLog:@"Unsubscribed to Push Notification server & proxy successfully."];
  442. [CCUtility setPushNotificationPublicKey:account data:nil];
  443. [CCUtility setPushNotificationSubscribingPublicKey:account publicKey:nil];
  444. [CCUtility setPushNotificationPrivateKey:account data:nil];
  445. [CCUtility setPushNotificationToken:account token:nil];
  446. [CCUtility setPushNotificationDeviceIdentifier:account deviceIdentifier:nil];
  447. [CCUtility setPushNotificationDeviceIdentifierSignature:account deviceIdentifierSignature:nil];
  448. if (self.pushKitToken != nil && subscribing) {
  449. [self subscribingNextcloudServerPushNotification:account urlBase:urlBase user:user];
  450. }
  451. }
  452. }];
  453. }
  454. }];
  455. }
  456. -(void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler
  457. {
  458. //Called when a notification is delivered to a foreground app.
  459. completionHandler(UNNotificationPresentationOptionAlert);
  460. }
  461. -(void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(nonnull UNNotificationResponse *)response withCompletionHandler:(nonnull void (^)(void))completionHandler
  462. {
  463. completionHandler();
  464. }
  465. - (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
  466. {
  467. self.pushKitToken = [self stringWithDeviceToken:deviceToken];
  468. [self pushNotification];
  469. }
  470. - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
  471. {
  472. NSString *message = [userInfo objectForKey:@"subject"];
  473. if (message) {
  474. NSArray *results = [[NCManageDatabase sharedInstance] getAllAccount];
  475. for (tableAccount *result in results) {
  476. if ([CCUtility getPushNotificationPrivateKey:result.account]) {
  477. NSData *decryptionKey = [CCUtility getPushNotificationPrivateKey:result.account];
  478. NSString *decryptedMessage = [[NCPushNotificationEncryption sharedInstance] decryptPushNotification:message withDevicePrivateKey:decryptionKey];
  479. if (decryptedMessage) {
  480. NSData *data = [decryptedMessage dataUsingEncoding:NSUTF8StringEncoding];
  481. NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
  482. NSInteger nid = [[json objectForKey:@"nid"] integerValue];
  483. BOOL delete = [[json objectForKey:@"delete"] boolValue];
  484. BOOL deleteAll = [[json objectForKey:@"delete-all"] boolValue];
  485. if (delete) {
  486. [self removeNotificationWithNotificationId:nid usingDecryptionKey:decryptionKey];
  487. } else if (deleteAll) {
  488. [self cleanAllNotifications];
  489. }
  490. }
  491. }
  492. }
  493. }
  494. completionHandler(UIBackgroundFetchResultNoData);
  495. }
  496. - (void)cleanAllNotifications
  497. {
  498. [[UNUserNotificationCenter currentNotificationCenter] removeAllDeliveredNotifications];
  499. }
  500. - (void)removeNotificationWithNotificationId:(NSInteger)notificationId usingDecryptionKey:(NSData *)key
  501. {
  502. // Check in pending notifications
  503. [[UNUserNotificationCenter currentNotificationCenter] getPendingNotificationRequestsWithCompletionHandler:^(NSArray<UNNotificationRequest *> * _Nonnull requests) {
  504. for (UNNotificationRequest *notificationRequest in requests) {
  505. NSString *message = [notificationRequest.content.userInfo objectForKey:@"subject"];
  506. NSString *decryptedMessage = [[NCPushNotificationEncryption sharedInstance] decryptPushNotification:message withDevicePrivateKey:key];
  507. if (decryptedMessage) {
  508. NSData *data = [decryptedMessage dataUsingEncoding:NSUTF8StringEncoding];
  509. NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
  510. NSInteger nid = [[json objectForKey:@"nid"] integerValue];
  511. if (nid == notificationId) {
  512. [[UNUserNotificationCenter currentNotificationCenter] removePendingNotificationRequestsWithIdentifiers:@[notificationRequest.identifier]];
  513. }
  514. }
  515. }
  516. }];
  517. // Check in delivered notifications
  518. [[UNUserNotificationCenter currentNotificationCenter] getDeliveredNotificationsWithCompletionHandler:^(NSArray<UNNotification *> * _Nonnull notifications) {
  519. for (UNNotification *notification in notifications) {
  520. NSString *message = [notification.request.content.userInfo objectForKey:@"subject"];
  521. NSString *decryptedMessage = [[NCPushNotificationEncryption sharedInstance] decryptPushNotification:message withDevicePrivateKey:key];
  522. if (decryptedMessage) {
  523. NSData *data = [decryptedMessage dataUsingEncoding:NSUTF8StringEncoding];
  524. NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
  525. NSInteger nid = [[json objectForKey:@"nid"] integerValue];
  526. if (nid == notificationId) {
  527. [[UNUserNotificationCenter currentNotificationCenter] removeDeliveredNotificationsWithIdentifiers:@[notification.request.identifier]];
  528. }
  529. }
  530. }
  531. }];
  532. }
  533. - (NSString *)stringWithDeviceToken:(NSData *)deviceToken
  534. {
  535. const char *data = [deviceToken bytes];
  536. NSMutableString *token = [NSMutableString string];
  537. for (NSUInteger i = 0; i < [deviceToken length]; i++) {
  538. [token appendFormat:@"%02.2hhX", data[i]];
  539. }
  540. return [token copy];
  541. }
  542. #pragma --------------------------------------------------------------------------------------------
  543. #pragma mark ===== ApplicationIconBadgeNumber =====
  544. #pragma --------------------------------------------------------------------------------------------
  545. - (void)updateApplicationIconBadgeNumber
  546. {
  547. if (self.account.length == 0) return;
  548. NSInteger counterDownload = [[NCOperationQueue shared] downloadCount];
  549. NSInteger counterUpload = [[NCManageDatabase sharedInstance] getMetadatasWithPredicate:[NSPredicate predicateWithFormat:@"status == %d OR status == %d OR status == %d", k_metadataStatusWaitUpload, k_metadataStatusInUpload, k_metadataStatusUploading]].count;
  550. NSInteger total = counterDownload + counterUpload;
  551. [UIApplication sharedApplication].applicationIconBadgeNumber = total;
  552. UITabBarController *tabBarController = (UITabBarController *)self.window.rootViewController;
  553. if ([tabBarController isKindOfClass:[UITabBarController class]]) {
  554. UITabBarItem *tabBarItem = [tabBarController.tabBar.items objectAtIndex:0];
  555. if (total > 0) {
  556. [tabBarItem setBadgeValue:[NSString stringWithFormat:@"%li", (unsigned long)total]];
  557. } else {
  558. [tabBarItem setBadgeValue:nil];
  559. }
  560. }
  561. }
  562. #pragma --------------------------------------------------------------------------------------------
  563. #pragma mark ===== Fetch =====
  564. #pragma --------------------------------------------------------------------------------------------
  565. - (void)application:(UIApplication *)application performFetchWithCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
  566. {
  567. if (self.account.length == 0) {
  568. completionHandler(UIBackgroundFetchResultNoData);
  569. return;
  570. }
  571. [[NCCommunicationCommon shared] writeLog:@"Start perform Fetch With Completion Handler"];
  572. // Verify new photo
  573. [[NCAutoUpload sharedInstance] initStateAutoUpload];
  574. // after 20 sec
  575. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 20 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
  576. [[NCCommunicationCommon shared] writeLog:@"End 20 sec. perform Fetch With Completion Handler"];
  577. completionHandler(UIBackgroundFetchResultNoData);
  578. });
  579. }
  580. #pragma --------------------------------------------------------------------------------------------
  581. #pragma mark ===== Operation Networking & Session =====
  582. #pragma --------------------------------------------------------------------------------------------
  583. //
  584. // Method called by the system when all the background task has end
  585. //
  586. - (void)application:(UIApplication *)application handleEventsForBackgroundURLSession:(NSString *)identifier completionHandler:(void (^)(void))completionHandler
  587. {
  588. [[NCCommunicationCommon shared] writeLog:[NSString stringWithFormat:@"Start handle Events For Background URLSession: %@", identifier]];
  589. [self updateApplicationIconBadgeNumber];
  590. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 20 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
  591. self.backgroundSessionCompletionHandler = completionHandler;
  592. void (^completionHandler)() = self.backgroundSessionCompletionHandler;
  593. self.backgroundSessionCompletionHandler = nil;
  594. completionHandler();
  595. });
  596. }
  597. #pragma --------------------------------------------------------------------------------------------
  598. #pragma mark ===== OpenURL =====
  599. #pragma --------------------------------------------------------------------------------------------
  600. // Method called from iOS system to send a file from other app.
  601. - (BOOL)application:(UIApplication *)application openURL:(NSURL *)url options:(NSDictionary<NSString *,id> *)options
  602. {
  603. if (self.account.length == 0) return YES;
  604. NSString *scheme = url.scheme;
  605. NSString *fileName;
  606. NSString *serverUrl;
  607. if ([scheme isEqualToString:@"nextcloud"]) {
  608. NSString *action = url.host;
  609. if ([action isEqualToString:@"open-file"]) {
  610. NSURLComponents *urlComponents = [NSURLComponents componentsWithURL:url resolvingAgainstBaseURL:NO];
  611. NSArray *queryItems = urlComponents.queryItems;
  612. NSString *user = [CCUtility valueForKey:@"user" fromQueryItems:queryItems];
  613. NSString *path = [CCUtility valueForKey:@"path" fromQueryItems:queryItems];
  614. NSString *link = [CCUtility valueForKey:@"link" fromQueryItems:queryItems];
  615. tableAccount *matchedAccount = nil;
  616. // verify parameter
  617. if (user.length == 0 || path.length == 0 || [[NSURL URLWithString:link] host].length == 0) {
  618. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"_error_", nil) message:NSLocalizedString(@"_error_parameter_schema_", nil) preferredStyle:UIAlertControllerStyleAlert];
  619. UIAlertAction *okAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"_ok_", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {}];
  620. [alertController addAction:okAction];
  621. [self.window.rootViewController presentViewController:alertController animated:YES completion:nil];
  622. } else {
  623. tableAccount *account = [[NCManageDatabase sharedInstance] getAccountActive];
  624. if (account) {
  625. NSURL *accountURL = [NSURL URLWithString:account.urlBase];
  626. NSString *accountUser = account.user;
  627. if ([link containsString:accountURL.host] && [user isEqualToString:accountUser]) {
  628. matchedAccount = account;
  629. } else {
  630. NSArray *accounts = [[NCManageDatabase sharedInstance] getAllAccount];
  631. for (tableAccount *account in accounts) {
  632. NSURL *accountURL = [NSURL URLWithString:account.urlBase];
  633. NSString *accountUser = account.user;
  634. if ([link containsString:accountURL.host] && [user isEqualToString:accountUser]) {
  635. matchedAccount = [[NCManageDatabase sharedInstance] setAccountActive:account.account];
  636. [self settingAccount:matchedAccount.account urlBase:matchedAccount.urlBase user:matchedAccount.user userID:matchedAccount.userID password:[CCUtility getPassword:matchedAccount.account]];
  637. [[NSNotificationCenter defaultCenter] postNotificationOnMainThreadName:k_notificationCenter_initializeMain object:nil userInfo:nil];
  638. }
  639. }
  640. }
  641. if (matchedAccount) {
  642. NSString *webDAV = [[NCUtility shared] getWebDAVWithAccount:self.account];
  643. if ([path containsString:@"/"]) {
  644. fileName = [path lastPathComponent];
  645. serverUrl = [NSString stringWithFormat:@"%@/%@/%@", matchedAccount.urlBase, webDAV, [path stringByDeletingLastPathComponent]];
  646. } else {
  647. fileName = path;
  648. serverUrl = [NSString stringWithFormat:@"%@/%@", matchedAccount.urlBase, webDAV];
  649. }
  650. [[NCCollectionCommon shared] openFileViewInFolderWithServerUrl:serverUrl fileName:fileName];
  651. } else {
  652. NSString *domain = [[NSURL URLWithString:link] host];
  653. NSString *fileName = [path lastPathComponent];
  654. NSString *message = [NSString stringWithFormat:NSLocalizedString(@"_account_not_available_", nil), user, domain, fileName];
  655. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"_info_", nil) message:message preferredStyle:UIAlertControllerStyleAlert];
  656. UIAlertAction *okAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"_ok_", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {}];
  657. [alertController addAction:okAction];
  658. [self.window.rootViewController presentViewController:alertController animated:YES completion:nil];
  659. }
  660. }
  661. }
  662. }
  663. return YES;
  664. }
  665. NSError *error;
  666. NSLog(@"[LOG] the path is: %@", url.path);
  667. NSArray *splitedUrl = [url.path componentsSeparatedByString:@"/"];
  668. self.fileNameUpload = [NSString stringWithFormat:@"%@",[splitedUrl objectAtIndex:([splitedUrl count]-1)]];
  669. if (self.account) {
  670. [[NSFileManager defaultManager]removeItemAtPath:[NSTemporaryDirectory() stringByAppendingString:self.fileNameUpload] error:nil];
  671. [[NSFileManager defaultManager]moveItemAtPath:url.path toPath:[NSTemporaryDirectory() stringByAppendingString:self.fileNameUpload] error:&error];
  672. if (error == nil) {
  673. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 1 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
  674. UIViewController *uploadNavigationViewController = [[UIStoryboard storyboardWithName:@"CCUploadFromOtherUpp" bundle:nil] instantiateViewControllerWithIdentifier:@"CCUploadNavigationViewController"];
  675. [self.window.rootViewController presentViewController:uploadNavigationViewController animated:YES completion:nil];
  676. });
  677. }
  678. }
  679. return YES;
  680. }
  681. #pragma --------------------------------------------------------------------------------------------
  682. #pragma mark ===== Passcode + Delegate =====
  683. #pragma --------------------------------------------------------------------------------------------
  684. - (void)passcodeWithAutomaticallyPromptForBiometricValidation:(BOOL)automaticallyPromptForBiometricValidation
  685. {
  686. LAContext *laContext = [LAContext new];
  687. NSError *error;
  688. BOOL isBiometryAvailable = false;
  689. if ([[CCUtility getPasscode] length] == 0 || [self.account length] == 0 || [CCUtility getNotPasscodeAtStart]) return;
  690. if (self.passcodeViewController == nil) {
  691. self.passcodeViewController = [[TOPasscodeViewController alloc] initWithStyle:TOPasscodeViewStyleTranslucentLight passcodeType:TOPasscodeTypeSixDigits];
  692. if (@available(iOS 13.0, *)) {
  693. if ([[UITraitCollection currentTraitCollection] userInterfaceStyle] == UIUserInterfaceStyleDark) {
  694. self.passcodeViewController.style = TOPasscodeViewStyleTranslucentDark;
  695. }
  696. }
  697. self.passcodeViewController.delegate = self;
  698. self.passcodeViewController.keypadButtonShowLettering = false;
  699. if (CCUtility.getEnableTouchFaceID && [laContext canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:&error]) {
  700. if (error == NULL) {
  701. if (laContext.biometryType == LABiometryTypeFaceID) {
  702. self.passcodeViewController.biometryType = TOPasscodeBiometryTypeFaceID;
  703. self.passcodeViewController.allowBiometricValidation = true;
  704. isBiometryAvailable = true;
  705. } else if (laContext.biometryType == LABiometryTypeTouchID) {
  706. self.passcodeViewController.biometryType = TOPasscodeBiometryTypeTouchID;
  707. self.passcodeViewController.allowBiometricValidation = true;
  708. isBiometryAvailable = true;
  709. } else {
  710. isBiometryAvailable = false;
  711. NSLog(@"No Biometric support");
  712. }
  713. }
  714. }
  715. [self.window.rootViewController presentViewController:self.passcodeViewController animated:YES completion:^{
  716. [self enableTouchFaceID:automaticallyPromptForBiometricValidation];
  717. }];
  718. } else {
  719. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 0.5 * NSEC_PER_SEC), dispatch_get_main_queue(), ^(void) {
  720. [self enableTouchFaceID:automaticallyPromptForBiometricValidation];
  721. });
  722. }
  723. }
  724. - (void)didInputCorrectPasscodeInPasscodeViewController:(TOPasscodeViewController *)passcodeViewController
  725. {
  726. [passcodeViewController dismissViewControllerAnimated:YES completion:^{
  727. self.passcodeViewController = nil;
  728. }];
  729. }
  730. - (BOOL)passcodeViewController:(TOPasscodeViewController *)passcodeViewController isCorrectCode:(NSString *)code
  731. {
  732. return [code isEqualToString:[CCUtility getPasscode]];
  733. }
  734. - (void)didPerformBiometricValidationRequestInPasscodeViewController:(TOPasscodeViewController *)passcodeViewController
  735. {
  736. [[LAContext new] evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics localizedReason:[[NCBrandOptions sharedInstance] brand] reply:^(BOOL success, NSError * _Nullable error) {
  737. if (success) {
  738. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 0.5 * NSEC_PER_SEC), dispatch_get_main_queue(), ^(void) {
  739. [passcodeViewController dismissViewControllerAnimated:YES completion:^{
  740. self.passcodeViewController = nil;
  741. }];
  742. });
  743. }
  744. }];
  745. }
  746. - (void)enableTouchFaceID:(BOOL)automaticallyPromptForBiometricValidation
  747. {
  748. if (CCUtility.getEnableTouchFaceID && automaticallyPromptForBiometricValidation && self.passcodeViewController.view.window) {
  749. [[LAContext new] evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics localizedReason:[[NCBrandOptions sharedInstance] brand] reply:^(BOOL success, NSError * _Nullable error) {
  750. if (success) {
  751. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 0.5 * NSEC_PER_SEC), dispatch_get_main_queue(), ^(void) {
  752. [self.passcodeViewController dismissViewControllerAnimated:YES completion:^{
  753. self.passcodeViewController = nil;
  754. }];
  755. });
  756. }
  757. }];
  758. }
  759. }
  760. @end