Wie erhalte ich Lokalergebnisse von einer Klasse (GPSLocation) von (userLocation.swift) und verwende sie in einer anderen Klasse in der Datei (target.swift) ..?Erhalte Lokationsergebnisse von einer anderen Klasse in swift
Ich brauche Ländercode, Stadt, Länge und Breite von:
userLocation.swift
import UIKit
import MapKit
class GPSLocation {
func getGPSLocation(completion:() -> Void) {
let locManager = CLLocationManager()
var currentLocation: CLLocation!
locManager.desiredAccuracy = kCLLocationAccuracyBest
if (CLLocationManager.authorizationStatus() == CLAuthorizationStatus.authorizedWhenInUse || CLLocationManager.authorizationStatus() == CLAuthorizationStatus.authorizedAlways) {
currentLocation = locManager.location
let latitude = String(format: "%.7f", currentLocation.coordinate.latitude)
let longitude = String(format: "%.7f", currentLocation.coordinate.longitude)
let location = CLLocation(latitude: currentLocation.coordinate.latitude, longitude: currentLocation.coordinate.longitude)
fetchCountryAndCity(location: location) { countryCode, city in
// debugPrint("Country:", countryCode)
// debugPrint("City:", city)
}
// debugPrint("Latitude:", latitude)
// debugPrint("Longitude:", longitude)
}
}
//MARK: Find countryCode & City name from longitude & latitude
func fetchCountryAndCity(location: CLLocation, completion: @escaping (String, String) ->()) {
CLGeocoder().reverseGeocodeLocation(location) { placemarks, error in
if let error = error {
debugPrint(error)
} else if let countryCode = placemarks?.first?.isoCountryCode,
let city = placemarks?.first?.locality {
completion(countryCode, city)
}
}
}
}
Und sie in der Datei drucken:
target.swift
import Foundation
class Post {
fileprivate func Post() {
func getLocation() {
// Get user GPS location (longitude, latitude, Country Code, City)
let getUserLocation = GPSLocation()
getUserLocation.getGPSLocation {
debugPrint("Country:", countryCode)
debugPrint("City:", city)
debugPrint("Latitude:", latitude)
debugPrint("Longitude:", longitude)
}
}
}
}
Vielen Dank im Voraus .. !!!
Erstellen Sie eine SharedInstance/Singleton? – Larme
Wenn Sie 'getGPSLocation' aufrufen, drucken Sie nicht schon das Zeug, das Sie wollen? – Sweeper
Übergeben Sie eine Closure an 'getGPSLocation' und rufen Sie sie aus dem' fetchCountryAndCity'-Beendigungshandler auf. – Paulw11