Search code examples
iosswiftxcodeswift5xcode13

Swift 5 String Manipulation


I am learning iOS development and I try to get Gps information from phone and send to api using Alamofire as below

let parameter = ["appVersion": "1.3.1",
                         "countryCode": "TZ",
                         "deviceName": "chrome",
                         "deviceId": "Web",
                         "deviceOS": "Mac OS",
                         "deviceToken": "token",
                         "latitude":"nothing",
                         "longitude":"nothing",
                         "date":"nothing",
                         "time":"nothing"
                          "speed":"Nothing"
                         ]
        AF.request("https://mtvmax.com/api/login",method: .post,parameters: parameter).response{
            response in
            debugPrint(response)
        }

And getting Gps information from phone as

func locationManager(_ _manager:CLLocationManager, didUpdateLocations Location:[CLLocation]){
        if let location = Location.first{
            //print(location.coordinate)
            Uilabel.text = location.description
            //print(location.description)
            
        }
        
    }

when I print location.description am getting responses <+37.78583400,-122.40641700> +/- 5.00m (speed -1.00 mps / course -1.00) @ 9/15/22, 10:05:21 AM East Africa Time

My question is how can I manipulate this string to get substrings of latitude, longitude, time, date, speed, course and altitude. I apologise for my English am using google translator


Solution

  • you can use CLLocation class to get these values like:

    func locationManager(_ _manager:CLLocationManager, didUpdateLocations Location:[CLLocation]){
            if let location = Location.first{
                let altitude = location.altitude.description
                let latitude = location.coordinate.latitude.description
                let longitude = location.coordinate.longitude.description
                let timeAndDate = location.timestamp
                let speed = location.speed.description
            
            }
            
        }