2016-05-12 3 views
0

auf Ich arbeite mit swift 2.2 und habe eine App erstellt, die den Standort an einen mdm-Server sendet. Ich habe die Klasse als eine Unterklasse von NSObject deklariert und das CLLOcationManagerDelegate enthalten. Aber die Methoden wie didUpdateLocations() und didUpdateToLocation() werden nicht aufgerufen. Ich verwende keine der Schaltflächen oder irgendetwas in der Ansicht, die den Standort sendet. Ich möchte, dass die App den Standort an den Server sendet, wenn die App den Standort aktualisiert. Hier ist mein Code.So rufen Sie die CLLocation Manager-Delegatenmethoden in iOS7 mit swift

class LocationProcessHandler: NSObject , CLLocationManagerDelegate{ 

    var location = CLLocationManager() 
    func startLocationUpdates() { 



     NSLog("It entered the start location updates method of the location process handler") 
    if (!(CLLocationManager.locationServicesEnabled()) || (CLLocationManager.authorizationStatus() != CLAuthorizationStatus.Denied)) { 

     NSLog("It Entered to create the managed app feedback ") 
     let persist = Persistence() 

        let json: [NSObject : AnyObject] = [ 
      "IsLocationSettingsEnabled" : "\(0)",mdmiosagent_Constants.MESSAGETYPEKEY : mdmiosagent_Constants.LOCATIONMSGTYPEKEY,mdmiosagent_Constants.UDIDKEY : persist.getObject(mdmiosagent_Constants.UDIDKEY),"TimeStamp" : "\(self.toLocalTime())" 
     ] 
     let userDefaults : NSUserDefaults = NSUserDefaults.standardUserDefaults() 
     userDefaults.setObject(json, forKey: mdmiosagent_Constants.MANAGED_APP_FEEDBACK) 
     userDefaults.synchronize() 
     NSLog("The dict to be sent as managed app feedback is \(json)") 
     do { 
     let jsonData : NSData = try NSJSONSerialization.dataWithJSONObject(json, options: NSJSONWritingOptions.PrettyPrinted) 
      let wrapper = HttpWrapper() 
      wrapper.silentPostData(serverurl: mdmiosagent_Constants.NATIVE_APP_SERVLET, urldata: jsonData) 
     } catch { 
      NSLog("json error") 
     } 

} 
    location.delegate = self 
    location.desiredAccuracy = kCLLocationAccuracyNearestTenMeters 


      if #available(iOS 8.0, *) { 
       location.requestWhenInUseAuthorization() 
      } 
     location.delegate = self 
     location.startUpdatingLocation() 
     NSLog("Started to monitor the significant location Changes") 
    location.stopMonitoringSignificantLocationChanges() 
    location.startMonitoringSignificantLocationChanges() 

    } 

func location(manager: CLLocationManager!, 
        didChangeAuthorizationStatus status: CLAuthorizationStatus) { 
    var shouldIAllow = false 
    var locationStatus = String() 
    switch status { 
    case CLAuthorizationStatus.Restricted: 
     locationStatus = "Restricted Access to location" 
    case CLAuthorizationStatus.Denied: 
     locationStatus = "User denied access to location" 
    case CLAuthorizationStatus.NotDetermined: 
     locationStatus = "Status not determined" 
    default: 
     locationStatus = "Allowed to location Access" 
     shouldIAllow = true 
    } 
    NSNotificationCenter.defaultCenter().postNotificationName("LabelHasbeenUpdated", object: nil) 
    if (shouldIAllow == true) { 
     NSLog("Location to Allowed") 
     // Start location services 
     location.startUpdatingLocation() 
    } else { 
     NSLog("Denied access: \(locationStatus)") 
    } 
} 

func processMessage (dict : NSDictionary) { 
    let msgType : NSString = dict.objectForKey(mdmiosagent_Constants.MESSAGETYPEKEY) as! String 
    if (msgType.isEqual(mdmiosagent_Constants.MONITORREGIONKEY)) { 
     // self.startmonitoring(dict) 
    } 
} 

func stopLocationUpdates() { 
    location.stopMonitoringSignificantLocationChanges() 
    location.stopUpdatingLocation() 

} 
// this particular function is Used with two commands but not used as of now 
func startmonitoring (currentLocation : CLLocation) { 
    let latitude : Double = currentLocation.coordinate.latitude 
    let Longitude : Double = currentLocation.coordinate.longitude 
    let regionID = "GeoFenceTrack" 
    let region: CLCircularRegion = CLCircularRegion.init(center: CLLocationCoordinate2DMake(latitude, Longitude), radius: Double(mdmiosagent_Constants.LOCATIONRADIUS)! , identifier: regionID) 
    location.delegate = self 
    location.desiredAccuracy = kCLLocationAccuracyBest 
    location.stopMonitoringForRegion(region) 
} 

func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { 
    NSLog("Location Updated") 
    let persist = Persistence() 
    let currentLocation : CLLocation = locations[0] 
    let latitude : Double = currentLocation.coordinate.latitude 
    let Longitude : Double = currentLocation.coordinate.longitude 
    let regionID = "GeoFenceTrack" 
    let region : CLCircularRegion = CLCircularRegion.init(center: CLLocationCoordinate2DMake(latitude, Longitude), radius: Double(persist.getObject(mdmiosagent_Constants.LOCATIONRADIUS))!, identifier: regionID) 
    self.sendLocation(currentLocation) 
    location.desiredAccuracy = kCLLocationAccuracyHundredMeters 
    location.delegate = self 
    location.stopMonitoringForRegion(region) 
} 

func locationManager(manager: CLLocationManager, didUpdateToLocation newLocation: CLLocation, fromLocation oldLocation: CLLocation) { 
    NSLog("Location Updated") 

    let latitude : Double = newLocation.coordinate.latitude 
    let Longitude : Double = newLocation.coordinate.longitude 

    let regionID = "GeoFenceTrack" 
    let region : CLCircularRegion = CLCircularRegion.init(center: CLLocationCoordinate2DMake(latitude, Longitude), radius: Double(mdmiosagent_Constants.LOCATIONRADIUS)!, identifier: regionID) 
    location.startMonitoringForRegion(region) 
    let currentLocation = newLocation 
    self.sendLocation(currentLocation) 
} 

func sendLocation (currentLocation : CLLocation) { 
    let latitude: String = "\(Int(currentLocation.coordinate.latitude))" 
    let longitude : String = "\(Int(currentLocation.coordinate.longitude))" 

    let json: [NSObject : AnyObject] = [ 
     mdmiosagent_Constants.MESSAGETYPEKEY : mdmiosagent_Constants.LOCATIONMSGTYPEKEY, 
     mdmiosagent_Constants.LATITUDEKEY : latitude, 
     mdmiosagent_Constants.LONGITUDEKEY : longitude, 
     mdmiosagent_Constants.UDIDKEY : defaults.UDID, 
     "TimeStamp" : "\(self.toLocalTime())", 
     "IsLocationSettingsEnabled" : "\(CLLocationManager.locationServicesEnabled())" 
    ] 

    let userDefaults : NSUserDefaults = NSUserDefaults.standardUserDefaults() 
    userDefaults.setObject(json, forKey: mdmiosagent_Constants.MANAGED_APP_FEEDBACK) 
    userDefaults.synchronize() 
    do { 
     let jsonData : NSData = try NSJSONSerialization.dataWithJSONObject(json, options: NSJSONWritingOptions.PrettyPrinted) 
     let wrapper = HttpWrapper() 
     wrapper.silentPostData(serverurl: mdmiosagent_Constants.NATIVE_APP_SERVLET, urldata: jsonData) 
    } catch { 
     NSLog("json error") 
    } 
+0

Können Sie Ihre Frage ein und diese beiden Methoden angeben. Deine beiden Methoden haben in iOS7 nicht funktioniert? Ich weiß, dass es keine veralteten Funktionen zwischen iOS7 und iOS8 im CLLocationManagerDelegate gibt. https://developer.apple.com/library/ios/documentation/CoreLocation/Reference/CLLocationManagerDelegate_Protocol/ –

+0

Ich muss requestWhenInuseAuthorization() und requestAlwaysAuthorization() verwenden Recht, die Delegierten Methoden zum Aufruf zu bringen? Diese beiden Methoden sind nicht mehr gültig. –

+0

Sie müssen diese beiden Funktionen nicht vor iOS8 aufrufen, also ist Ihre Frage, wenn Sie diese Funktionen aufrufen, stürzt Ihre App ab? –

Antwort

2

in iOS 7 und höher Sie die folgenden Methoden

func locationManager(manager: CLLocationManager!,didUpdateLocations locations: [AnyObject]!){ 
    print("latitude:\(manager.location.coordinate.latitude)") 
    print("longitude:\(manager.location.coordinate.longitude)") 
} 


func locationManager(manager: CLLocationManager!,didFailWithError error: NSError!){ 
    print("error") 
} 

gewährleisten, wenn Ihre Klasse bestätigen können

import CoreLocation 

class ViewController: UIViewController , CLLocationManagerDelegate{ 

chcek wie auf Abruf

self.locationManager = CLLocationManager() 
    let Device = UIDevice.currentDevice() 
private let iosVersion = Double(Device.systemVersion) ?? 0 
if iosVersion >= 8 { 
self.locationManager.requestWhenInUseAuthorization() 
} 
self.locationManager.delegate = self 
self.locationManager.startUpdatingLocation() 

Update

diese hinzufügen und überprüfen, sobald der Status bereits gewährt wird oder nicht

// authorization status 
func locationManager(manager: CLLocationManager!, 
    didChangeAuthorizationStatus status: CLAuthorizationStatus) { 
     var shouldIAllow = false 

     switch status { 
     case CLAuthorizationStatus.Restricted: 
      locationStatus = "Restricted Access to location" 
     case CLAuthorizationStatus.Denied: 
      locationStatus = "User denied access to location" 
     case CLAuthorizationStatus.NotDetermined: 
      locationStatus = "Status not determined" 
     default: 
      locationStatus = "Allowed to location Access" 
      shouldIAllow = true 
     } 
      NSNotificationCenter.defaultCenter().postNotificationName("LabelHasbeenUpdated", object: nil) 
     if (shouldIAllow == true) { 
      NSLog("Location to Allowed") 
      // Start location services 
      locationManager.startUpdatingLocation() 
     } else { 
      NSLog("Denied access: \(locationStatus)") 
     } 
} 
+0

Kann ich sie als solche verwenden, ohne requestWhenInuseAuthorization() und requestAlwaysAuthorization() aufzurufen? –

+0

Es ist keine Unterklasse von UIViewController, verhindert das, dass die Methoden aufgerufen werden? –

+0

chcek die aktualisierte Antwort bro –