forked from nextcloud/ios
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAppDelegate.swift
448 lines (374 loc) · 21.9 KB
/
AppDelegate.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
//
// AppDelegate.swift
// Nextcloud
//
// Created by Marino Faggiana on 04/09/14 (19/02/21 swift).
// Copyright (c) 2014 Marino Faggiana. All rights reserved.
//
// Author Marino Faggiana <[email protected]>
//
// 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 UIKit
import BackgroundTasks
import NextcloudKit
import LocalAuthentication
import Firebase
import WidgetKit
import Queuer
import EasyTipView
import SwiftUI
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate {
var tipView: EasyTipView?
var backgroundSessionCompletionHandler: (() -> Void)?
var activeLogin: NCLogin?
var activeLoginWeb: NCLoginProvider?
var taskAutoUploadDate: Date = Date()
var isUiTestingEnabled: Bool {
return ProcessInfo.processInfo.arguments.contains("UI_TESTING")
}
var notificationSettings: UNNotificationSettings?
var loginFlowV2Token = ""
var loginFlowV2Endpoint = ""
var loginFlowV2Login = ""
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
if isUiTestingEnabled {
NCAccount().deleteAllAccounts()
}
let utilityFileSystem = NCUtilityFileSystem()
let utility = NCUtility()
var levelLog = 0
let versionNextcloudiOS = String(format: NCBrandOptions.shared.textCopyrightNextcloudiOS, utility.getVersionApp())
NCSettingsBundleHelper.checkAndExecuteSettings(delay: 0)
UserDefaults.standard.register(defaults: ["UserAgent": userAgent])
if !NCKeychain().disableCrashservice, !NCBrandOptions.shared.disable_crash_service {
FirebaseApp.configure()
}
utilityFileSystem.createDirectoryStandard()
utilityFileSystem.emptyTemporaryDirectory()
utilityFileSystem.clearCacheDirectory("com.limit-point.LivePhoto")
// Create users colors
NCBrandColor.shared.createUserColors()
NextcloudKit.shared.setup(delegate: NCNetworking.shared)
NextcloudKit.shared.nkCommonInstance.pathLog = utilityFileSystem.directoryGroup
if NCBrandOptions.shared.disable_log {
utilityFileSystem.removeFile(atPath: NextcloudKit.shared.nkCommonInstance.filenamePathLog)
utilityFileSystem.removeFile(atPath: NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first! + "/" + NextcloudKit.shared.nkCommonInstance.filenameLog)
} else {
levelLog = NCKeychain().logLevel
NextcloudKit.shared.nkCommonInstance.levelLog = levelLog
NextcloudKit.shared.nkCommonInstance.copyLogToDocumentDirectory = true
NextcloudKit.shared.nkCommonInstance.writeLog("[INFO] Start session with level \(levelLog) " + versionNextcloudiOS)
}
/// Push Notification & display notification
UNUserNotificationCenter.current().getNotificationSettings { settings in
self.notificationSettings = settings
}
application.registerForRemoteNotifications()
UNUserNotificationCenter.current().delegate = self
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .badge, .sound]) { _, _ in }
if !utility.isSimulatorOrTestFlight() {
let review = NCStoreReview()
review.incrementAppRuns()
review.showStoreReview()
}
/// Background task register
BGTaskScheduler.shared.register(forTaskWithIdentifier: NCGlobal.shared.refreshTask, using: nil) { task in
self.handleAppRefresh(task)
}
BGTaskScheduler.shared.register(forTaskWithIdentifier: NCGlobal.shared.processingTask, using: nil) { task in
self.handleProcessingTask(task)
}
if NCBrandOptions.shared.enforce_passcode_lock {
NCKeychain().requestPasscodeAtStart = true
}
/// Activation singleton
_ = NCActionCenter.shared
_ = NCNetworkingProcess.shared
return true
}
func applicationWillTerminate(_ application: UIApplication) {
if self.notificationSettings?.authorizationStatus != .denied && UIApplication.shared.backgroundRefreshStatus == .available {
let content = UNMutableNotificationContent()
content.title = NCBrandOptions.shared.brand
content.body = NSLocalizedString("_keep_running_", comment: "")
let req = UNNotificationRequest(identifier: UUID().uuidString, content: content, trigger: nil)
let notificationCenter = UNUserNotificationCenter.current()
notificationCenter.add(req)
}
NextcloudKit.shared.nkCommonInstance.writeLog("[INFO] bye bye")
}
// MARK: - UISceneSession Lifecycle
func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {
// Called when a new scene session is being created.
// Use this method to select a configuration to create the new scene with.
return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role)
}
func application(_ application: UIApplication, didDiscardSceneSessions sceneSessions: Set<UISceneSession>) {
// Called when the user discards a scene session.
// If any sessions were discarded while the application was not running, this will be called shortly after application:didFinishLaunchingWithOptions.
// Use this method to release any resources that were specific to the discarded scenes, as they will not return.
}
// MARK: - Background Task
/*
@discussion Schedule a refresh task request to ask that the system launch your app briefly so that you can download data and keep your app's contents up-to-date. The system will fulfill this request intelligently based on system conditions and app usage.
*/
func scheduleAppRefresh() {
let request = BGAppRefreshTaskRequest(identifier: NCGlobal.shared.refreshTask)
request.earliestBeginDate = Date(timeIntervalSinceNow: 60) // Refresh after 60 seconds.
do {
try BGTaskScheduler.shared.submit(request)
} catch {
NextcloudKit.shared.nkCommonInstance.writeLog("[ERROR] Refresh task failed to submit request: \(error)")
}
}
/*
@discussion Schedule a processing task request to ask that the system launch your app when conditions are favorable for battery life to handle deferrable, longer-running processing, such as syncing, database maintenance, or similar tasks. The system will attempt to fulfill this request to the best of its ability within the next two days as long as the user has used your app within the past week.
*/
func scheduleAppProcessing() {
let request = BGProcessingTaskRequest(identifier: NCGlobal.shared.processingTask)
request.earliestBeginDate = Date(timeIntervalSinceNow: 5 * 60) // Refresh after 5 minutes.
request.requiresNetworkConnectivity = false
request.requiresExternalPower = false
do {
try BGTaskScheduler.shared.submit(request)
} catch {
NextcloudKit.shared.nkCommonInstance.writeLog("[ERROR] Background Processing task failed to submit request: \(error)")
}
}
func handleAppRefresh(_ task: BGTask) {
scheduleAppRefresh()
handleAppRefreshProcessingTask(taskText: "AppRefresh") {
task.setTaskCompleted(success: true)
}
}
func handleProcessingTask(_ task: BGTask) {
scheduleAppProcessing()
handleAppRefreshProcessingTask(taskText: "ProcessingTask") {
task.setTaskCompleted(success: true)
}
}
func handleAppRefreshProcessingTask(taskText: String, completion: @escaping () -> Void = {}) {
Task {
var numAutoUpload = 0
guard let account = NCManageDatabase.shared.getActiveTableAccount()?.account else {
return
}
NextcloudKit.shared.nkCommonInstance.writeLog("[DEBUG] \(taskText) start handle")
// Test every > 1 min
if Date() > self.taskAutoUploadDate.addingTimeInterval(60) {
self.taskAutoUploadDate = Date()
numAutoUpload = await NCAutoUpload.shared.initAutoUpload(account: account)
NextcloudKit.shared.nkCommonInstance.writeLog("[DEBUG] \(taskText) auto upload with \(numAutoUpload) uploads")
} else {
NextcloudKit.shared.nkCommonInstance.writeLog("[DEBUG] \(taskText) disabled auto upload")
}
let results = await NCNetworkingProcess.shared.refreshProcessingTask()
NextcloudKit.shared.nkCommonInstance.writeLog("[DEBUG] \(taskText) networking process with download: \(results.counterDownloading) upload: \(results.counterUploading)")
if taskText == "ProcessingTask",
numAutoUpload == 0,
results.counterDownloading == 0,
results.counterUploading == 0,
let directories = NCManageDatabase.shared.getTablesDirectory(predicate: NSPredicate(format: "account == %@ AND offline == true", account), sorted: "offlineDate", ascending: true) {
for directory: tableDirectory in directories {
// test only 3 time for day (every 8 h.)
if let offlineDate = directory.offlineDate, offlineDate.addingTimeInterval(28800) > Date() {
NextcloudKit.shared.nkCommonInstance.writeLog("[DEBUG] \(taskText) skip synchronization for \(directory.serverUrl) in date \(offlineDate)")
continue
}
let results = await NCNetworking.shared.synchronization(account: account, serverUrl: directory.serverUrl, add: false)
NextcloudKit.shared.nkCommonInstance.writeLog("[DEBUG] \(taskText) end synchronization for \(directory.serverUrl), errorCode: \(results.errorCode), item: \(results.num)")
}
}
let counter = NCManageDatabase.shared.getResultsMetadatas(predicate: NSPredicate(format: "account == %@ AND (session == %@ || session == %@) AND status != %d",
account,
NCNetworking.shared.sessionDownloadBackground,
NCNetworking.shared.sessionUploadBackground,
NCGlobal.shared.metadataStatusNormal))?.count ?? 0
UIApplication.shared.applicationIconBadgeNumber = counter
NextcloudKit.shared.nkCommonInstance.writeLog("[DEBUG] \(taskText) completion handle")
completion()
}
}
// MARK: - Background Networking Session
func application(_ application: UIApplication, handleEventsForBackgroundURLSession identifier: String, completionHandler: @escaping () -> Void) {
NextcloudKit.shared.nkCommonInstance.writeLog("[DEBUG] Start handle Events For Background URLSession: \(identifier)")
WidgetCenter.shared.reloadAllTimelines()
backgroundSessionCompletionHandler = completionHandler
}
// MARK: - Push Notifications
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
completionHandler([.list, .banner, .sound])
}
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
if let pref = UserDefaults(suiteName: NCBrandOptions.shared.capabilitiesGroup),
let data = pref.object(forKey: "NOTIFICATION_DATA") as? [String: AnyObject] {
nextcloudPushNotificationAction(data: data)
pref.set(nil, forKey: "NOTIFICATION_DATA")
}
completionHandler()
}
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
NCNetworking.shared.checkPushNotificationServerProxyCertificateUntrusted(viewController: UIApplication.shared.firstWindow?.rootViewController) { error in
if error == .success {
NCPushNotification.shared.registerForRemoteNotificationsWithDeviceToken(deviceToken)
}
}
}
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
NCPushNotification.shared.applicationdidReceiveRemoteNotification(userInfo: userInfo) { result in
completionHandler(result)
}
}
func nextcloudPushNotificationAction(data: [String: AnyObject]) {
guard let data = NCApplicationHandle().nextcloudPushNotificationAction(data: data)
else {
return
}
let account = data["account"] as? String ?? "unavailable"
let app = data["app"] as? String
func openNotification(controller: NCMainTabBarController) {
if app == NCGlobal.shared.termsOfServiceName {
NotificationCenter.default.postOnMainThread(name: NCGlobal.shared.notificationCenterGetServerData, second: 0.5)
} else if let viewController = UIStoryboard(name: "NCNotification", bundle: nil).instantiateInitialViewController() as? NCNotification {
viewController.session = NCSession.shared.getSession(account: account)
DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
let navigationController = UINavigationController(rootViewController: viewController)
navigationController.modalPresentationStyle = .fullScreen
controller.present(navigationController, animated: true)
}
}
}
if let controller = SceneManager.shared.getControllers().first(where: { $0.account == account }) {
openNotification(controller: controller)
} else if let tableAccount = NCManageDatabase.shared.getAllTableAccount().first(where: { $0.account == account }),
let controller = UIApplication.shared.firstWindow?.rootViewController as? NCMainTabBarController {
NCAccount().changeAccount(tableAccount.account, userProfile: nil, controller: controller) {
openNotification(controller: controller)
}
} else {
let message = NSLocalizedString("_the_account_", comment: "") + " " + account + " " + NSLocalizedString("_does_not_exist_", comment: "")
let alertController = UIAlertController(title: NSLocalizedString("_info_", comment: ""), message: message, preferredStyle: .alert)
alertController.addAction(UIAlertAction(title: NSLocalizedString("_ok_", comment: ""), style: .default, handler: { _ in }))
UIApplication.shared.firstWindow?.rootViewController?.present(alertController, animated: true, completion: { })
}
}
// MARK: - Login
func openLogin(selector: Int, window: UIWindow? = nil) {
UIApplication.shared.allSceneSessionDestructionExceptFirst()
func showLoginViewController(_ viewController: UIViewController?) {
guard let viewController else { return }
let navigationController = NCLoginNavigationController(rootViewController: viewController)
navigationController.modalPresentationStyle = .fullScreen
navigationController.navigationBar.barStyle = .black
navigationController.navigationBar.tintColor = NCBrandColor.shared.customerText
navigationController.navigationBar.barTintColor = NCBrandColor.shared.customer
navigationController.navigationBar.isTranslucent = false
if let controller = UIApplication.shared.firstWindow?.rootViewController {
if let presentedVC = controller.presentedViewController, !(presentedVC is NCLoginNavigationController) {
presentedVC.dismiss(animated: false) {
controller.present(navigationController, animated: true)
}
} else {
controller.present(navigationController, animated: true)
}
} else {
window?.rootViewController = navigationController
window?.makeKeyAndVisible()
}
}
// Nextcloud standard login
if selector == NCGlobal.shared.introSignup {
if activeLogin?.view.window == nil {
if selector == NCGlobal.shared.introSignup {
let web = UIStoryboard(name: "NCLogin", bundle: nil).instantiateViewController(withIdentifier: "NCLoginProvider") as? NCLoginProvider
web?.urlBase = NCBrandOptions.shared.linkloginPreferredProviders
showLoginViewController(web)
} else {
activeLogin = UIStoryboard(name: "NCLogin", bundle: nil).instantiateViewController(withIdentifier: "NCLogin") as? NCLogin
if let controller = UIApplication.shared.firstWindow?.rootViewController as? NCMainTabBarController, !controller.account.isEmpty {
let session = NCSession.shared.getSession(account: controller.account)
activeLogin?.urlBase = session.urlBase
}
showLoginViewController(activeLogin)
}
}
} else {
if activeLogin?.view.window == nil {
activeLogin = UIStoryboard(name: "NCLogin", bundle: nil).instantiateViewController(withIdentifier: "NCLogin") as? NCLogin
activeLogin?.urlBase = NCBrandOptions.shared.disable_request_login_url ? NCBrandOptions.shared.loginBaseUrl : ""
showLoginViewController(activeLogin)
}
}
}
// MARK: -
func trustCertificateError(host: String) {
guard let activeTableAccount = NCManageDatabase.shared.getActiveTableAccount(),
let currentHost = URL(string: activeTableAccount.urlBase)?.host,
let pushNotificationServerProxyHost = URL(string: NCBrandOptions.shared.pushNotificationServerProxy)?.host,
host != pushNotificationServerProxyHost,
host == currentHost
else { return }
let certificateHostSavedPath = NCUtilityFileSystem().directoryCertificates + "/" + host + ".der"
var title = NSLocalizedString("_ssl_certificate_changed_", comment: "")
if !FileManager.default.fileExists(atPath: certificateHostSavedPath) {
title = NSLocalizedString("_connect_server_anyway_", comment: "")
}
let alertController = UIAlertController(title: title, message: NSLocalizedString("_server_is_trusted_", comment: ""), preferredStyle: .alert)
alertController.addAction(UIAlertAction(title: NSLocalizedString("_yes_", comment: ""), style: .default, handler: { _ in
NCNetworking.shared.writeCertificate(host: host)
}))
alertController.addAction(UIAlertAction(title: NSLocalizedString("_no_", comment: ""), style: .default, handler: { _ in }))
alertController.addAction(UIAlertAction(title: NSLocalizedString("_certificate_details_", comment: ""), style: .default, handler: { _ in
if let navigationController = UIStoryboard(name: "NCViewCertificateDetails", bundle: nil).instantiateInitialViewController() as? UINavigationController,
let viewController = navigationController.topViewController as? NCViewCertificateDetails {
viewController.delegate = self
viewController.host = host
UIApplication.shared.firstWindow?.rootViewController?.present(navigationController, animated: true)
}
}))
UIApplication.shared.firstWindow?.rootViewController?.present(alertController, animated: true)
}
// MARK: - Reset Application
func resetApplication() {
let utilityFileSystem = NCUtilityFileSystem()
NCNetworking.shared.cancelAllTask()
URLCache.shared.removeAllCachedResponses()
utilityFileSystem.removeGroupDirectoryProviderStorage()
utilityFileSystem.removeGroupApplicationSupport()
utilityFileSystem.removeDocumentsDirectory()
utilityFileSystem.removeTemporaryDirectory()
NCKeychain().removeAll()
NCNetworking.shared.removeAllKeyUserDefaultsData(account: nil)
exit(0)
}
// MARK: - Universal Links
func application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: @escaping ([UIUserActivityRestoring]?) -> Void) -> Bool {
let applicationHandle = NCApplicationHandle()
return applicationHandle.applicationOpenUserActivity(userActivity)
}
}
// MARK: - Extension
extension AppDelegate: NCViewCertificateDetailsDelegate {
func viewCertificateDetailsDismiss(host: String) {
trustCertificateError(host: host)
}
}
extension AppDelegate: NCCreateFormUploadConflictDelegate {
func dismissCreateFormUploadConflict(metadatas: [tableMetadata]?) {
guard let metadatas = metadatas, !metadatas.isEmpty else { return }
NCNetworkingProcess.shared.createProcessUploads(metadatas: metadatas)
}
}