Forráskód Böngészése

Something is maybe working

Thomas Chef 3 éve
szülő
commit
b727f868ab

+ 4 - 0
JourneyGPSTracker.xcodeproj/project.pbxproj

@@ -7,6 +7,7 @@
 	objects = {
 
 /* Begin PBXBuildFile section */
+		F5A69F9F280C0165001B0EBF /* SendPOSTData.swift in Sources */ = {isa = PBXBuildFile; fileRef = F5A69F9E280C0165001B0EBF /* SendPOSTData.swift */; };
 		F5F388C1280B231400087E94 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = F5F388C0280B231400087E94 /* AppDelegate.swift */; };
 		F5F388C3280B231400087E94 /* SceneDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = F5F388C2280B231400087E94 /* SceneDelegate.swift */; };
 		F5F388C5280B231400087E94 /* ViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = F5F388C4280B231400087E94 /* ViewController.swift */; };
@@ -16,6 +17,7 @@
 /* End PBXBuildFile section */
 
 /* Begin PBXFileReference section */
+		F5A69F9E280C0165001B0EBF /* SendPOSTData.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SendPOSTData.swift; sourceTree = "<group>"; };
 		F5F388BD280B231400087E94 /* JourneyGPSTracker.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = JourneyGPSTracker.app; sourceTree = BUILT_PRODUCTS_DIR; };
 		F5F388C0280B231400087E94 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
 		F5F388C2280B231400087E94 /* SceneDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SceneDelegate.swift; sourceTree = "<group>"; };
@@ -63,6 +65,7 @@
 				F5F388C9280B231500087E94 /* Assets.xcassets */,
 				F5F388CB280B231500087E94 /* LaunchScreen.storyboard */,
 				F5F388CE280B231500087E94 /* Info.plist */,
+				F5A69F9E280C0165001B0EBF /* SendPOSTData.swift */,
 			);
 			path = JourneyGPSTracker;
 			sourceTree = "<group>";
@@ -141,6 +144,7 @@
 				F5F388C5280B231400087E94 /* ViewController.swift in Sources */,
 				F5F388C1280B231400087E94 /* AppDelegate.swift in Sources */,
 				F5F388C3280B231400087E94 /* SceneDelegate.swift in Sources */,
+				F5A69F9F280C0165001B0EBF /* SendPOSTData.swift in Sources */,
 			);
 			runOnlyForDeploymentPostprocessing = 0;
 		};

+ 4 - 1
JourneyGPSTracker/Base.lproj/Main.storyboard

@@ -31,9 +31,12 @@
                                     <action selector="jhfgjf:" destination="BYZ-38-t0r" eventType="editingChanged" id="607-lr-uNy"/>
                                 </connections>
                             </button>
-                            <switch opaque="NO" contentMode="scaleToFill" horizontalHuggingPriority="750" verticalHuggingPriority="750" fixedFrame="YES" contentHorizontalAlignment="center" contentVerticalAlignment="center" on="YES" translatesAutoresizingMaskIntoConstraints="NO" id="vwo-8o-W4T">
+                            <switch opaque="NO" contentMode="scaleToFill" horizontalHuggingPriority="750" verticalHuggingPriority="750" fixedFrame="YES" contentHorizontalAlignment="center" contentVerticalAlignment="center" translatesAutoresizingMaskIntoConstraints="NO" id="vwo-8o-W4T">
                                 <rect key="frame" x="227" y="84" width="49" height="31"/>
                                 <autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
+                                <connections>
+                                    <action selector="trackingEnabledChanged:" destination="BYZ-38-t0r" eventType="valueChanged" id="tMj-hT-aGR"/>
+                                </connections>
                             </switch>
                         </subviews>
                         <color key="backgroundColor" systemColor="systemBackgroundColor" cocoaTouchSystemColor="whiteColor"/>

+ 37 - 0
JourneyGPSTracker/SendPOSTData.swift

@@ -0,0 +1,37 @@
+//
+//  SendPOSTData.swift
+//  JourneyGPSTracker
+//
+//  Created by Thomas Chef on 2022-04-17.
+//  Copyright © 2022 Thomas Chef. All rights reserved.
+//
+
+import Foundation
+import CoreLocation
+
+//class SendPOSTData {
+    
+    func placeOrder(didUpdateLocations locations: [CLLocation]) {
+        print("placeOrder")
+        locations.forEach { (location) in
+            var kalle = "Hejhejhej"
+            guard let encoded = try? JSONEncoder().encode(kalle) else {
+                print("Failed to encode order")
+                return
+            }
+            
+            let url = URL(string: "http://chef.maya.se/gpsapi/cupcakes")!
+            var request = URLRequest(url: url)
+            request.setValue("application/json", forHTTPHeaderField: "Content-Type")
+            request.httpMethod = "POST"
+            
+            /*do {
+                let (data, _) = try await URLSession.shared.upload(for: request, from: encoded)
+                // handle the result
+            } catch {
+                print("Checkout failed.")
+            }*/
+        }
+    }
+
+//}

+ 29 - 5
JourneyGPSTracker/ViewController.swift

@@ -20,11 +20,27 @@ class ViewController: UIViewController {
         locationManager = CLLocationManager()
         locationManager?.delegate = self
         locationManager?.requestAlwaysAuthorization()
+        locationManager?.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
+        locationManager?.activityType = CLActivityType.otherNavigation
+        locationManager?.distanceFilter = 100.0
     }
     
     @IBAction func buttonClicked(_ sender: Any) {
         print("Clicked !")
-        locationManager?.requestLocation()
+        //locationManager?.requestLocation()
+        
+    }
+    
+    @IBAction func trackingEnabledChanged(_ sender: UISwitch) {
+        print("Switch Changed !")
+        switch sender.isOn {
+        case true:
+            print("ON")
+            locationManager?.startUpdatingLocation()
+        default:
+            print("OFF")
+            locationManager?.stopUpdatingLocation()
+        }
     }
     
     @IBOutlet weak var clickButton: UIButton!
@@ -55,15 +71,23 @@ extension ViewController: CLLocationManagerDelegate {
       print("LocationManager didUpdateLocations: numberOfLocation: \(locations.count)")
       let dateFormatter = DateFormatter()
       dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
+        
+        placeOrder(didUpdateLocations: locations)
       
       locations.forEach { (location) in
         print("LocationManager didUpdateLocations: \(dateFormatter.string(from: location.timestamp)); \(location.coordinate.latitude), \(location.coordinate.longitude)")
-        print("LocationManager altitude: \(location.altitude)")
         print("LocationManager horizontalAccuracy: \(location.horizontalAccuracy)")
         print("LocationManager verticalAccuracy: \(location.verticalAccuracy)")
-        print("LocationManager speed: \(location.speed)")
-        print("LocationManager timestamp: \(location.timestamp)")
-        print("LocationManager course: \(location.course)")
+      }
+    }
+    
+    func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
+      print("LocationManager didFailWithError \(error.localizedDescription)")
+      if let error = error as? CLError, error.code == .denied {
+         // Location updates are not authorized.
+        // To prevent forever looping of `didFailWithError` callback
+         locationManager?.stopMonitoringSignificantLocationChanges()
+         return
       }
     }
 }