2016-11-17 6 views
0

ユーザーのロケーションメソッドをLocationUtilsに分割して、それらを使用できるようにして、重複したメソッドを書き続けることはしませんが、私はそれを行うのに苦労しています。私はiOSを初めて使っています。ユーザーの場所を静的に取得し、他のクラスのiOSで使用する方法

class LocationUtils: NSObject, CLLocationManagerDelegate { 
    let locationManager = CLLocationManager() 
    var userCity: String? 

    func enableLocationServices() { 
    self.locationManager.requestAlwaysAuthorization() 

    if CLLocationManager.locationServicesEnabled() { 
     locationManager.delegate = self // Comment 
     locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters 
     locationManager.startUpdatingLocation() 
    } 
    self.locationManager.requestLocation() 
    } 


    // Try ro call this statically 
    func getUsersClosestCity(lat: CLLocationDegrees, lng: CLLocationDegrees){ 
    let geoCoder = CLGeocoder() 
    let location = CLLocation(latitude: lat, longitude: lng) 
    geoCoder.reverseGeocodeLocation(location) { 
     (placemarks, error) -> Void in 

     let placeArray = placemarks as [CLPlacemark]! 
     var placeMark: CLPlacemark! 
     // Can the array throw an exception 
     placeMark = placeArray?[0] 
     if (placeMark != nil) { 
     //userCity = placeMark.addressDictionary?["City"] as? String? 
     self.userCity = placeMark.addressDictionary?["City"] as? String 

     } 

    } 

    } 
    func getUserCity() -> String? { 
    return self.userCity 
    } 

    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { 
    let locValue:CLLocationCoordinate2D = manager.location!.coordinate 
    self.getUsersClosestCity(lat: locValue.latitude, lng: locValue.longitude) 
    } 

} 

今、私は私が場所を削除すると、これは、ビューにUILabelを追加しません

override func loadView() { 
    super.loadView() 
    setLocationView() 

    } 





func setLocationView() { 
    let locationLabel : UILabel = UILabel(frame: CGRect(x: UIScreen.main.bounds.width/3, y: UIScreen.main.bounds.height, width: 500, height: 21)) 
    let locationLabelFrame = CGRect(x : UIScreen.main.bounds.width/4, y: 3 * (UIScreen.main.bounds.width/4), width : 125, height : 45) 
    locationLabel.frame = locationLabelFrame 
    locationLabel.backgroundColor = UIColor.blue 
    let locUtil = LocationUtils() 
    locUtil.enableLocationServices() 
    if (locUtil.getUserCity() != nil) { 
     locationLabel.text = locUtil.getUserCity() 
    } 

    self.view.addSubview(locationLabel) 
    } 

それを使用する方法のプロジェクト

における様々なクラスでこれらのメソッドを使用したいです表示にラベルが付いています

私が学ぶことができる人や私がこの方法を教えることができる人がいれば、私は感謝します

あなたは彼があなたのviewControllersのすべてで使用することができシングルトンロケーションサービスにすることができ、あなたに

答えて

0

に感謝:あなたはLocationReportingService.shared.beginMonitoring()を呼び出し、それを使用するには

class LocationReportingService { 
    static let shared = LocationReportingService() 
    fileprivate let locationManager = CLLocationManager() 
    private override init() { 
     super.init() 
    } 
    func beginMonitoring() { 
     //insert logic here 
    } 
} 

を。 las know locationのプロパティを追加したり、他のView ControllerがリッスンできるNotificationCenter.default.postをブロードキャストすることもできます。

関連する問題