marinofaggiana 4 лет назад
Родитель
Сommit
f967abbb5d

+ 20 - 11
iOSClient/Networking/NCAutoUpload.swift

@@ -38,19 +38,20 @@ class NCAutoUpload: NSObject, CLLocationManagerDelegate {
 
     // MARK: -
     
-    func startSignificantChangeUpdates() {
+    @objc func startSignificantChangeUpdates() {
         
         if locationManager == nil {
             
             locationManager = CLLocationManager.init()
             locationManager?.delegate = self
+            locationManager?.distanceFilter = 100
             locationManager?.requestAlwaysAuthorization()
         }
         
         locationManager?.startMonitoringSignificantLocationChanges()
     }
     
-   @objc func stopSignificantChangeUpdates() {
+    @objc func stopSignificantChangeUpdates() {
         
         locationManager?.stopMonitoringSignificantLocationChanges()
     }
@@ -58,10 +59,10 @@ class NCAutoUpload: NSObject, CLLocationManagerDelegate {
     func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
         
         let location = locations.last
-        let latitude = String(describing: location?.coordinate.latitude)
-        let longitude = String(describing: location?.coordinate.longitude)
+        guard let latitude = location?.coordinate.latitude else { return }
+        guard let longitude = location?.coordinate.longitude else { return }
         
-        NCCommunicationCommon.shared.writeLog("update location manager: latitude " + latitude + ", longitude " + longitude)
+        NCCommunicationCommon.shared.writeLog("update location manager: latitude \(latitude) longitude \(longitude)")
         
         if let account = NCManageDatabase.shared.getAccountActive() {
             if account.autoUpload && account.autoUploadBackground && UIApplication.shared.applicationState == UIApplication.State.background {
@@ -75,13 +76,21 @@ class NCAutoUpload: NSObject, CLLocationManagerDelegate {
     }
     
     func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
-        NCManageDatabase.shared.setAccountAutoUploadProperty("autoUploadBackground", state: false)
-        self.stopSignificantChangeUpdates()
+        NCAskAuthorization.shared.askAuthorizationLocationManager() { (hasFullPermissions) in
+            if !hasFullPermissions {
+                NCManageDatabase.shared.setAccountAutoUploadProperty("autoUploadBackground", state: false)
+                self.stopSignificantChangeUpdates()
+            }
+        }
     }
     
     func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
-        NCManageDatabase.shared.setAccountAutoUploadProperty("autoUploadBackground", state: false)
-        self.stopSignificantChangeUpdates()
+        NCAskAuthorization.shared.askAuthorizationLocationManager() { (hasFullPermissions) in
+            if !hasFullPermissions {
+                NCManageDatabase.shared.setAccountAutoUploadProperty("autoUploadBackground", state: false)
+                self.stopSignificantChangeUpdates()
+            }
+        }
     }
     
     // MARK: -
@@ -93,8 +102,8 @@ class NCAutoUpload: NSObject, CLLocationManagerDelegate {
                     if hasPermission {
                         self.uploadAssetsNewAndFull(viewController:viewController, selector: NCBrandGlobal.shared.selectorUploadAutoUpload)
                         if account.autoUploadBackground {
-                            NCAskAuthorization.shared.askAuthorizationLocationManager(viewController: viewController) { (hasPermissions) in
-                                if hasPermissions {
+                            NCAskAuthorization.shared.askAuthorizationLocationManager() { (hasFullPermissions) in
+                                if hasFullPermissions {
                                     self.startSignificantChangeUpdates()
                                 } else {
                                     NCManageDatabase.shared.setAccountAutoUploadProperty("autoUploadBackground", state: false)

+ 3 - 2
iOSClient/Settings/CCManageAutoUpload.m

@@ -282,9 +282,9 @@
         
         if ([[rowDescriptor.value valueData] boolValue] == YES) {
                         
-            [[NCAskAuthorization shared] askAuthorizationLocationManagerWithViewController:self completion:^(BOOL hasPermission) {
+            [[NCAskAuthorization shared]  askAuthorizationLocationManagerWithCompletion: ^(BOOL hasFullPermissions) {
                 
-                if (hasPermission == YES) {
+                if (hasFullPermissions == YES) {
                     
                     UIAlertController *alertController = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"_autoupload_background_title_", nil) message:NSLocalizedString(@"_autoupload_background_msg_", nil) preferredStyle:UIAlertControllerStyleAlert];
                     UIAlertAction *okAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"_ok_", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {}];
@@ -293,6 +293,7 @@
                     [self presentViewController:alertController animated:YES completion:nil];
                     
                     [[NCManageDatabase shared] setAccountAutoUploadProperty:@"autoUploadBackground" state:YES];
+                    [[NCAutoUpload shared] startSignificantChangeUpdates];
                         
                 } else {
                      

+ 2 - 12
iOSClient/Utility/NCAskAuthorization.swift

@@ -101,24 +101,14 @@ class NCAskAuthorization: NSObject {
         }
     }
     
-    @objc func askAuthorizationLocationManager(viewController: UIViewController?, completion: @escaping (_ hasPermission: Bool)->()) {
+    @objc func askAuthorizationLocationManager(completion: @escaping (_ hasFullPermissions: Bool)->()) {
         
         switch CLLocationManager.authorizationStatus() {
         case CLAuthorizationStatus.authorizedAlways:
             completion(true)
             break
         case CLAuthorizationStatus.authorizedWhenInUse, CLAuthorizationStatus.denied, CLAuthorizationStatus.restricted:
-            let alert = UIAlertController(title: NSLocalizedString("_error_", comment: ""), message: NSLocalizedString("_err_permission_locationmanager_", comment: ""), preferredStyle: .alert)
-            alert.addAction(UIAlertAction(title: NSLocalizedString("_open_settings_", comment: ""), style: .default, handler: { action in
-                UIApplication.shared.open(URL(string: UIApplication.openSettingsURLString)!)
-                completion(false)
-            }))
-            alert.addAction(UIAlertAction(title: NSLocalizedString("_cancel_", comment: ""), style: .cancel, handler: { action in
-                completion(false)
-            }))
-            DispatchQueue.main.async {
-                viewController?.present(alert, animated: true, completion: nil)
-            }
+            completion(false)
             break
         case CLAuthorizationStatus.notDetermined:
             DispatchQueue.main.async {