2016-11-25 4 views

Antwort

3

Für Benutzer aktuellen Standort bekommen müssen Sie erklären:

let locationManager = CLLocationManager() 

In viewDidLoad(): Sie aktuelle Standort Benutzer

// Ask for Authorisation from the User. 
    self.locationManager.requestAlwaysAuthorization() 

    // For use in foreground 
    self.locationManager.requestWhenInUseAuthorization() 

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

Dann in CLLocationManagerDelegate Methode Koordinaten erhalten kann:

func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { 
    var locValue:CLLocationCoordinate2D = manager.location.coordinate 
    print("locations = \(locValue.latitude) \(locValue.longitude)") 
} 

Wenn der Benutzer den Standort abgelehnt hat, geben Sie ihm die Option, den Standort pe zu ändern rmission von den Einstellungen:

func locationManager(manager: CLLocationManager, didFailWithError error: NSError) { 
    print("error::: \(error)") 
    locationManager.stopUpdatingLocation() 
    let alert = UIAlertController(title: "Settings", message: "Allow location from settings", preferredStyle: UIAlertControllerStyle.Alert) 
    self.presentViewController(alert, animated: true, completion: nil) 
    alert.addAction(UIAlertAction(title: TextMessages().callAlertTitle, style: .Default, handler: { action in 
     switch action.style{ 
     case .Default: UIApplication.sharedApplication().openURL(NSURL(string: UIApplicationOpenSettingsURLString)!) 
     case .Cancel: print("cancel") 
     case .Destructive: print("destructive") 
     } 
    })) 
} 
0

Nein, Sie können sie nur innerhalb der App innerhalb einer Warnung erinnern, z. B. "Zur besseren Verwendung schlagen wir Ihnen vor, uns die Erlaubnis zur Verwendung Ihres Standorts zu geben". Und um den Button "Gehe zu Einstellungen" hinzuzufügen, der den Benutzer zu den Standortberechtigungen für Ihre Anwendung umleitet.

+0

ist das ernsthaft die Antwort? –

+0

@NumanKaraaslan Ja. Und wenn Sie es sorgfältig lesen, werden Sie feststellen, dass dies eine kurze Variante der angenommenen Antwort ist. – Lexorus

Verwandte Themen