위치 권한 선언

 
#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>
 
 
@interface ViewController : UIViewController<CLLocationManagerDelegate>
@property (strong, nonatomic) CLLocationManager *locationManager;
@property (strong, nonatomic) IBOutlet UILabel *txtGps;
 
 
@end
 
cs

 

 

#import "ViewController.h"
 
@interface ViewController ()
 
@end
 
@implementation ViewController
 
- (void)viewDidLoad {
    [super viewDidLoad];
 
    self.locationManager = [[CLLocationManager allocinit];
    self.locationManager.delegate = self;
    // 위치 정확도 설정
    self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    
    CLAuthorizationStatus status = self.locationManager.authorizationStatus;
    if (status == kCLAuthorizationStatusNotDetermined) {
        // 권한요청
        [self.locationManager requestWhenInUseAuthorization];
    }
    else if (status == kCLAuthorizationStatusAuthorizedWhenInUse || status == kCLAuthorizationStatusAuthorizedAlways) {
        // 위치 업데이트 시작
        [self.locationManager startUpdatingLocation];
    }
    else {
        NSLog(@"위치 권한 거부");
    }
}
 
// 위치 권한 받음
- (void)locationManagerDidChangeAuthorization:(CLLocationManager *)manager {
    CLAuthorizationStatus status = manager.authorizationStatus;
    if (status == kCLAuthorizationStatusAuthorizedWhenInUse || status == kCLAuthorizationStatusAuthorizedAlways) {
        // 위치 업데이트 시작
        [self.locationManager startUpdatingLocation];
    }
}
 
// 위치 업데이트
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations {
    CLLocation *location = locations.lastObject;
    if(location) {
        double latitude = location.coordinate.latitude;
        double longitude = location.coordinate.longitude;
        [self.txtGps setText:[NSString stringWithFormat:@"위도 : %f , 경도 : %f", latitude, longitude]];
    }
}
 
// 위치 업데이트 - 실패
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {
    [self.txtGps setText:@""];
}
 
 
@end
 
cs

 

 

결과

'IT > Ⅰ. IOS' 카테고리의 다른 글

[IOS] 파일 읽기, 쓰기  (0) 2025.07.09
[IOS] TTS 사용  (7) 2025.07.06
[IOS] FCM Crashlytics 로그  (2) 2025.07.06
[IOS] FCM 발송  (0) 2025.06.30
[IOS] GIF, SVG, APNG 이미지 사용  (0) 2025.05.22

+ Recent posts