2016-11-25 3 views

답변

3

당신은 선언 할 필요가 그런 CLLocationManagerDelegate 방법에 당신이 얻을 수있는

// 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() 
    } 

를 사용자의 현재 위치 좌표 :

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

사용자가 위치를 거부 한 경우 위치를 변경하는 옵션을 제공합니다. pe 설정에서 접수 :

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

아니요, '더 나은 사용을 위해 사용자에게 내 위치 사용 권한을 제공 할 것을 제안합니다.'와 같은 알림 내에서 알림을 앱에 알릴 수 있습니다. 또한 응용 프로그램의 위치 권한으로 사용자를 리디렉션하는 "설정으로 이동"버튼을 추가하십시오. 있는 viewDidLoad에서

let locationManager = CLLocationManager() 

() : 사용자의 현재 위치를 얻기 위해

+0

이 답변은 심각합니까? –

+0

@ NumanKaraaslan 네. 그리고주의 깊게 읽으면, 이것이 수용된 대답의 짧은 변종임을 알게 될 것입니다. – Lexorus