수색…


통사론

  1. desiredAccuracy
  2. distanceFilter
  3. requestLocation ()
  4. startUpdatingLocation ()
  5. allowDeferredLocationUpdates (untilTraveled : timeout :)
  6. startMonitoringSignificantLocationChanges ()
  7. allowDeferredLocationUpdates (untilTraveled : timeout :)
  8. authorizedAlways
  9. authorizedWhenInUse
  10. locationManager (_ : didChangeAuthorization :)

비고

런타임시 위치 시뮬레이션

  1. Xcode에서 앱을 실행합니다.
  2. 디버그 바에서 "위치 시뮬레이션"버튼을 클릭하십시오.
  3. 메뉴에서 위치를 선택하십시오.

위치 시뮬레이션 - 디버그

CoreLocation 프레임 워크 연결

CoreLocation 기능을 사용하는 클래스에서 CoreLocation 모듈을 가져옵니다.

//Swift
import CoreLocation

//Objective-C
#import <CoreLocation/CoreLocation.h>

위치 서비스 사용 권한 요청

다음을 사용하여 앱의 승인 상태를 확인합니다.

//Swift
let status: CLAuthorizationStatus = CLLocationManager.authorizationStatus()

//Objective-C
CLAuthorizationStatus status = [CLLocationManager authorizationStatus];

다음 상수와 비교하여 상태를 테스트하십시오.

//Swift
switch status {
case .NotDetermined:
    // Do stuff
case .AuthorizedAlways:
    // Do stuff
case .AuthorizedWhenInUse:
    // Do stuff
case .Restricted:
    // Do stuff
case .Denied:
    // Do stuff
}

//Objective-C
switch (status) {
    case kCLAuthorizationStatusNotDetermined:
        
        //The user hasn't yet chosen whether your app can use location services or not.
        
        break;
        
    case kCLAuthorizationStatusAuthorizedAlways:
        
        //The user has let your app use location services all the time, even if the app is in the background.
        
        break;
        
    case kCLAuthorizationStatusAuthorizedWhenInUse:
        
        //The user has let your app use location services only when the app is in the foreground.
        
        break;
        
    case kCLAuthorizationStatusRestricted:
        
        //The user can't choose whether or not your app can use location services or not, this could be due to parental controls for example.
        
        break;
        
    case kCLAuthorizationStatusDenied:
        
        //The user has chosen to not let your app use location services.
        
        break;
        
    default:
        break;
}

응용 프로그램을 사용하는 동안 위치 서비스 권한 받기

사용 중일 때 위치 대화 상자

가장 간단한 방법은 위치 관리자를 루트보기 컨트롤러의 속성으로 초기화하고 viewDidLoad 권한 요청을 배치하는 것입니다.

그러면 권한을 요청하는 경고 컨트롤러가 나타납니다.

//Swift
let locationManager = CLLocationManager()
locationManager.requestWhenInUseAuthorization()

//Objective-C
CLLocationManager *locationManager = [[CLLocationManager alloc] init];
[locationManager requestWhenInUseAuthorization];

Info.plist에 NSLocationWhenInUseUsageDescription 키를 추가하십시오. 이 값은 경고 컨트롤러의 message 레이블에 사용됩니다.

여기에 이미지 설명을 입력하십시오.


위치 서비스 허가 받기 항상

여기에 이미지 설명을 입력하십시오.

앱이 활성화되어 있지 않은 경우에도 위치 서비스 사용 권한을 요청하려면 다음 호출을 대신 사용하십시오.

//Swift
locationManager.requestAlwaysAuthorization()

//Objective-C
[locationManager requestAlwaysAuthorization];

그런 다음 NSLocationAlwaysUsageDescription 키를 Info.plist에 추가하십시오. 다시 값은 경고 컨트롤러의 message 레이블에 사용됩니다.

여기에 이미지 설명을 입력하십시오.

GPX 파일을 사용하여 사용자 정의 위치 추가

위치 서비스를 확인하려면 실제 장치가 필요하지만 테스트 목적으로 시뮬레이터를 사용하고 아래 단계에 따라 자체 위치를 추가 할 수도 있습니다.

  • 새 GPX 파일을 프로젝트에 추가하십시오.
  • GPX 파일에서 웨이 포인트 추가
<?xml version="1.0"?>
<gpx version="1.1" creator="Xcode"> 
<!--
        Provide one or more waypoints containing a latitude/longitude pair. If you provide one
        waypoint, Xcode will simulate that specific location. If you provide multiple waypoints,
        Xcode will simulate a route visitng each waypoint.
 -->
<wpt lat="52.599878" lon="4.702029">
     <name>location name (eg. Florida)</name>
</wpt>
  • 그런 다음 제품 -> 구성표 -> 구성표 편집으로 가서 RUN으로 기본 위치를 GPX 파일 이름으로 설정하십시오.

백그라운드에서 위치 서비스

응용 프로그램이 백그라운드에서 실행되는 동안 표준 위치 서비스를 사용하려면 먼저 대상 설정의 기능 탭에서 Background Modes 를 켜고 Location updates 선택해야 Location updates .

배경 모드

또는 Info.plist에 직접 추가하십시오.

<key>NSLocationAlwaysUsageDescription</key>
<string>I want to get your location Information in background</string>

<key>UIBackgroundModes</key>
<array>
    <string>location</string>
</array>

그런 다음 CLLocationManager를 설정해야합니다.

목표 C

//The Location Manager must have a strong reference to it.
_locationManager = [[CLLocationManager alloc] init];
_locationManager.delegate = self;

//Request Always authorization (iOS8+)
if ([_locationManager respondsToSelector:@selector(requestAlwaysAuthorization)]) {
    [_locationManager requestAlwaysAuthorization];
}

//Allow location updates in the background (iOS9+)
if ([_locationManager respondsToSelector:@selector(allowsBackgroundLocationUpdates)]) {
    _locationManager.allowsBackgroundLocationUpdates = YES;
}

[_locationManager startUpdatingLocation];

빠른

self.locationManager.delegate = self

if #available (iOS 8.0,*) {
    self.locationManager.requestAlwaysAuthorization()
}

if #available (iOS 9.0,*) {
    self.locationManager.allowsBackgroundLocationUpdates = true
}

self.locationManager.startUpdatingLocation()


Modified text is an extract of the original Stack Overflow Documentation
아래 라이선스 CC BY-SA 3.0
와 제휴하지 않음 Stack Overflow