123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783 |
- //
- // AppDelegate.m
- // Nextcloud
- //
- // Created by Marino Faggiana on 04/09/14.
- // Copyright (c) 2014 Marino Faggiana. All rights reserved.
- //
- // Author Marino Faggiana <marino.faggiana@nextcloud.com>
- //
- // This program is free software: you can redistribute it and/or modify
- // it under the terms of the GNU General Public License as published by
- // the Free Software Foundation, either version 3 of the License, or
- // (at your option) any later version.
- //
- // This program is distributed in the hope that it will be useful,
- // but WITHOUT ANY WARRANTY; without even the implied warranty of
- // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- // GNU General Public License for more details.
- //
- // You should have received a copy of the GNU General Public License
- // along with this program. If not, see <http://www.gnu.org/licenses/>.
- //
- #import "AppDelegate.h"
- #import "NCBridgeSwift.h"
- #import "NSNotificationCenter+MainThread.h"
- #import "NCPushNotification.h"
- #import <QuartzCore/QuartzCore.h>
- @import Firebase;
- @class NCViewerRichdocument;
- @interface AppDelegate() <TOPasscodeViewControllerDelegate>
- @end
- @implementation AppDelegate
- + (void)initialize
- {
- [[NSUserDefaults standardUserDefaults] registerDefaults:@{@"UserAgent": [CCUtility getUserAgent]}];
- }
- - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
- {
- BOOL isSimulatorOrTestFlight = [[NCUtility shared] isSimulatorOrTestFlight];
-
- if (![CCUtility getDisableCrashservice] && NCBrandOptions.shared.disable_crash_service == false) {
- [FIRApp configure];
- }
-
- [CCUtility createDirectoryStandard];
- [CCUtility emptyTemporaryDirectory];
-
- // Networking
- [[NCCommunicationCommon shared] setupWithDelegate:[NCNetworking shared]];
- [[NCCommunicationCommon shared] setupWithUserAgent:[CCUtility getUserAgent]];
-
- // LOG
- NSInteger levelLog = [CCUtility getLogLevel];
- [[NCCommunicationCommon shared] setLevelLog:levelLog];
- [[NCCommunicationCommon shared] setPrintLog:true];
- NSString *versionApp = [NSString stringWithFormat:@"%@.%@", [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleShortVersionString"], [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleVersion"]];
- NSString *versionNextcloudiOS = [NSString stringWithFormat:[NCBrandOptions shared].textCopyrightNextcloudiOS, versionApp];
- if (isSimulatorOrTestFlight) {
- [[NCCommunicationCommon shared] writeLog:[NSString stringWithFormat:@"Start session with level %lu %@ (Simulator / TestFlight)", (unsigned long)levelLog, versionNextcloudiOS]];
- } else {
- [[NCCommunicationCommon shared] writeLog:[NSString stringWithFormat:@"Start session with level %lu %@", (unsigned long)levelLog, versionNextcloudiOS]];
- }
-
- [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(initializeMain:) name:NCBrandGlobal.shared.notificationCenterInitializeMain object:nil];
-
- // Set account, if no exists clear all
- tableAccount *tableAccount = [[NCManageDatabase shared] getAccountActive];
- if (tableAccount == nil) {
- // remove all the keys Chain
- [CCUtility deleteAllChainStore];
- // remove all the App group key
- [[NSUserDefaults standardUserDefaults] removePersistentDomainForName:[[NSBundle mainBundle] bundleIdentifier]];
- } else {
- // FIX 3.0.5 lost urlbase
- if (tableAccount.urlBase.length == 0) {
- NSString *user = [tableAccount.user stringByAppendingString:@" "];
- NSString *urlBase = [tableAccount.account stringByReplacingOccurrencesOfString:user withString:@""];
- tableAccount.urlBase = urlBase;
- [[NCManageDatabase shared] updateAccount:tableAccount];
-
- tableAccount = [[NCManageDatabase shared] getAccountActive];
- }
- [self settingAccount:tableAccount.account urlBase:tableAccount.urlBase user:tableAccount.user userID:tableAccount.userID password:[CCUtility getPassword:tableAccount.account]];
- }
-
- // UserDefaults
- self.ncUserDefaults = [[NSUserDefaults alloc] initWithSuiteName:[NCBrandOptions shared].capabilitiesGroups];
-
- // Background Fetch
- [application setMinimumBackgroundFetchInterval:UIApplicationBackgroundFetchIntervalMinimum];
- self.listProgressMetadata = [NSMutableDictionary new];
- self.listFilesVC = [NSMutableDictionary new];
- self.listFavoriteVC = [NSMutableDictionary new];
- self.listOfflineVC = [NSMutableDictionary new];
-
- self.pasteboardOcIds = [NSMutableArray new];
- // Push Notification
- [application registerForRemoteNotifications];
-
- // Display notification
- [UNUserNotificationCenter currentNotificationCenter].delegate = self;
- UNAuthorizationOptions authOptions = UNAuthorizationOptionAlert | UNAuthorizationOptionSound | UNAuthorizationOptionBadge;
- [[UNUserNotificationCenter currentNotificationCenter] requestAuthorizationWithOptions:authOptions completionHandler:^(BOOL granted, NSError * _Nullable error) { }];
-
- //AV Session
- [[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayback error:nil];
- [[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
-
- // Start Timer
- [self startTimerErrorNetworking];
- // Store review
- if ([[NCUtility shared] isSimulatorOrTestFlight] == false) {
- NCStoreReview *review = [NCStoreReview new];
- [review incrementAppRuns];
- [review showStoreReview];
- }
-
- // Detect Dark mode
- if (@available(iOS 13.0, *)) {
- if ([CCUtility getDarkModeDetect]) {
- if ([[UITraitCollection currentTraitCollection] userInterfaceStyle] == UIUserInterfaceStyleDark) {
- [CCUtility setDarkMode:YES];
- } else {
- [CCUtility setDarkMode:NO];
- }
- }
- }
-
- if ([NCBrandOptions shared].disable_intro) {
- [CCUtility setIntro:YES];
-
- if (self.account == nil || self.account.length == 0) {
- [self openLoginView:nil selector:NCBrandGlobal.shared.introLogin openLoginWeb:false];
- }
-
- } else {
-
- if ([CCUtility getIntro] == NO) {
- UIViewController *introViewController = [[UIStoryboard storyboardWithName:@"NCIntro" bundle:[NSBundle mainBundle]] instantiateInitialViewController];
-
- UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController: introViewController];
- self.window.rootViewController = navController;
- [self.window makeKeyAndVisible];
- }
- }
- // init home
- [[NSNotificationCenter defaultCenter] postNotificationOnMainThreadName:NCBrandGlobal.shared.notificationCenterInitializeMain object:nil userInfo:nil];
- // Passcode
- dispatch_async(dispatch_get_main_queue(), ^{
- [self passcodeWithAutomaticallyPromptForBiometricValidation:true];
- });
-
- // Auto upload
- self.networkingAutoUpload = [NCNetworkingAutoUpload new];
-
- // Background task
- if (@available(iOS 13.0, *)) {
- [[BGTaskScheduler sharedScheduler] registerForTaskWithIdentifier:NCBrandGlobal.shared.backgroudTask usingQueue:nil launchHandler:^(BGTask *task) {
- [self handleBackgroundTask:task];
- }];
- }
-
- return YES;
- }
- //
- // L' applicazione si dimetterà dallo stato di attivo
- //
- - (void)applicationWillResignActive:(UIApplication *)application
- {
- if (self.account == nil || self.account.length == 0) { return; }
-
- // Dismiss FileViewInFolder
- if (self.activeFileViewInFolder != nil ) {
- [self.activeFileViewInFolder dismissViewControllerAnimated:false completion:^{
- self.activeFileViewInFolder = nil;
- }];
- }
- }
- //
- // L' applicazione entrerà in primo piano (attivo solo dopo il background)
- //
- - (void)applicationWillEnterForeground:(UIApplication *)application
- {
- if (self.account == nil || self.account.length == 0) { return; }
-
- [[NSNotificationCenter defaultCenter] postNotificationOnMainThreadName:NCBrandGlobal.shared.notificationCenterApplicationWillEnterForeground object:nil];
-
- // Request Passcode
- [self passcodeWithAutomaticallyPromptForBiometricValidation:true];
-
- // Initialize Auto upload
- [[NCAutoUpload shared] initAutoUploadWithViewController:nil];
-
- // Read active directory
- [[NSNotificationCenter defaultCenter] postNotificationOnMainThreadName:NCBrandGlobal.shared.notificationCenterReloadDataSourceNetworkForced object:nil];
-
- // Required unsubscribing / subscribing
- [[NCPushNotification shared] pushNotification];
-
- // RichDocument
- [[NSNotificationCenter defaultCenter] postNotificationOnMainThreadName:NCBrandGlobal.shared.notificationCenterRichdocumentGrabFocus object:nil];
-
- // Request Service Server Nextcloud
- [[NCService shared] startRequestServicesServer];
- }
- //
- // L' applicazione entrerà in primo piano (attivo sempre)
- //
- - (void)applicationDidBecomeActive:(UIApplication *)application
- {
- if (self.account == nil || self.account.length == 0) { return; }
-
- // Brand
- #if defined(HC)
- tableAccount *account = [[NCManageDatabase shared] getAccountActive];
- if (account.hcIsTrial == true || account.hcTrialExpired == true || account.hcNextGroupExpirationGroupExpired == true) {
-
- HCTrial *vc = [[UIStoryboard storyboardWithName:@"HCTrial" bundle:nil] instantiateInitialViewController];
- vc.account = account;
-
- [self.window.rootViewController presentViewController:vc animated:YES completion:nil];
- }
- #endif
-
- [[NCNetworking shared] verifyUploadZombie];
- }
- //
- // L' applicazione è entrata nello sfondo
- //
- - (void)applicationDidEnterBackground:(UIApplication *)application
- {
- if (self.account == nil || self.account.length == 0) { return; }
- [[NSNotificationCenter defaultCenter] postNotificationOnMainThreadName:NCBrandGlobal.shared.notificationCenterApplicationDidEnterBackground object:nil];
-
- [self passcodeWithAutomaticallyPromptForBiometricValidation:false];
-
- if (@available(iOS 13.0, *)) {
- [self scheduleBackgroundTask];
- }
- }
- //
- // L'applicazione terminerà
- //
- - (void)applicationWillTerminate:(UIApplication *)application
- {
- [[NCCommunicationCommon shared] writeLog:@"bye bye"];
- }
- // NotificationCenter
- - (void)initializeMain:(NSNotification *)notification
- {
- if (self.account == nil || self.account.length == 0) { return; }
-
- // Clear error certificate
- [CCUtility setCertificateError:self.account error:NO];
-
- // Setting Theming
- [[NCBrandColor shared] settingThemingColorWithAccount:self.account];
-
- // close detail
- [[NSNotificationCenter defaultCenter] postNotificationOnMainThreadName:NCBrandGlobal.shared.notificationCenterMenuDetailClose object:nil];
-
- // Start Auto Upload
- [[NCAutoUpload shared] initAutoUploadWithViewController:nil];
-
- // Start services
- [[NCService shared] startRequestServicesServer];
-
- // Registeration push notification
- [[NCPushNotification shared] pushNotification];
-
- // Registeration domain File Provider
- //FileProviderDomain *fileProviderDomain = [FileProviderDomain new];
- //[fileProviderDomain removeAllDomains];
- //[fileProviderDomain registerDomains];
-
- [[NCCommunicationCommon shared] writeLog:@"initialize Main"];
- }
- #pragma mark Login / checkErrorNetworking
- - (void)checkErrorNetworking
- {
- if (self.account == nil || self.account.length == 0) { return; }
-
- // check unauthorized server (401)
- if ([CCUtility getPassword:self.account].length == 0) {
-
- [self openLoginView:self.window.rootViewController selector:NCBrandGlobal.shared.introLogin openLoginWeb:true];
- }
-
- // check certificate untrusted (-1202)
- if ([CCUtility getCertificateError:self.account]) {
-
- UIAlertController *alertController = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"_ssl_certificate_untrusted_", nil) message:NSLocalizedString(@"_connect_server_anyway_", nil) preferredStyle:UIAlertControllerStyleAlert];
- [alertController addAction: [UIAlertAction actionWithTitle:NSLocalizedString(@"_yes_", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
- [[NCNetworking shared] wrtiteCertificateWithDirectoryCertificate:[CCUtility getDirectoryCerificates]];
- [self startTimerErrorNetworking];
- }]];
-
- [alertController addAction: [UIAlertAction actionWithTitle:NSLocalizedString(@"_no_", nil) style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
- [self startTimerErrorNetworking];
- }]];
- [self.window.rootViewController presentViewController:alertController animated:YES completion:^{
- // Stop timer error network
- [self.timerErrorNetworking invalidate];
- }];
- }
- }
- - (void)openLoginView:(UIViewController *)viewController selector:(NSInteger)selector openLoginWeb:(BOOL)openLoginWeb
- {
- // use appConfig [MDM]
- if ([NCBrandOptions shared].use_configuration) {
-
- if (!(_appConfigView.isViewLoaded && _appConfigView.view.window)) {
-
- self.appConfigView = [[UIStoryboard storyboardWithName:@"CCLogin" bundle:nil] instantiateViewControllerWithIdentifier:@"NCAppConfigView"];
-
- [self showLoginViewController:self.appConfigView forContext:viewController];
- }
-
- return;
- }
-
- // only for personalized LoginWeb [customer]
- if ([NCBrandOptions shared].use_login_web_personalized) {
-
- if (!(_activeLoginWeb.isViewLoaded && _activeLoginWeb.view.window)) {
-
- self.activeLoginWeb = [[UIStoryboard storyboardWithName:@"CCLogin" bundle:nil] instantiateViewControllerWithIdentifier:@"NCLoginWeb"];
- self.activeLoginWeb.urlBase = [[NCBrandOptions shared] loginBaseUrl];
- [self showLoginViewController:self.activeLoginWeb forContext:viewController];
- }
-
- return;
- }
-
- // normal login
- if (selector == NCBrandGlobal.shared.introSignup) {
-
- if (!(_activeLoginWeb.isViewLoaded && _activeLoginWeb.view.window)) {
-
- self.activeLoginWeb = [[UIStoryboard storyboardWithName:@"CCLogin" bundle:nil] instantiateViewControllerWithIdentifier:@"NCLoginWeb"];
-
- if (selector == NCBrandGlobal.shared.introSignup) {
- self.activeLoginWeb.urlBase = [[NCBrandOptions shared] linkloginPreferredProviders];
- } else {
- self.activeLoginWeb.urlBase = self.urlBase;
- }
-
- [self showLoginViewController:self.activeLoginWeb forContext:viewController];
- }
-
- } else if ([NCBrandOptions shared].disable_intro && [NCBrandOptions shared].disable_request_login_url) {
-
- self.activeLoginWeb = [[UIStoryboard storyboardWithName:@"CCLogin" bundle:nil] instantiateViewControllerWithIdentifier:@"NCLoginWeb"];
- self.activeLoginWeb.urlBase = [[NCBrandOptions shared] loginBaseUrl];
-
- [self showLoginViewController:self.activeLoginWeb forContext:viewController];
-
- } else if (openLoginWeb) {
-
- if (!(_activeLoginWeb.isViewLoaded && _activeLoginWeb.view.window)) {
- self.activeLoginWeb = [[UIStoryboard storyboardWithName:@"CCLogin" bundle:nil] instantiateViewControllerWithIdentifier:@"NCLoginWeb"];
- self.activeLoginWeb.urlBase = self.urlBase;
- [self showLoginViewController:self.activeLoginWeb forContext:viewController];
- }
-
- } else {
-
- if (!(_activeLogin.isViewLoaded && _activeLogin.view.window)) {
-
- _activeLogin = [[UIStoryboard storyboardWithName:@"CCLogin" bundle:nil] instantiateViewControllerWithIdentifier:@"CCLoginNextcloud"];
-
- [self showLoginViewController:_activeLogin forContext:viewController];
- }
- }
- }
- -(void)showLoginViewController:(UIViewController *)viewController forContext:(UIViewController *)contextViewController
- {
- if (contextViewController == NULL) {
-
- UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:viewController];
- navigationController.navigationBar.barStyle = UIBarStyleBlack;
- navigationController.navigationBar.tintColor = NCBrandColor.shared.customerText;
- navigationController.navigationBar.barTintColor = NCBrandColor.shared.customer;
- [navigationController.navigationBar setTranslucent:false];
- self.window.rootViewController = navigationController;
-
- [self.window makeKeyAndVisible];
-
- } else if ([contextViewController isKindOfClass:[UINavigationController class]]) {
-
- UINavigationController *navigationController = ((UINavigationController *)contextViewController);
- [navigationController pushViewController:viewController animated:true];
-
- } else {
-
- UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:viewController];
- navigationController.modalPresentationStyle = UIModalPresentationFullScreen;
- navigationController.navigationBar.barStyle = UIBarStyleBlack;
- navigationController.navigationBar.tintColor = NCBrandColor.shared.customerText;
- navigationController.navigationBar.barTintColor = NCBrandColor.shared.customer;
- [navigationController.navigationBar setTranslucent:false];
-
- [contextViewController presentViewController:navigationController animated:true completion:nil];
- }
- }
- - (void)startTimerErrorNetworking
- {
- self.timerErrorNetworking = [NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(checkErrorNetworking) userInfo:nil repeats:YES];
- }
- #pragma mark Account & Communication
- - (void)settingAccount:(NSString *)account urlBase:(NSString *)urlBase user:(NSString *)user userID:(NSString *)userID password:(NSString *)password
- {
- self.account = account;
- self.urlBase = urlBase;
- self.user = user;
- self.userID = userID;
- self.password = password;
- (void)[NCNetworkingNotificationCenter shared];
- [[NCCommunicationCommon shared] setupWithAccount:account user:user userId:userID password:password urlBase:urlBase];
-
- NSInteger serverVersionMajor = [[NCManageDatabase shared] getCapabilitiesServerIntWithAccount:account elements:NCElementsJSON.shared.capabilitiesVersionMajor];
- if (serverVersionMajor > 0) {
- [[NCCommunicationCommon shared] setupWithNextcloudVersion:serverVersionMajor];
- }
-
- [[NCCommunicationCommon shared] setupWithWebDav:[[NCUtilityFileSystem shared] getWebDAVWithAccount:account]];
- [[NCCommunicationCommon shared] setupWithDav:[[NCUtilityFileSystem shared] getDAV]];
- }
- - (void)deleteAccount:(NSString *)account wipe:(BOOL)wipe
- {
- // Push Notification
- tableAccount *accountPN = [[NCManageDatabase shared] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
- [[NCPushNotification shared] unsubscribingNextcloudServerPushNotification:accountPN.account urlBase:accountPN.urlBase user:accountPN.user withSubscribing:false];
- [self settingAccount:nil urlBase:nil user:nil userID:nil password:nil];
-
- /* DELETE ALL FILES LOCAL FS */
- NSArray *results = [[NCManageDatabase shared] getTableLocalFilesWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account] sorted:@"ocId" ascending:NO];
- for (tableLocalFile *result in results) {
- [CCUtility removeFileAtPath:[CCUtility getDirectoryProviderStorageOcId:result.ocId]];
- }
- // Clear database
- [[NCManageDatabase shared] clearDatabaseWithAccount:account removeAccount:true];
- [CCUtility clearAllKeysEndToEnd:account];
- [CCUtility clearAllKeysPushNotification:account];
- [CCUtility setCertificateError:account error:false];
- [CCUtility setPassword:account password:nil];
-
- if (wipe) {
- NSArray *listAccount = [[NCManageDatabase shared] getAccounts];
- if ([listAccount count] > 0) {
- NSString *newAccount = listAccount[0];
- tableAccount *tableAccount = [[NCManageDatabase shared] setAccountActive:newAccount];
- [self settingAccount:newAccount urlBase:tableAccount.urlBase user:tableAccount.user userID:tableAccount.userID password:[CCUtility getPassword:tableAccount.account]];
- [[NSNotificationCenter defaultCenter] postNotificationOnMainThreadName:NCBrandGlobal.shared.notificationCenterInitializeMain object:nil userInfo:nil];
- } else {
- [self openLoginView:self.window.rootViewController selector:NCBrandGlobal.shared.introLogin openLoginWeb:false];
- }
- }
- }
- #pragma mark Push Notifications
- -(void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler
- {
- completionHandler(UNNotificationPresentationOptionAlert);
- }
- -(void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(nonnull UNNotificationResponse *)response withCompletionHandler:(nonnull void (^)(void))completionHandler
- {
- completionHandler();
- }
- - (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
- {
- [[NCPushNotification shared] registerForRemoteNotificationsWithDeviceToken:deviceToken];
- }
- - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
- {
- [[NCPushNotification shared] applicationdidReceiveRemoteNotification:userInfo fetchCompletionHandler:^(UIBackgroundFetchResult result) {
- completionHandler(result);
- }];
- }
- #pragma mark Background Task
- -(void)scheduleBackgroundTask
- {
- if (@available(iOS 13.0, *)) {
- NSError *error = NULL;
- // cancel existing task (if any)
- [BGTaskScheduler.sharedScheduler cancelTaskRequestWithIdentifier:NCBrandGlobal.shared.backgroudTask];
- // new task
- BGProcessingTaskRequest *request = [[BGProcessingTaskRequest alloc] initWithIdentifier:NCBrandGlobal.shared.backgroudTask];
- request.requiresNetworkConnectivity = YES;
- request.requiresExternalPower = NO;
- request.earliestBeginDate = [NSDate dateWithTimeIntervalSinceNow:5];
- BOOL success = [[BGTaskScheduler sharedScheduler] submitTaskRequest:request error:&error];
- if (!success) {
- /*
- Here are possible error codes for Domain=BGTaskSchedulerErrorDomain extracted from ObjC headers with some explanation.
- BGTaskSchedulerErrorCodeUnavailable = 1 // Background task scheduling functionality is not available for this app/extension. Background App Refresh may have been disabled in Settings.
- BGTaskSchedulerErrorCodeTooManyPendingTaskRequests = 2 // The task request could not be submitted because there are too many pending task requests of this type. Cancel some existing task requests before trying again.
- BGTaskSchedulerErrorCodeNotPermitted = 3 // The task request could not be submitted because the appropriate background mode is not included in the UIBackgroundModes array, or its identifier was not present in the BGTaskSchedulerPermittedIdentifiers array in the app's Info.plist.
- */
- [[NCCommunicationCommon shared] writeLog:[NSString stringWithFormat:@"Background task failed to submit request: %@", error]];
- } else {
- [[NCCommunicationCommon shared] writeLog:[NSString stringWithFormat:@"Background task success submit request %@", request]];
- }
- }
- }
- -(void)handleBackgroundTask:(BGTask *)task API_AVAILABLE(ios(13.0))
- {
- if (self.account == nil || self.account.length == 0) {
- return;
- }
-
- //do things with task
- [[NCCommunicationCommon shared] writeLog:@"Start handler background task"];
-
- // Verify new photo
- [[NCAutoUpload shared] initAutoUploadWithViewController:nil];
-
- //[task setTaskCompletedWithSuccess:true];
- }
- #pragma mark Fetch
- - (void)application:(UIApplication *)application performFetchWithCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
- {
- if (self.account == nil || self.account.length == 0) {
- completionHandler(UIBackgroundFetchResultNoData);
- return;
- }
-
- [[NCCommunicationCommon shared] writeLog:@"Start perform Fetch"];
-
- // Verify new photo
- [[NCAutoUpload shared] initAutoUploadWithViewController:nil];
-
- // after 20 sec
- /*
- dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 20 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
- [[NCCommunicationCommon shared] writeLog:@"End 20 sec. perform Fetch"];
- completionHandler(UIBackgroundFetchResultNoData);
- });
- */
- }
- #pragma mark Operation Networking & Session
- //
- // Method called by the system when all the background task has end
- //
- - (void)application:(UIApplication *)application handleEventsForBackgroundURLSession:(NSString *)identifier completionHandler:(void (^)(void))completionHandler
- {
- [[NCCommunicationCommon shared] writeLog:[NSString stringWithFormat:@"Start handle Events For Background URLSession: %@", identifier]];
-
- dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 20 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
- self.backgroundSessionCompletionHandler = completionHandler;
- void (^completionHandler)() = self.backgroundSessionCompletionHandler;
- self.backgroundSessionCompletionHandler = nil;
- completionHandler();
- });
- }
- #pragma mark OpenURL
- - (BOOL)application:(UIApplication *)application openURL:(NSURL *)url options:(NSDictionary<NSString *,id> *)options
- {
- if (self.account == nil || self.account.length == 0) { return YES; }
-
- NSString *scheme = url.scheme;
- NSString *fileName;
- NSString *serverUrl;
- if ([scheme isEqualToString:@"nextcloud"]) {
-
- NSString *action = url.host;
- if ([action isEqualToString:@"open-file"]) {
- NSURLComponents *urlComponents = [NSURLComponents componentsWithURL:url resolvingAgainstBaseURL:NO];
- NSArray *queryItems = urlComponents.queryItems;
- NSString *user = [CCUtility valueForKey:@"user" fromQueryItems:queryItems];
- NSString *path = [CCUtility valueForKey:@"path" fromQueryItems:queryItems];
- NSString *link = [CCUtility valueForKey:@"link" fromQueryItems:queryItems];
- tableAccount *matchedAccount = nil;
- // verify parameter
- if (user.length == 0 || path.length == 0 || [[NSURL URLWithString:link] host].length == 0) {
-
- UIAlertController *alertController = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"_error_", nil) message:NSLocalizedString(@"_error_parameter_schema_", nil) preferredStyle:UIAlertControllerStyleAlert];
- UIAlertAction *okAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"_ok_", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {}];
-
- [alertController addAction:okAction];
- [self.window.rootViewController presentViewController:alertController animated:YES completion:nil];
-
- } else {
-
- tableAccount *account = [[NCManageDatabase shared] getAccountActive];
- if (account) {
- NSURL *accountURL = [NSURL URLWithString:account.urlBase];
- NSString *accountUser = account.user;
- if ([link containsString:accountURL.host] && [user isEqualToString:accountUser]) {
- matchedAccount = account;
- } else {
- NSArray *accounts = [[NCManageDatabase shared] getAllAccount];
- for (tableAccount *account in accounts) {
- NSURL *accountURL = [NSURL URLWithString:account.urlBase];
- NSString *accountUser = account.user;
- if ([link containsString:accountURL.host] && [user isEqualToString:accountUser]) {
- matchedAccount = [[NCManageDatabase shared] setAccountActive:account.account];
- [self settingAccount:matchedAccount.account urlBase:matchedAccount.urlBase user:matchedAccount.user userID:matchedAccount.userID password:[CCUtility getPassword:matchedAccount.account]];
- [[NSNotificationCenter defaultCenter] postNotificationOnMainThreadName:NCBrandGlobal.shared.notificationCenterInitializeMain object:nil userInfo:nil];
- }
- }
- }
-
- if (matchedAccount) {
-
- NSString *webDAV = [[NCUtilityFileSystem shared] getWebDAVWithAccount:self.account];
- if ([path containsString:@"/"]) {
- fileName = [path lastPathComponent];
- serverUrl = [NSString stringWithFormat:@"%@/%@/%@", matchedAccount.urlBase, webDAV, [path stringByDeletingLastPathComponent]];
-
- } else {
-
- fileName = path;
- serverUrl = [NSString stringWithFormat:@"%@/%@", matchedAccount.urlBase, webDAV];
- }
-
- [[NCCollectionCommon shared] openFileViewInFolderWithServerUrl:serverUrl fileName:fileName];
-
- } else {
-
- NSString *domain = [[NSURL URLWithString:link] host];
- NSString *fileName = [path lastPathComponent];
- NSString *message = [NSString stringWithFormat:NSLocalizedString(@"_account_not_available_", nil), user, domain, fileName];
-
- UIAlertController *alertController = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"_info_", nil) message:message preferredStyle:UIAlertControllerStyleAlert];
- UIAlertAction *okAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"_ok_", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {}];
-
- [alertController addAction:okAction];
- [self.window.rootViewController presentViewController:alertController animated:YES completion:nil];
- }
- }
- }
- }
- return YES;
- }
-
- return YES;
- }
- #pragma mark Passcode + Delegate
- - (void)passcodeWithAutomaticallyPromptForBiometricValidation:(BOOL)automaticallyPromptForBiometricValidation
- {
- LAContext *laContext = [LAContext new];
- NSError *error;
- BOOL isBiometryAvailable = false;
-
- if ([[CCUtility getPasscode] length] == 0 || [self.account length] == 0 || [CCUtility getNotPasscodeAtStart]) return;
-
- if (self.passcodeViewController == nil) {
-
- self.passcodeViewController = [[TOPasscodeViewController alloc] initWithStyle:TOPasscodeViewStyleTranslucentLight passcodeType:TOPasscodeTypeSixDigits];
- if (@available(iOS 13.0, *)) {
- if ([[UITraitCollection currentTraitCollection] userInterfaceStyle] == UIUserInterfaceStyleDark) {
- self.passcodeViewController.style = TOPasscodeViewStyleTranslucentDark;
- }
- }
- self.passcodeViewController.delegate = self;
- self.passcodeViewController.keypadButtonShowLettering = false;
-
- if (CCUtility.getEnableTouchFaceID && [laContext canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:&error]) {
- if (error == NULL) {
- if (laContext.biometryType == LABiometryTypeFaceID) {
- self.passcodeViewController.biometryType = TOPasscodeBiometryTypeFaceID;
- self.passcodeViewController.allowBiometricValidation = true;
- isBiometryAvailable = true;
- } else if (laContext.biometryType == LABiometryTypeTouchID) {
- self.passcodeViewController.biometryType = TOPasscodeBiometryTypeTouchID;
- self.passcodeViewController.allowBiometricValidation = true;
- isBiometryAvailable = true;
- } else {
- isBiometryAvailable = false;
- NSLog(@"No Biometric support");
- }
- }
- }
-
- [self.window.rootViewController presentViewController:self.passcodeViewController animated:YES completion:^{
- [self enableTouchFaceID:automaticallyPromptForBiometricValidation];
- }];
-
- } else {
-
- dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 0.5 * NSEC_PER_SEC), dispatch_get_main_queue(), ^(void) {
- [self enableTouchFaceID:automaticallyPromptForBiometricValidation];
- });
- }
- }
- - (void)didInputCorrectPasscodeInPasscodeViewController:(TOPasscodeViewController *)passcodeViewController
- {
- [passcodeViewController dismissViewControllerAnimated:YES completion:^{
- self.passcodeViewController = nil;
- }];
- }
- - (BOOL)passcodeViewController:(TOPasscodeViewController *)passcodeViewController isCorrectCode:(NSString *)code
- {
- return [code isEqualToString:[CCUtility getPasscode]];
- }
- - (void)didPerformBiometricValidationRequestInPasscodeViewController:(TOPasscodeViewController *)passcodeViewController
- {
- [[LAContext new] evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics localizedReason:[[NCBrandOptions shared] brand] reply:^(BOOL success, NSError * _Nullable error) {
- if (success) {
- dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 0.5 * NSEC_PER_SEC), dispatch_get_main_queue(), ^(void) {
- [passcodeViewController dismissViewControllerAnimated:YES completion:^{
- self.passcodeViewController = nil;
- }];
- });
- }
- }];
- }
- - (void)enableTouchFaceID:(BOOL)automaticallyPromptForBiometricValidation
- {
- if (CCUtility.getEnableTouchFaceID && automaticallyPromptForBiometricValidation && self.passcodeViewController.view.window) {
- [[LAContext new] evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics localizedReason:[[NCBrandOptions shared] brand] reply:^(BOOL success, NSError * _Nullable error) {
- if (success) {
- dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 0.5 * NSEC_PER_SEC), dispatch_get_main_queue(), ^(void) {
- [self.passcodeViewController dismissViewControllerAnimated:YES completion:^{
- self.passcodeViewController = nil;
- }];
- });
- }
- }];
- }
- }
- @end
|